Java Code Examples for com.intellij.lang.PsiBuilder.getUserData()

The following are Jave code examples for showing how to use getUserData() of the com.intellij.lang.PsiBuilder class. You can vote up the examples you like. Your votes will be used in our system to get more good examples.
Example 1
Project: AppleScript-IDEA   File: AppleScriptGeneratedParserUtil.java   Source Code and License Vote up 6 votes
public static boolean parseApplicationHandlerDefinitionSignature(PsiBuilder b, int l) {
  if (!recursion_guard_(b, l, "parseApplicationHandlerDefinitionSignature")) return false;
  boolean r;
  //application handled definition in script only makes sense inside <using terms of> statement
  if (b.getUserData(IS_PARSING_USING_TERMS_FROM_STATEMENT) != Boolean.TRUE || b.getUserData(PARSING_TELL_COMPOUND_STATEMENT) == Boolean.TRUE) return false;

  StringHolder parsedCommandName = new StringHolder();
  String toldApplicationName = getTargetApplicationName(b);
  PsiBuilder.Marker m2 = enter_section_(b, l, _COLLAPSE_, "<parse Application Handler Definition");
  r = parseDictionaryCommandNameInner(b, l + 1, parsedCommandName, toldApplicationName, true, null);
  exit_section_(b, l, m2, DICTIONARY_COMMAND_NAME, r, false, null);

  if (!r) return false;
  // TODO: 06/12/15 may be try to avoid creating PSI here!..
  List<AppleScriptCommand> allCommandsWithName = getAllCommandsWithName(b, parsedCommandName.value, toldApplicationName, false, null);

  for (AppleScriptCommand command : allCommandsWithName) {
    r = parseParametersForCommand(b, l + 1, command);//custom parsing here
    if (r) {
      break;
    }
  }
  boolean incompleteHandlerCall = !r && allCommandsWithName.size() > 0 && (b.getTokenType() == NLS || b.eof());
  return r || incompleteHandlerCall;

}
 
Example 2
Project: AppleScript-IDEA   File: AppleScriptGeneratedParserUtil.java   Source Code and License Vote up 6 votes
/**
   * If inside tell (only in a tell?) compound statement - first check it's terms
   */
  public static boolean parseExpression(PsiBuilder b, int l, String dictionaryTermToken, Parser expression) {
    if (!recursion_guard_(b, l, "parseExpression")) return false;
    if (!nextTokenIsFast(b, dictionaryTermToken)) return false;
    boolean r;

    //check application terms first
    if (b.getUserData(PARSING_TELL_COMPOUND_STATEMENT) == Boolean.TRUE) {
      String toldAppName = peekTargetApplicationName(b);
      if (!StringUtil.isEmpty(toldAppName)) {
        StringHolder parsedName = new StringHolder();
        PsiBuilder.Marker mComName = enter_section_(b, l, _AND_, "<parse Expression>");
        r = parseCommandNameForApplication(b, l + 1, parsedName, toldAppName, true);
        exit_section_(b, l, mComName, null, r, false, null);
        if (r) return false;
        if (ParsableScriptSuiteRegistryHelper.isPropertyWithPrefixExist(toldAppName, dictionaryTermToken)) {
          return false;
//          PsiBuilder.Marker m = enter_section_(b, l, _AND_, null, "<dictionary constant>");
//          r = parseDictionaryConstant(b, l + 1);
//          exit_section_(b, l, m, r, false, null);
//          if (r) return false;
        }
      }
    }
    return expression.parse(b, l + 1);
  }
 
Example 3
Project: AppleScript-IDEA   File: AppleScriptGeneratedParserUtil.java   Source Code and License Vote up 6 votes
/**
 * If rule matches, removes added application name from the name stack which was used for parsing application terms
 *
 * @param b {@link PsiBuilder}
 * @param l level deep
 * @return true if rule matches
 */
