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

The following are Jave code examples for showing how to use getTokenText() 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
private static boolean parseCommandParameterSelector(PsiBuilder b, int l, AppleScriptCommand command,
                                                     StringHolder parsedParameterSelector) {
  if (!recursion_guard_(b, l, "parseCommandParameterSelector")) return false;
  boolean r = false;
  PsiBuilder.Marker m = enter_section_(b, l, _NONE_, "<parse Command Parameter Selector>");//todo check this _AND_
  parsedParameterSelector.value = b.getTokenText() == null ? "" : b.getTokenText();
  while (!b.eof() && b.getTokenType() != NLS && b.getTokenType() != COMMENT) {
    b.advanceLexer();
    if (command.getParameterByName(parsedParameterSelector.value) != null) {
      r = true;
      break;
    }
    parsedParameterSelector.value += " " + b.getTokenText();
  }
  exit_section_(b, l, m, COMMAND_PARAMETER_SELECTOR, r, false, null);
  return r;
}
 
Example 2
Project: AppleScript-IDEA   File: AppleScriptGeneratedParserUtil.java   Source Code and License Vote up 6 votes
private static boolean parseStdLibCommandName(PsiBuilder b, int l, StringHolder parsedName) {
  if (!recursion_guard_(b, l, "parseStdLibCommandName")) return false;
  boolean r = false;
  parsedName.value = "";
  parsedName.value = b.getTokenText() == null ? "" : b.getTokenText();
  PsiBuilder.Marker m = enter_section_(b);
  boolean commandWithPrefixExists = ParsableScriptSuiteRegistryHelper.isStdCommandWithPrefixExist(parsedName.value);
  String nextTokenText = parsedName.value;
  while (b.getTokenText() != null && commandWithPrefixExists) {
    b.advanceLexer(); //advance lexer in any case
    nextTokenText += " " + b.getTokenText();
    commandWithPrefixExists = ParsableScriptSuiteRegistryHelper.isStdCommandWithPrefixExist(nextTokenText);
    if (commandWithPrefixExists) {
      parsedName.value = nextTokenText;
    } else if (ParsableScriptSuiteRegistryHelper.isStdCommand(parsedName.value)) {
      r = true;
      break;
    }
  }
  exit_section_(b, m, null, r);
  return r;
}
 
Example 3
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 4
Project: AppleScript-IDEA   File: AppleScriptGeneratedParserUtil.java   Source Code and License Vote up 6 votes
/**
 * @param b {@link PsiBuilder}
 * @param l level deep
 * @return true if parsed token(s) is the property of the scripting additions library
 */
private static boolean tryToParseStdProperty(PsiBuilder b, int l) {
  if (!recursion_guard_(b, l, "tryToParseStdProperty")) return false;
  boolean r = false;
  PsiBuilder.Marker m = enter_section_(b);
  StringHolder currentTokenText = new StringHolder();
  currentTokenText.value = b.getTokenText() == null ? "" : b.getTokenText();
  boolean propertyWithPrefixExists = ParsableScriptSuiteRegistryHelper
          .isStdPropertyWithPrefixExist(currentTokenText.value);
  String nextTokenText = currentTokenText.value;
  while (b.getTokenText() != null && propertyWithPrefixExists) {
    b.advanceLexer(); //advance lexer in any case
    nextTokenText += " " + b.getTokenText();
    propertyWithPrefixExists = ParsableScriptSuiteRegistryHelper.isStdPropertyWithPrefixExist(nextTokenText);
    if (propertyWithPrefixExists) {
      currentTokenText.value = nextTokenText;
    } else if (ParsableScriptSuiteRegistryHelper.isStdProperty(currentTokenText.value)) {
      r = true;
      break;
    }
  }
  exit_section_(b, m, null, r);
  return r;
}
 
Example 5
Project: AppleScript-IDEA   File: AppleScriptGeneratedParserUtil.java   Source Code and License Vote up 6 votes
/**
 * @param b               {@link PsiBuilder}
 * @param l               level deep
 * @param applicationName name of the application
 * @return true if parsed token(s) is the property of the specified application
 */
