Package org.antlr.v4.tool

Examples of org.antlr.v4.tool.Grammar


    that all nodes in tree referred to this grammar. Later, we will
    use it for error handling and generally knowing from where a rule
    comes from.
   */
  public Grammar createGrammar(GrammarRootAST ast) {
    final Grammar g;
    if ( ast.grammarType==ANTLRParser.LEXER ) g = new LexerGrammar(this, ast);
    else g = new Grammar(this, ast);

    // ensure each node has pointer to surrounding grammar
    GrammarTransformPipeline.setGrammarPtr(g, ast);
    return g;
  }
View Full Code Here


   *  grammar created while processing a combined grammar, use
   *  getImplicitLexer() on returned grammar.
   */
  public Grammar loadGrammar(String fileName) {
    GrammarRootAST grammarRootAST = parseGrammar(fileName);
    final Grammar g = createGrammar(grammarRootAST);
    g.fileName = fileName;
    process(g, false);
    return g;
  }
View Full Code Here

   * @param g
   * @param nameNode The node associated with the imported grammar name.
   */
  public Grammar loadImportedGrammar(Grammar g, GrammarAST nameNode) throws IOException {
    String name = nameNode.getText();
    Grammar imported = importedGrammars.get(name);
    if (imported == null) {
      g.tool.log("grammar", "load " + name + " from " + g.fileName);
      File importedFile = null;
      for (String extension : ALL_GRAMMAR_EXTENSIONS) {
        importedFile = getImportedGrammarFile(g, name + extension);
View Full Code Here

      if ( r!=null ) r.isStartRule = false;
    }
  }

  void assignLexerTokenTypes(Grammar g, List<GrammarAST> tokensDefs) {
    Grammar G = g.getOutermostGrammar(); // put in root, even if imported
    for (GrammarAST def : tokensDefs) {
      // tokens { id (',' id)* } so must check IDs not TOKEN_REF
      if ( Grammar.isTokenName(def.getText()) ) {
        G.defineTokenName(def.getText());
      }
    }

    /* Define token types for nonfragment rules which do not include a 'type(...)'
     * or 'more' lexer command.
     */
    for (Rule r : g.rules.values()) {
      if ( !r.isFragment() && !hasTypeOrMoreCommand(r) ) {
        G.defineTokenName(r.name);
      }
    }

    // FOR ALL X : 'xxx'; RULES, DEFINE 'xxx' AS TYPE X
    List<Pair<GrammarAST,GrammarAST>> litAliases =
      Grammar.getStringLiteralAliasesFromLexerRules(g.ast);
    Set<String> conflictingLiterals = new HashSet<String>();
    if ( litAliases!=null ) {
      for (Pair<GrammarAST,GrammarAST> pair : litAliases) {
        GrammarAST nameAST = pair.a;
        GrammarAST litAST = pair.b;
        if ( !G.stringLiteralToTypeMap.containsKey(litAST.getText()) ) {
          G.defineTokenAlias(nameAST.getText(), litAST.getText());
        }
        else {
          // oops two literal defs in two rules (within or across modes).
          conflictingLiterals.add(litAST.getText());
        }
View Full Code Here

   * @param g The grammar.
   * @param channelDefs A collection of AST nodes defining individual channels
   * within a {@code channels{}} block in the grammar.
   */
  void assignChannelTypes(Grammar g, List<GrammarAST> channelDefs) {
    Grammar outermost = g.getOutermostGrammar();
    for (GrammarAST channel : channelDefs) {
      String channelName = channel.getText();

      // Channel names can't alias tokens or modes, because constant
      // values are also assigned to them and the ->channel(NAME) lexer
      // command does not distinguish between the various ways a constant
      // can be declared. This method does not verify that channels do not
      // alias rules, because rule names are not associated with constant
      // values in ANTLR grammar semantics.

      if (g.getTokenType(channelName) != Token.INVALID_TYPE) {
        g.tool.errMgr.grammarError(ErrorType.CHANNEL_CONFLICTS_WITH_TOKEN, g.fileName, channel.token, channelName);
      }

      if (outermost instanceof LexerGrammar) {
        LexerGrammar lexerGrammar = (LexerGrammar)outermost;
        if (lexerGrammar.modes.containsKey(channelName)) {
          g.tool.errMgr.grammarError(ErrorType.CHANNEL_CONFLICTS_WITH_MODE, g.fileName, channel.token, channelName);
        }
      }

      outermost.defineChannelName(channel.getText());
    }
  }
View Full Code Here

  /** Build a file with a parser containing rule functions. Use the
   *  controller as factory in SourceGenTriggers so it triggers codegen
   *  extensions too, not just the factory functions in this factory.
   */
  public OutputModelObject buildParserOutputModel() {
    Grammar g = delegate.getGrammar();
    CodeGenerator gen = delegate.getGenerator();
    ParserFile file = parserFile(gen.getRecognizerFileName());
    setRoot(file);
    Parser parser = parser(file);
    file.parser = parser;
View Full Code Here

    CodeGenerator gen = delegate.getGenerator();
    LexerFile file = lexerFile(gen.getRecognizerFileName());
    setRoot(file);
    file.lexer = lexer(file);

    Grammar g = delegate.getGrammar();
    for (Rule r : g.rules.values()) {
      buildLexerRuleActions(file.lexer, r);
    }

    return file;
View Full Code Here

    }
    else {
      buildNormalRuleFunction(r, function);
    }

    Grammar g = getGrammar();
    for (ActionAST a : r.actions) {
      if ( a instanceof PredAST ) {
        PredAST p = (PredAST)a;
        RuleSempredFunction rsf = parser.sempredFuncs.get(r);
        if ( rsf==null ) {
View Full Code Here

    if (r.actions.isEmpty()) {
      return;
    }

    CodeGenerator gen = delegate.getGenerator();
    Grammar g = delegate.getGrammar();
    String ctxType = gen.getTarget().getRuleFunctionContextStructName(r);
    RuleActionFunction raf = lexer.actionFuncs.get(r);
    if ( raf==null ) {
      raf = new RuleActionFunction(delegate, r, ctxType);
    }
View Full Code Here

import static org.junit.Assert.assertTrue;

public class TestATNConstruction extends BaseTest {
  @Test
  public void testA() throws Exception {
    Grammar g = new Grammar(
      "parser grammar P;\n"+
      "a : A;");
    String expecting =
      "RuleStart_a_0->s2\n" +
      "s2-A->s3\n" +
View Full Code Here

TOP

Related Classes of org.antlr.v4.tool.Grammar

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.