public static boolean parseTellCompoundStatement(PsiBuilder b, int l) {
  if (!recursion_guard_(b, l, "parseTellCompoundStatement")) return false;
  if (!nextTokenIs(b, TELL)) return false;
  boolean r;
  boolean pushStateBefore = b.getUserData(APPLICATION_NAME_PUSHED) == Boolean.TRUE;
  boolean compoundStateBefore = b.getUserData(PARSING_TELL_COMPOUND_STATEMENT) == Boolean.TRUE;
  b.putUserData(APPLICATION_NAME_PUSHED, false);
  b.putUserData(PARSING_TELL_COMPOUND_STATEMENT, true);

  r = AppleScriptParser.tellCompoundStatement(b, l + 1);

  b.putUserData(PARSING_TELL_COMPOUND_STATEMENT, compoundStateBefore);
  popApplicationNameIfWasPushed(b);
  b.putUserData(APPLICATION_NAME_PUSHED, pushStateBefore);
  return r;
}
 
Example 4
Project: AppleScript-IDEA   File: AppleScriptGeneratedParserUtil.java   Source Code and License Vote up 6 votes
/**
 * If rule matches, removes added application name from the name stack which was used for parsing application terms
 *
 * @param b {@link PsiBuilder}
 * @param l level deep
 * @return true if rule matches
 */
public static boolean parseUsingTermsFromStatement(PsiBuilder b, int l) {
  if (!recursion_guard_(b, l, "parseUsingTermsFromStatement")) return false;
  boolean r;
  boolean oldParseUsingTermsState = b.getUserData(IS_PARSING_USING_TERMS_FROM_STATEMENT) == Boolean.TRUE;
  boolean oldPushedState = b.getUserData(APPLICATION_NAME_PUSHED) == Boolean.TRUE;
  b.putUserData(IS_PARSING_USING_TERMS_FROM_STATEMENT, true);
  b.putUserData(APPLICATION_NAME_PUSHED, false);

  r = AppleScriptParser.usingTermsFromStatement(b, l + 1);

  popApplicationNameIfWasPushed(b);
  b.putUserData(IS_PARSING_USING_TERMS_FROM_STATEMENT, oldParseUsingTermsState);
  b.putUserData(APPLICATION_NAME_PUSHED, oldPushedState);

  return r;
}
 
Example 5
Project: AppleScript-IDEA   File: AppleScriptGeneratedParserUtil.java   Source Code and License Vote up 6 votes
public static boolean parseDictionaryCommandName(PsiBuilder b, int l) {
  boolean r;
  if (nextTokenIs(b, NLS)) {
    return false;
  }
  StringHolder parsedCommandName = new StringHolder();
  String toldApplicationName = getTargetApplicationName(b);
  boolean areThereUseStatements = b.getUserData(WAS_USE_STATEMENT_USED) == Boolean.TRUE;
  Set<String> applicationsToImport = null;
  if (areThereUseStatements) {
    applicationsToImport = b.getUserData(USED_APPLICATION_NAMES);
  }
  PsiBuilder.Marker m = enter_section_(b, l, _COLLAPSE_, "<parse ApplicationDictionary Command Name>");
  r = parseDictionaryCommandNameInner(b, l + 1, parsedCommandName, toldApplicationName, areThereUseStatements, applicationsToImport);
  exit_section_(b, l, m, DICTIONARY_COMMAND_NAME, r, false, null);
  return r;
}
 
Example 6
Project: AppleScript-IDEA   File: AppleScriptGeneratedParserUtil.java   Source Code and License Vote up 6 votes
/**
 * @param b                     {@link PsiBuilder}
 * @param l                     level deep
 * @param isPluralForm          if parsing plural form of a class name
 * @param checkForUseStatements parser rule for the check if there are use statements in the script
 * @return true in case rule matches, false otherwise
 */
public static boolean parseDictionaryClassName(PsiBuilder b, int l, final boolean isPluralForm, @NotNull Parser checkForUseStatements) {
  if (!recursion_guard_(b, l, "parseDictionaryClassName")) return false;
  boolean r;
  final String s = b.getTokenText();
  if (s == null || s.length() == 0 || !AppleScriptNames.isIdentifierStart(s.charAt(0))) return false;
  final String toldApplicationName = getTargetApplicationName(b);
  final boolean areThereUseStatements = checkForUseStatements.parse(b, l + 1);
  Set<String> applicationsToImportFrom = null;
  if (areThereUseStatements) {
    applicationsToImportFrom = b.getUserData(USED_APPLICATION_NAMES);
  }
  final StringHolder currentTokenText = new StringHolder();
  currentTokenText.value = s;
  r = parseDictionaryClassName(b, l + 1, currentTokenText, isPluralForm, toldApplicationName, areThereUseStatements, applicationsToImportFrom);
  return r;
}
 