private static boolean tryToParseApplicationProperty(PsiBuilder b, int l, @NotNull String applicationName) {
  if (!recursion_guard_(b, l, "tryToParseApplicationProperty")) return false;
  boolean r = false;
  PsiBuilder.Marker m = enter_section_(b);
  StringHolder currentTokenText = new StringHolder();
  currentTokenText.value = b.getTokenText() == null ? "" : b.getTokenText();
  boolean propertyWithPrefixExist = ParsableScriptSuiteRegistryHelper.isPropertyWithPrefixExist(applicationName,
          currentTokenText.value);
  //find the longest lexeme
  String nextTokenText = currentTokenText.value;
  while (b.getTokenText() != null && propertyWithPrefixExist) {
    b.advanceLexer(); //advance lexer in any case
    nextTokenText += " " + b.getTokenText();
    propertyWithPrefixExist = ParsableScriptSuiteRegistryHelper.isPropertyWithPrefixExist(applicationName,
            nextTokenText);
    if (propertyWithPrefixExist) {
      currentTokenText.value = nextTokenText;
    } else if (ParsableScriptSuiteRegistryHelper.isApplicationProperty(applicationName, currentTokenText.value)) {
      r = true;
      break;
    }
  }
  exit_section_(b, m, null, r);
  return r;
}
 
Example 6
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 7
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 8
Project: AppleScript-IDEA   File: AppleScriptGeneratedParserUtil.java   Source Code and License Vote up 5 votes
/**
 * @param b                           {@link PsiBuilder}
 * @param l                           Level deep
 * @param tellStatementStartCondition If this is the application reference of a &lt;tell&gt; or &lt;using terms
 *                                    from&gt; statements
 * @return true if parsed
 */
public static boolean parseApplicationName(PsiBuilder b, int l, Parser tellStatementStartCondition) {
  if (!recursion_guard_(b, l, "parseApplicationName")) return false;
  boolean r;
  consumeToken(b, THE_KW);
  if (!nextTokenIs(b, "", APPLICATION, APP)) return false;

  PsiBuilder.Marker mCls = enter_section_(b, l, _NONE_, "<parse application name>");
  r = consumeToken(b, APPLICATION);
  if (!r) r = consumeToken(b, APP);
  exit_section_(b, l, mCls, DICTIONARY_CLASS_NAME, r, false, null);

  if (!nextTokenIs(b, "", STRING_LITERAL, ID)) return false;
  PsiBuilder.Marker mProp = enter_section_(b, l, _NONE_, "<parse application name>");
  boolean idReference = consumeToken(b, ID);
  exit_section_(b, l, mProp, DICTIONARY_PROPERTY_NAME, idReference, false, null);
  PsiBuilder.Marker m = enter_section_(b);
  String applicationNameString = b.getTokenText();
  r = consumeToken(b, STRING_LITERAL);

  // if this is start of <tell compound> or <tell simple> or <using terms from> statements, push the application name
  // which dictionary will be consulted for terms parsing (only the last pushed application is queried)
  if (r && applicationNameString != null) {
    applicationNameString = applicationNameString.replace("\"", "");
    if (!StringUtil.isEmptyOrSpaces(applicationNameString) && tellStatementStartCondition.parse(b, l + 1)) {
      pushTargetApplicationName(b, applicationNameString);
    }
  }
  exit_section_(b, m, null, r);
  return r;
}
 
Example 9
Project: AppleScript-IDEA   File: AppleScriptGeneratedParserUtil.java   Source Code and License Vote up 5 votes
private static boolean tryToParseStdConstant(PsiBuilder b, int l) {
  if (!recursion_guard_(b, l, "tryToParseStdConstant")) return false;
  StringHolder currentTokenText = new StringHolder();
  currentTokenText.value = b.getTokenText() == null ? "" : b.getTokenText();
  boolean r = false, propertyOrClassExists = false, constantWithPrefixExists = ParsableScriptSuiteRegistryHelper
          .isStdConstantWithPrefixExist(currentTokenText.value);
  String nextTokenText = currentTokenText.value;
  PsiBuilder.Marker m = enter_section_(b);
  while (b.getTokenText() != null && constantWithPrefixExists) {
    b.advanceLexer();
    nextTokenText += " " + b.getTokenText();
    constantWithPrefixExists = ParsableScriptSuiteRegistryHelper.isStdConstantWithPrefixExist(nextTokenText);
    if (constantWithPrefixExists) {
      currentTokenText.value = nextTokenText;
    } else if (ParsableScriptSuiteRegistryHelper.isStdConstant(currentTokenText.value)) {
      r = true;
      break;
    }
  }
  if (r) {
    // grammar allows className and propertyName as primaryExpression, so we should match the longest token between
    // className or propertyName tokens. We check and return false if the property or class with the longer name
    // exists, as it will be parsed later
    currentTokenText.value += " " + b.getTokenText();
    propertyOrClassExists = ParsableScriptSuiteRegistryHelper
            .isStdPropertyWithPrefixExist(currentTokenText.value) ||
            ParsableScriptSuiteRegistryHelper
                    .isStdClassWithPrefixExist(currentTokenText.value);
  }
  r = r && !propertyOrClassExists;
  exit_section_(b, m, null, r);
  return r;
}
 
