Antlr3.Tool.Grammar.GetLexerGrammar C# (CSharp) Method

GetLexerGrammar() public method

public GetLexerGrammar ( ) : string
return string
        public virtual string GetLexerGrammar()
        {
            if ( LexerGrammarTemplate.GetAttribute( "literals" ) == null &&
                 LexerGrammarTemplate.GetAttribute( "rules" ) == null )
            {
                // if no rules, return nothing
                return null;
            }
            LexerGrammarTemplate.SetAttribute( "name", name );

            IDictionary<string, object> lexerActions;
            Actions.TryGetValue("lexer", out lexerActions);
            // if there are any actions set for lexer, pass them in
            if ( lexerActions != null )
            {
                LexerGrammarTemplate.SetAttribute( "actionNames",
                                            lexerActions.Keys );
                LexerGrammarTemplate.SetAttribute( "actions",
                                            lexerActions.Values );
            }
            // make sure generated grammar has the same options
            if ( options != null )
            {
                foreach ( var option in options )
                {
                    string optionName = option.Key;
                    if ( !doNotCopyOptionsToLexer.Contains( optionName ) )
                    {
                        object value = option.Value;
                        LexerGrammarTemplate.SetAttribute( "options.{name,value}", optionName, value );
                    }
                }
            }
            return LexerGrammarTemplate.Render();
        }

Usage Example

Beispiel #1
0
        public void TestArgsOnTokenInLexerRuleOfCombined()
        {
            ErrorQueue equeue = new ErrorQueue();
            ErrorManager.SetErrorListener( equeue );
            Grammar g = new Grammar(
                "grammar t;\n" +
                "a : R;\n" +
                "R : 'z' ID[32] ;\n" +
                "ID : 'a';\n" );

            string lexerGrammarStr = g.GetLexerGrammar();
            System.IO.StringReader sr = new System.IO.StringReader( lexerGrammarStr );
            Grammar lexerGrammar = new Grammar();
            lexerGrammar.FileName = "<internally-generated-lexer>";
            lexerGrammar.ImportTokenVocabulary( g );
            lexerGrammar.ParseAndBuildAST( sr );
            lexerGrammar.DefineGrammarSymbols();
            lexerGrammar.CheckNameSpaceAndActions();
            sr.Close();

            AntlrTool antlr = newTool();
            antlr.SetOutputDirectory( null ); // write to /dev/null
            CodeGenerator generator = new CodeGenerator( antlr, lexerGrammar, "Java" );
            lexerGrammar.CodeGenerator = generator;
            generator.GenRecognizer();

            int expectedMsgID = ErrorManager.MSG_RULE_HAS_NO_ARGS;
            object expectedArg = "ID";
            object expectedArg2 = null;
            GrammarSemanticsMessage expectedMessage =
                new GrammarSemanticsMessage( expectedMsgID, lexerGrammar, null, expectedArg, expectedArg2 );
            checkError( equeue, expectedMessage );
        }
All Usage Examples Of Antlr3.Tool.Grammar::GetLexerGrammar
Grammar