Example 7
Project: AppleScript-IDEA   File: AppleScriptGeneratedParserUtil.java   Source Code and License Vote up 5 votes
/**
 * <<< COMMAND_HANDLER_CALL >>>
 */
// commandName commandParameters?
public static boolean parseCommandHandlerCallExpression(PsiBuilder b, int l) {
  if (!recursion_guard_(b, l, "parseCommandHandlerCallExpression")) return false;
  boolean r;

  if (nextTokenIs(b, NLS)) return false;
  final String s = b.getTokenText();

  if (s == null || s.length() == 0 || !AppleScriptNames.isIdentifierStart(s.charAt(0))) return false;
  StringHolder parsedCommandName = new StringHolder();
  //get current application name to which messages will be sent in the current block
  String toldApplicationName = getTargetApplicationName(b);
  //if there are <use statements> present in the script
  boolean areThereUseStatements = b.getUserData(WAS_USE_STATEMENT_USED) == Boolean.TRUE;
  Set<String> applicationsToImport = null;
  if (areThereUseStatements) {
    //adding list of application names from use statements
    applicationsToImport = b.getUserData(USED_APPLICATION_NAMES);
  }
  PsiBuilder.Marker m2 = enter_section_(b, l, _COLLAPSE_, "<parse Command Handler Call Expression>");
  // TODO: 19/12/15 need to parse command name together with parameters for each possible application in order to be
  // able to parse the longest possible application name ('open for access' std lib vs 'open' from application dict)
  r = parseDictionaryCommandNameInner(b, l + 1, parsedCommandName, toldApplicationName, areThereUseStatements, applicationsToImport);
  exit_section_(b, l, m2, DICTIONARY_COMMAND_NAME, r, false, null);

  if (!r) return false;
  // TODO: 06/12/15 may be try to avoid creating PSI here!..
  List<AppleScriptCommand> allCommandsWithName = getAllCommandsWithName(b, parsedCommandName.value, toldApplicationName, areThereUseStatements,
          applicationsToImport);

  for (AppleScriptCommand command : allCommandsWithName) {
    r = parseParametersForCommand(b, l + 1, command);
    if (r) {
      break;
    }
  }
  boolean incompleteHandlerCall = !r && allCommandsWithName.size() > 0 && (b.getTokenType() == NLS || b.eof());
  return r || incompleteHandlerCall;
}
 
Example 8
Project: AppleScript-IDEA   File: AppleScriptGeneratedParserUtil.java   Source Code and License Vote up 5 votes
@Nullable
private static String peekTargetApplicationName(PsiBuilder b) {
  Stack<String> applicationNameStack = b.getUserData(TOLD_APPLICATION_NAME_STACK);
  String toldApplicationName = null;
  if (applicationNameStack != null && !applicationNameStack.isEmpty()) {
    toldApplicationName = applicationNameStack.peek();
  }
  return toldApplicationName;
}
 
Example 9
Project: AppleScript-IDEA   File: AppleScriptGeneratedParserUtil.java   Source Code and License Vote up 5 votes
public static boolean parseTellSimpleStatementInner(PsiBuilder b, int l) {
  if (!recursion_guard_(b, l, "tellSimpleStatement")) return false;
  if (!nextTokenIs(b, TELL)) return false;
  boolean r;
  boolean pushStateBefore = b.getUserData(APPLICATION_NAME_PUSHED) == Boolean.TRUE;
  b.putUserData(APPLICATION_NAME_PUSHED, false);
  b.putUserData(PARSING_TELL_SIMPLE_STATEMENT, true);
  r = AppleScriptParser.tellSimpleStatement(b, l + 1);
  b.putUserData(PARSING_TELL_SIMPLE_STATEMENT, false);
  popApplicationNameIfWasPushed(b);
  b.putUserData(APPLICATION_NAME_PUSHED, pushStateBefore);
  return r;
}
 