Example 10
Project: AppleScript-IDEA   File: AppleScriptGeneratedParserUtil.java   Source Code and License Vote up 5 votes
private static boolean tryToParseApplicationConstant(PsiBuilder b, int l, @NotNull String applicationName) {
  if (!recursion_guard_(b, l, "tryToParseApplicationConstant")) return false;
  StringHolder currentTokenText = new StringHolder();
  currentTokenText.value = b.getTokenText() == null ? "" : b.getTokenText();
  boolean r = false, propertyOrClassExists = false, constantWithPrefixExists = ParsableScriptSuiteRegistryHelper
          .isConstantWithPrefixExist(applicationName, currentTokenText.value);
  String nextTokenText = currentTokenText.value;
  PsiBuilder.Marker m = enter_section_(b);
  while (b.getTokenText() != null && constantWithPrefixExists) {
    b.advanceLexer();
    nextTokenText += " " + b.getTokenText();
    constantWithPrefixExists = ParsableScriptSuiteRegistryHelper
            .isConstantWithPrefixExist(applicationName, nextTokenText);
    if (constantWithPrefixExists) {
      currentTokenText.value = nextTokenText;
    } else if (ParsableScriptSuiteRegistryHelper.isApplicationConstant(applicationName, currentTokenText.value)) {
      r = true;
      break;
    }
  }
  if (r) {
    // grammar allows className and propertyName as primaryExpression, so we should match the longest token between
    // className or propertyName tokens. We check and return false if the property or class with the longer name
    // exists, as it will be parsed later
    propertyOrClassExists = ParsableScriptSuiteRegistryHelper
            .isPropertyWithPrefixExist(applicationName, currentTokenText.value) ||
            ParsableScriptSuiteRegistryHelper
                    .isClassWithPrefixExist(applicationName, currentTokenText.value);
    if (propertyOrClassExists) {
      currentTokenText.value += " " + b.getTokenText();
      propertyOrClassExists = ParsableScriptSuiteRegistryHelper
              .isPropertyWithPrefixExist(applicationName, currentTokenText.value) ||
              ParsableScriptSuiteRegistryHelper
                      .isClassWithPrefixExist(applicationName, currentTokenText.value);
    }
  }
  r = r && !propertyOrClassExists;
  exit_section_(b, m, null, r);
  return r;
}
 
Example 11
Project: AppleScript-IDEA   File: AppleScriptGeneratedParserUtil.java   Source Code and License Vote up 4 votes
/**
   * @param b               {@link PsiBuilder}
   * @param l               level deep
   * @param parsedName      holder for parsed term string
   * @param applicationName name of the application, which dictionary terms will be queried
   * @param checkStdLib     whether to check in scripting additions library if no terms were found in this application
   * @return true if command name was parsed
   */
  private static boolean parseCommandNameForApplication(PsiBuilder b, int l,
                                                        StringHolder parsedName,
                                                        @NotNull String applicationName,
                                                        boolean checkStdLib) {
    if (!recursion_guard_(b, l, "parseCommandNameForApplication")) return false;
    boolean r = false;
    parsedName.value = "";
    PsiBuilder.Marker m = enter_section_(b);
    parsedName.value = b.getTokenText() == null ? "" : b.getTokenText();

    boolean commandWithPrefixExists = ParsableScriptSuiteRegistryHelper.isCommandWithPrefixExist(applicationName,
            parsedName.value);
    String nextTokenText = parsedName.value;
    while (b.getTokenText() != null && commandWithPrefixExists) {
      b.advanceLexer(); //advance lexer to the next token
      nextTokenText += " " + b.getTokenText();
      commandWithPrefixExists = ParsableScriptSuiteRegistryHelper
              .isCommandWithPrefixExist(applicationName, nextTokenText);
      if (commandWithPrefixExists) {
        //if command with prefix exists, append token text
        parsedName.value = nextTokenText;
      } else if (ParsableScriptSuiteRegistryHelper.isApplicationCommand(applicationName, parsedName.value)) {
        //if there is std command with longer prefix exists do not parse it here
        r = !checkStdLib || !ParsableScriptSuiteRegistryHelper.isStdCommandWithPrefixExist(nextTokenText);
        // if there is class name with longer prefix exists !! AND !! next is NLS token => do not this as command name
//        boolean longerClassNameExists = ParsableScriptSuiteRegistryHelper
//                .isClassWithPrefixExist(applicationName, nextTokenText);
//        if (r && longerClassNameExists) {
//          while (b.getTokenType() != NLS) {
//            b.advanceLexer();
//            nextTokenText += " " + b.getTokenText();
//          }
//          r = ParsableScriptSuiteRegistryHelper.isApplicationClass(applicationName, nextTokenText);
//        }
        r = r && !ParsableScriptSuiteRegistryHelper.isClassWithPrefixExist(applicationName, nextTokenText);
        break;
      }
    }
    exit_section_(b, m, null, r);
    return r;
  }
 