Example 10
Project: AppleScript-IDEA   File: AppleScriptGeneratedParserUtil.java   Source Code and License Vote up 5 votes
private static void popApplicationNameIfWasPushed(PsiBuilder b) {
  if (b.getUserData(APPLICATION_NAME_PUSHED) == Boolean.TRUE) {
    Stack<String> dictionaryNameStack = b.getUserData(TOLD_APPLICATION_NAME_STACK);
    if (dictionaryNameStack != null && !dictionaryNameStack.isEmpty()) {//should always be true if r==true
      dictionaryNameStack.pop();
    }
  }
}
 
Example 11
Project: AppleScript-IDEA   File: AppleScriptGeneratedParserUtil.java   Source Code and License Vote up 5 votes
/**
 * Add application name from this &lt;use statement&gt; to the set of application names from dictionary of which to
 * import terms if importing condition is not explicitly prohibited by syntax
 *
 * @param b           {@link PsiBuilder}
 * @param l           level deep
 * @param isImporting whether to make terms from this application's dictionary available in the script
 * @return true if this is application reference
 */
public static boolean parseUsedApplicationNameExternal(PsiBuilder b, int l, Parser isImporting) {
  if (!recursion_guard_(b, l, "parseUsedApplicationNameExternal")) return false;
  boolean r;
  if (!nextTokenIs(b, "parseUsedApplicationNameExternal", APPLICATION, APP, SCRIPTING_ADDITIONS)) return false;
  String appName = null;
  r = consumeToken(b, SCRIPTING_ADDITIONS);
  if (r) {
    appName = ApplicationDictionary.SCRIPTING_ADDITIONS_LIBRARY;
  } else {
    PsiBuilder.Marker mAppRef = enter_section_(b, l, _NONE_, "<application reference>");
    PsiBuilder.Marker mCls = enter_section_(b, l, _NONE_, "<dictionary class name>");
    r = consumeToken(b, APPLICATION);
    if (!r) r = consumeToken(b, APP);
    exit_section_(b, l, mCls, DICTIONARY_CLASS_NAME, r, false, null);
    if (r) {
      String appNameStr = b.getTokenText();
      r = consumeToken(b, STRING_LITERAL);
      if (r && !StringUtil.isEmpty(appNameStr)) {
        appName = appNameStr.replace("\"", "");
      }
    }
    exit_section_(b, l, mAppRef, APPLICATION_REFERENCE, r, false, null);
  }
  boolean doTermsImport = isImporting.parse(b, l + 1);
  if (doTermsImport && !StringUtil.isEmpty(appName)) {
    Set<String> usedAppNames = b.getUserData(USED_APPLICATION_NAMES);
    if (usedAppNames == null) {
      usedAppNames = new HashSet<>();
      b.putUserData(USED_APPLICATION_NAMES, usedAppNames);
    }
    usedAppNames.add(appName);
  }
  return r;
}
 
Example 12
Project: AppleScript-IDEA   File: AppleScriptGeneratedParserUtil.java   Source Code and License Vote up 5 votes
/**
 * If there is at least one &lt;use statement&gt; in script, set WAS_USE_STATEMENT_USED flag to true
 */
public static boolean parseUseStatement(PsiBuilder b, int l, Parser useStatement) {
  if (!recursion_guard_(b, l, "parseUseStatement")) return false;
  boolean r = useStatement.parse(b, l + 1);
  boolean prevPass = b.getUserData(WAS_USE_STATEMENT_USED) == Boolean.TRUE;
  b.putUserData(WAS_USE_STATEMENT_USED, r || prevPass);
  return r;
}
 
Example 13
Project: AppleScript-IDEA   File: AppleScriptGeneratedParserUtil.java   Source Code and License Vote up 5 votes
/**
 * @return true if this is the application reference of a &lt;tell&gt; or &lt;using terms
 * from&gt; statements
 */
public static boolean isTellStatementStart(PsiBuilder b, int l) {
  if (!isInTellStatement(b, l + 1)) return false;
  int i = -1;
  IElementType prevElem = b.rawLookup(i);
  while (prevElem == com.intellij.psi.TokenType.WHITE_SPACE || prevElem == MY
          || prevElem == APPLICATION || prevElem == STRING_LITERAL || prevElem == null
          || prevElem == ID) {
    prevElem = b.rawLookup(--i);
  }
  return prevElem == TELL
          || (b.getUserData(IS_PARSING_USING_TERMS_FROM_STATEMENT) == Boolean.TRUE)
          && prevElem == FROM;
}
 
Example 14
Project: AppleScript-IDEA   File: AppleScriptGeneratedParserUtil.java   Source Code and License Vote up 5 votes
private static Stack<String> pushTargetApplicationName(PsiBuilder b, @NotNull String applicationNameString) {
  Stack<String> dictionaryNameStack = b.getUserData(TOLD_APPLICATION_NAME_STACK);
  if (dictionaryNameStack == null) {
    dictionaryNameStack = new Stack<>();
    b.putUserData(TOLD_APPLICATION_NAME_STACK, dictionaryNameStack);
  }
  dictionaryNameStack.push(applicationNameString);
  b.putUserData(APPLICATION_NAME_PUSHED, true);
  return dictionaryNameStack;
}
 
Example 15
Project: AppleScript-IDEA   File: AppleScriptGeneratedParserUtil.java   Source Code and License Vote up 5 votes
private static boolean parseCommandDirectParameterValue(PsiBuilder b, int l,
                                                          @NotNull CommandDirectParameter parameter) {
    if (!recursion_guard_(b, l, "parseCommandDirectParameterValue")) return false;
    boolean r = false;
//    String parameterTypeSpecifier = parameter.getTypeSpecifier();
    // if we are inside tell compound statement=> direct parameter is optional
    // so it may be wrongly detected instead of parameter selector. So, checking if it is a parameter selector first
    boolean isTellCompound = b.getUserData(PARSING_TELL_COMPOUND_STATEMENT) == Boolean.TRUE;
    if (isTellCompound || parameter.isOptional()) {
      AppleScriptCommand myCmd = parameter.getCommand();
      for (String paramName : myCmd.getParameterNames()) {
        if (nextTokenIs(b, paramName)) return true;
      }
    }
    PsiBuilder.Marker m = enter_section_(b, l, _NONE_, "<parse Command Direct Parameter Value >");
    String parameterTypeSpecifier = parameter.getTypeSpecifier();
    if ("type".equals(parameterTypeSpecifier)) {
      r = typeSpecifier(b, l + 1);
    }
    // see https://bitbucket.org/adernov/applescript-internal/issues/23/coercion-expression-priority
//    else if ("text".equals(parameterTypeSpecifier)) {
//      r = AppleScriptParser.coercionExpressionWrapper(b, l + 1);
//      if (!r) r = AppleScriptParser.concatenationExpressionWrapper(b, l + 1);
//      if (!r) r = AppleScriptParser.stringLiteralExpression(b, l + 1);
//    }
    if (!r)
      r = com.intellij.plugin.applescript.lang.parser.AppleScriptParser.expression(b, l + 1);
    exit_section_(b, l, m, DIRECT_PARAMETER_VAL, r, false, null);
    // A tell statement specifies a default target for all commands contained
    // within it, so the direct parameter is optional.
    return r || parameter.isOptional() || isTellCompound;
  }
 
Example 16
Project: AppleScript-IDEA   File: AppleScriptGeneratedParserUtil.java   Source Code and License Vote up 5 votes
public static boolean parseDictionaryPropertyName(PsiBuilder b, int l) {
  if (!recursion_guard_(b, l, "parseDictionaryPropertyInner")) return false;
  boolean r;
  String toldApplicationName = getTargetApplicationName(b);
  boolean areThereUseStatements = b.getUserData(WAS_USE_STATEMENT_USED) == Boolean.TRUE;
  Set<String> applicationsToImportFrom = null;
  if (areThereUseStatements) {
    applicationsToImportFrom = b.getUserData(USED_APPLICATION_NAMES);
  }
  r = tryToParseApplicationProperty(b, l + 1, toldApplicationName);
  if (r) return true;

  if (areThereUseStatements) {
    if (applicationsToImportFrom != null && !applicationsToImportFrom.isEmpty()) {
      for (String appName : applicationsToImportFrom) {
        r = tryToParseApplicationProperty(b, l + 1, appName);
        if (r) return true;
      }
    }
  } else {
    r = tryToParseStdProperty(b, l + 1);
    if (r) return true;
    // if told app name == standard additions, Cocoa Standard terms were not checked
    r = tryToParseApplicationProperty(b, l + 1, ApplicationDictionary.COCOA_STANDARD_LIBRARY);
    if (r) return true;
  }
  return false;
}
 