Example 12
Project: AppleScript-IDEA   File: AppleScriptGeneratedParserUtil.java   Source Code and License Vote up 4 votes
/**
 * @param b                        {@link PsiBuilder}
 * @param l                        Level deep
 * @param currentTokenText         Text string of the token being parsed
 * @param isPluralForm             Whether we are parsing class name in plural form
 * @param toldApplicationName      The name of the application (which is the target of all messages in current block)
 *                                 dictionary of which will be queried first of all when searching for class names
 * @param areThereUseStatements    Whether there are use statements used in the script, in which case standard library
 *                                 Should be specified explicitly
 * @param applicationsToImportFrom Set list of the applications (if specified) attached to script from use statements
 * @return true if class name parsed
 */
private static boolean parseDictionaryClassName(PsiBuilder b, int l, StringHolder currentTokenText,
                                                final boolean isPluralForm,
                                                @NotNull String toldApplicationName,
                                                final boolean areThereUseStatements,
                                                final @Nullable Set<String> applicationsToImportFrom) {
  boolean r, propertyExists = false;
  PsiBuilder.Marker m = enter_section_(b);
  r = parseApplicationClassName(b, l + 1, currentTokenText, isPluralForm, toldApplicationName);
  if (r) {
    // grammar allows className and propertyName as primaryExpression, so we should match the longest token between
    // className or propertyName tokens. Here we check and return false if the property with the longer name exists,
    // as it will be parsed later
    currentTokenText.value += " " + b.getTokenText();
    propertyExists = ParsableScriptSuiteRegistryHelper
            .isPropertyWithPrefixExist(toldApplicationName, currentTokenText.value);
  }
  exit_section_(b, m, null, r && !propertyExists);
  if (propertyExists) return false;
  if (r) return true;
  if (areThereUseStatements) {
    // if there are <use> statements in script (with importing dictionary terms condition), checking dictionary for
    // each application
    if (applicationsToImportFrom != null && !applicationsToImportFrom.isEmpty()) {
      for (String appName : applicationsToImportFrom) {
        m = enter_section_(b);
        r = parseApplicationClassName(b, l + 1, currentTokenText, isPluralForm, appName);
        if (r) {
          // check here as well for the property with longer name
          currentTokenText.value += " " + b.getTokenText();
          propertyExists = ParsableScriptSuiteRegistryHelper
                  .isPropertyWithPrefixExist(appName, currentTokenText.value);
        }
        exit_section_(b, m, null, r && !propertyExists);
        if (propertyExists) return false;
        if (r) return true;
      }
    }
  } else {
    m = enter_section_(b);
    r = parseStdClassName(b, l + 1, currentTokenText, isPluralForm);
    exit_section_(b, m, null, r);
    if (r) return true;
  }
  // It looks like not always CocoaStandard classes(and other terms) could be presented in applications terminology,
  // so in this case we check CocoaStandard here
  m = enter_section_(b);
  r = parseApplicationClassName(b, l + 1, currentTokenText, isPluralForm,
          ApplicationDictionary.COCOA_STANDARD_LIBRARY);
  if (r) {
    // grammar allows className and propertyName as primaryExpression, so we should match the longest token between
    // className or propertyName tokens. Here we check and return false if the property with the longer name exists,
    // as it will be parsed later
    currentTokenText.value += " " + b.getTokenText();
    propertyExists = ParsableScriptSuiteRegistryHelper
            .isPropertyWithPrefixExist(toldApplicationName, currentTokenText.value);
  }
  exit_section_(b, m, null, r && !propertyExists);
  return !propertyExists && r;
}