Example 17
Project: AppleScript-IDEA   File: AppleScriptGeneratedParserUtil.java   Source Code and License Vote up 5 votes
public static boolean parseDictionaryConstant(PsiBuilder b, int l) {
  if (!recursion_guard_(b, l, "parseDictionaryConstant")) return false;
  boolean r, propertyOrClassExists = false;
  String toldApplicationName = getTargetApplicationName(b);
  //TODO: to think how to better handle such situations?
  // (if there are too many constants defined -> lead to many incorrect parsing errors like
  // 'end' tell/repeat etc is not detected
  // dictionary constant could appear only if we are inside dictionary command call
  boolean insideExpression = (b.getUserData(PARSING_COMMAND_HANDLER_CALL_PARAMETERS) == Boolean.TRUE
          || b.getUserData(PARSING_COMMAND_ASSIGNMENT_STATEMENT) == Boolean.TRUE)
          || b.getUserData(PARSING_LITERAL_EXPRESSION) == Boolean.TRUE;
  if (!ApplicationDictionary.COCOA_STANDARD_LIBRARY.equals(toldApplicationName) && !insideExpression)//only inside 
    // tell statements?
    return false;

  final StringHolder currentTokenText = new StringHolder();
  currentTokenText.value = "";
  boolean areThereUseStatements = b.getUserData(WAS_USE_STATEMENT_USED) == Boolean.TRUE;
  Set<String> applicationsToImportFrom = null;
  if (areThereUseStatements) {
    applicationsToImportFrom = b.getUserData(USED_APPLICATION_NAMES);
  }
  r = tryToParseApplicationConstant(b, l + 1, toldApplicationName);
  if (r) return true;
  if (areThereUseStatements && insideExpression) {
    if (applicationsToImportFrom != null && !applicationsToImportFrom.isEmpty()) {
      for (String appName : applicationsToImportFrom) {
        r = tryToParseApplicationConstant(b, l + 1, appName);
        if (r) return true;
      }
    }
  } else {
    r = tryToParseStdConstant(b, l + 1);
    if (r) return true;
    r = tryToParseApplicationConstant(b, l + 1, ApplicationDictionary.COCOA_STANDARD_LIBRARY);
    if (r) return true;
  }
  return false;
}
 
Example 18
Project: AppleScript-IDEA   File: AppleScriptGeneratedParserUtil.java   Source Code and License Vote up 4 votes
private static boolean isInTellStatement(PsiBuilder b, int l) {
  return (b.getUserData(PARSING_TELL_SIMPLE_STATEMENT) == Boolean.TRUE && nextTokenIs(b, TO))
          || (b.getUserData(PARSING_TELL_COMPOUND_STATEMENT) == Boolean.TRUE
          && b.getUserData(PARSING_TELL_SIMPLE_STATEMENT) == Boolean.FALSE)
          || b.getUserData(IS_PARSING_USING_TERMS_FROM_STATEMENT) == Boolean.TRUE;
}
 
Example 19
Project: AppleScript-IDEA   File: AppleScriptGeneratedParserUtil.java   Source Code and License Vote up 4 votes
public static boolean parseCheckForUseStatements(PsiBuilder b, int l) {
  return recursion_guard_(b, l, "parseCheckForUseStatements") && b.getUserData(WAS_USE_STATEMENT_USED) == Boolean.TRUE;
}
 
Example 20
Project: AppleScript-IDEA   File: AppleScriptGeneratedParserUtil.java   Source Code and License Vote up 3 votes
/**
   * Call handler with labeled parameters in grammar not allowed in assignment, tell statements or when parsing
   * application command parameters
   *
   * @param b {@link PsiBuilder}
   * @param l level deep
   * @return true if handlerLabeledParametersCall is currently allowed
   */
  public static boolean isHandlerLabeledParametersCallAllowed(PsiBuilder b, int l) {
    return b.getUserData(PARSING_COMMAND_ASSIGNMENT_STATEMENT) != Boolean.TRUE
//            && b.getUserData(PARSING_TELL_SIMPLE_STATEMENT) != Boolean.TRUE
            && b.getUserData(PARSING_TELL_SIMPLE_OBJECT_REF) != Boolean.TRUE
            && b.getUserData(PARSING_COMMAND_HANDLER_CALL_PARAMETERS) != Boolean.TRUE;
  }