Пример #1
0
 public override void EnterId([NotNull] ANTLRv4Parser.IdContext context)
 {
     if (context.Parent is ANTLRv4Parser.ModeSpecContext)
     {
         TerminalNodeImpl term = context.GetChild(0) as TerminalNodeImpl;
         string           id   = term.GetText();
         ISymbol          sym  = new ModeSymbol(id, term.Symbol);
         _pd.RootScope.define(ref sym);
         CombinedScopeSymbol s = (CombinedScopeSymbol)sym;
         _pd.Attributes[context] = new List <CombinedScopeSymbol>()
         {
             s
         };
         _pd.Attributes[context.GetChild(0)] = new List <CombinedScopeSymbol>()
         {
             s
         };
     }
     else if (context.Parent is ANTLRv4Parser.IdListContext && context.Parent?.Parent is ANTLRv4Parser.ChannelsSpecContext)
     {
         TerminalNodeImpl term = context.GetChild(0) as TerminalNodeImpl;
         string           id   = term.GetText();
         ISymbol          sym  = new ChannelSymbol(id, term.Symbol);
         _pd.RootScope.define(ref sym);
         CombinedScopeSymbol s = (CombinedScopeSymbol)sym;
         _pd.Attributes[context] = new List <CombinedScopeSymbol>()
         {
             s
         };
         _pd.Attributes[term] = new List <CombinedScopeSymbol>()
         {
             s
         };
     }
 }
Пример #2
0
    private double GetDoubleValue(TerminalNodeImpl t)
    {
        if (t.Symbol.Type == VARIABLE)
        {
            return(Convert.ToDouble(ENVIRONMENT[t.ToString()]));
        }

        return(double.Parse(t.GetText()));
    }
Пример #3
0
        private object InterpretTerminalNodeImpl(IParseTree node)
        {
            TerminalNodeImpl term = (TerminalNodeImpl)node;

            if (term.Symbol.Type == JsonLTParser.STRING)
            {
                StringBuilder text = new StringBuilder(term.GetText());
                text.Remove(0, 1);
                text.Remove(text.Length - 1, 1);
                text.Replace("\\t", "\t");
                text.Replace("\\r", "\r");
                text.Replace("\\n", "\n");
                text.Replace("\\\\", "\\");
                text.Replace("\\", "");
                return(text.ToString());
            }
            if (term.Symbol.Type == JsonLTParser.NUMBER)
            {
                string text = term.GetText();
                if (text.Contains(".") || text.Contains("E") || text.Contains("e"))
                {
                    return(Convert.ToDouble(text));
                }
                else
                {
                    return(Convert.ToInt64(text));
                }
            }
            if (term.Symbol.Type == JsonLTParser.TRUE)
            {
                return(true);
            }
            if (term.Symbol.Type == JsonLTParser.FALSE)
            {
                return(false);
            }
            if (term.Symbol.Type == JsonLTParser.NULL)
            {
                return(null);
            }
            return(null);
        }
Пример #4
0
 /// <summary>
 /// Constructs the AST recursively from the ANTLR Java Parser classes.
 /// </summary>
 /// <param name="ctx"></param>
 /// <returns></returns>
 public static JavaAST ConstructAST(TerminalNodeImpl ctx)
 {
     if (literals.Contains(ctx.Symbol.Type))
     {
         return(JavaAST.CreateToken(ctx.Symbol.Type, ctx.GetText()));
     }
     else
     {
         return(JavaAST.CreateToken(ctx.Symbol.Type));
     }
 }
Пример #5
0
    private string GetTerminalString(TerminalNodeImpl t)
    {
        if (t.Symbol.Type == VARIABLE)
        {
            return(ENVIRONMENT[t.ToString()].ToString());
        }

        if (t.Symbol.Type == NUMBER)
        {
            return(t.GetText());
        }

        throw new ChildLangRuntimeException();
    }
Пример #6
0
 public override void EnterId([NotNull] ANTLRv4Parser.IdContext context)
 {
     if (context.Parent is ANTLRv4Parser.LexerCommandExprContext && context.Parent.Parent is ANTLRv4Parser.LexerCommandContext)
     {
         ANTLRv4Parser.LexerCommandContext lc = context.Parent.Parent as ANTLRv4Parser.LexerCommandContext;
         if (lc.GetChild(0)?.GetChild(0)?.GetText() == "pushMode")
         {
             TerminalNodeImpl term     = context.GetChild(0) as TerminalNodeImpl;
             string           id       = term.GetText();
             IList <ISymbol>  sym_list = _pd.RootScope.LookupType(id);
             if (!sym_list.Any())
             {
                 ISymbol sym = new ModeSymbol(id, null);
                 _pd.RootScope.define(ref sym);
             }
             List <CombinedScopeSymbol> ref_list = new List <CombinedScopeSymbol>();
             foreach (ISymbol sym in sym_list)
             {
                 CombinedScopeSymbol s = new RefSymbol(term.Symbol, sym);
                 ref_list.Add(s);
             }
             _pd.Attributes[context]             = ref_list;
             _pd.Attributes[context.GetChild(0)] = ref_list;
         }
         else if (lc.GetChild(0)?.GetChild(0)?.GetText() == "channel")
         {
             TerminalNodeImpl term     = context.GetChild(0) as TerminalNodeImpl;
             string           id       = term.GetText();
             IList <ISymbol>  sym_list = _pd.RootScope.LookupType(id);
             if (!sym_list.Any())
             {
                 ISymbol sym = new ChannelSymbol(id, null);
                 _pd.RootScope.define(ref sym);
             }
             List <CombinedScopeSymbol> ref_list = new List <CombinedScopeSymbol>();
             foreach (ISymbol sym in sym_list)
             {
                 CombinedScopeSymbol s = new RefSymbol(term.Symbol, sym);
                 ref_list.Add(s);
             }
             _pd.Attributes[context]             = ref_list;
             _pd.Attributes[context.GetChild(0)] = ref_list;
         }
     }
 }
Пример #7
0
        public override void EnterLexerRuleSpec([NotNull] ANTLRv4Parser.LexerRuleSpecContext context)
        {
            int i;

            for (i = 0; i < context.ChildCount; ++i)
            {
                if (!(context.GetChild(i) is TerminalNodeImpl))
                {
                    continue;
                }

                TerminalNodeImpl c = context.GetChild(i) as TerminalNodeImpl;
                if (c.Symbol.Type == ANTLRv4Lexer.TOKEN_REF)
                {
                    break;
                }
            }
            if (i == context.ChildCount)
            {
                return;
            }

            TerminalNodeImpl token_ref = context.GetChild(i) as TerminalNodeImpl;
            string           id        = token_ref.GetText();
            ISymbol          sym       = new TerminalSymbol(id, token_ref.Symbol);

            _pd.RootScope.define(ref sym);
            CombinedScopeSymbol s = (CombinedScopeSymbol)sym;

            _pd.Attributes[context] = new List <CombinedScopeSymbol>()
            {
                s
            };
            _pd.Attributes[context.GetChild(i)] = new List <CombinedScopeSymbol>()
            {
                s
            };
        }
Пример #8
0
        public override void EnterTerminal([NotNull] ANTLRv4Parser.TerminalContext context)
        {
            TerminalNodeImpl first = context.GetChild(0) as TerminalNodeImpl;

            if (first.Symbol.Type == ANTLRv4Parser.TOKEN_REF)
            {
                string          id   = first.GetText();
                IList <ISymbol> list = _pd.RootScope.LookupType(id);
                if (!list.Any())
                {
                    ISymbol sym = new TerminalSymbol(id, first.Symbol);
                    _pd.RootScope.define(ref sym);
                }
                List <CombinedScopeSymbol> new_attrs = new List <CombinedScopeSymbol>();
                foreach (ISymbol sym in list)
                {
                    CombinedScopeSymbol s = new RefSymbol(first.Symbol, sym);
                    new_attrs.Add(s);
                }
                _pd.Attributes[context]             = new_attrs;
                _pd.Attributes[context.GetChild(0)] = new_attrs;
            }
        }
Пример #9
0
        private static void HandleTerminalNode(TerminalNodeImpl termNode, LevelInformationStack stackInfo)
        {
            //the terminal node itself isn't a ruleContext object, so there is some trickery that happens below
            var termParentCheck = termNode.Parent as RuleContext;

            if (termParentCheck != null)
            {
                stackInfo.LevelInfo.Add(termParentCheck.Depth() + 1, new LevelInformation());
            }
            else
            {
                Console.WriteLine("well, what is it then?");  //debug
            }
            //if this termNode is just a path step, ignore it, it'll get solved
            if (termNode.GetText() == "/")
            {
                return;
            }

            #region check Parent node to know what to do with current line

            //path traversal nodes are the node parents that directly (meh, kind of) sire terminal nodes that correspond to a transition in the path
            //the only thing to do is add the current path to the current depth
            if (PathTraverseNodes.Contains(BranchingParentName(termNode)))
            {
                stackInfo.LevelInfo[((RuleContext)termNode.Parent).Depth() + 1].CurrentPartialPath = termNode.GetText();

                stackInfo.LevelInfo[((RuleContext)termNode.Parent).Depth() + 1].ConvertedCode = termNode.GetText() + ".";
            }

            //predicate nodes are parent nodes that directly (really directly) sire sub query filtering code
            //have to create the beginning filtering code and set the partialPath, and this partialPath should be a new levelInfo so as not to corrupt the main stack
            else if (PredicateNodes.Contains(BranchingParentName(termNode)))
            {
                //'[' chars are known points of entry for 'Where(' clause
                if (termNode.GetText() == "[")
                {
                    stackInfo.LevelInfo[((RuleContext)termNode.Parent).Depth() + 1].ConvertedCode      = "Where(c" + (((RuleContext)termNode.Parent).Depth() + 1) + "=>";
                    stackInfo.LevelInfo[((RuleContext)termNode.Parent).Depth() + 1].CurrentPartialPath = "c" + (((RuleContext)termNode.Parent).Depth() + 1);
                }
                //when we hit the end of the predicate portion, box it off with a ')' instead of the ']'
                else if (termNode.GetText() == "]")
                {
                    stackInfo.LevelInfo[((RuleContext)termNode.Parent).Depth() + 1].ConvertedCode = ")";
                }
                //any other chars that exist under a predicate node are just added
                //debug this should not be hit?
                else
                {
                    stackInfo.LevelInfo[((RuleContext)termNode.Parent).Depth() + 1].CurrentPartialPath = termNode.GetText();
                }
            }

            //parenthesized nodes are parent nodes that directly (again, meh, kind of) sire sub queries to be grouped together
            //they are similar in function to predicate nodes, but do not require inserting a custom code transformation like the 'Where(' clause for predicates
            else if (ParenthesizedNodes.Contains(BranchingParentName(termNode)))
            {
                //the end caps always get added to provide the subquery portion
                if (termNode.GetText() == "(" || termNode.GetText() == ")")
                {
                    stackInfo.LevelInfo[((RuleContext)termNode.Parent).Depth() + 1].ConvertedCode = termNode.GetText();
                }
                else
                {
                    //debug
                    Console.WriteLine("I expected different things");
                }
            }

            else if (LogicNodes.Contains(BranchingParentName(termNode)))
            {
                switch (BranchingParentName(termNode))
                {
                case nameof(XPath1W3Parser.AndExprContext):
                    stackInfo.LevelInfo[((RuleContext)termNode.Parent).Depth() + 1].ConvertedCode = " && ";
                    break;

                case nameof(XPath1W3Parser.OrExprContext):
                    stackInfo.LevelInfo[((RuleContext)termNode.Parent).Depth() + 1].ConvertedCode = " || ";
                    break;
                }
            }

            else if (BranchingParentName(termNode) == nameof(XPath1W3Parser.AbbrevForwardStepContext))
            {
                //the @ sign denotes a specific attribute to test
                //need to swap the @ for the current partial path and put it into the code
                //the partial path doesn't change because the next step
                if (termNode.GetText() == "@")
                {
                    stackInfo.LevelInfo[((RuleContext)termNode.Parent).Depth() + 1].ConvertedCode = stackInfo.RollUpPartialPath();
                }
                //if it's not a "@" then it has to be the name of the attribute we are testing
                else
                {
                    stackInfo.LevelInfo[((RuleContext)termNode.Parent).Depth() + 1].ConvertedCode = "." + termNode.GetText();
                }
            }

            else if (BranchingParentName(termNode) == nameof(XPath1W3Parser.EqualityExprContext))
            {
                //if we have an equality operator - add it straight away
                if (termNode.GetText() == "=" || termNode.GetText() == "!=")
                {
                    string s = termNode.GetText();
                    if (s == "=")
                    {
                        s += "=";           //gotta be == for c# or we might assign value at worst, at best it will fail compilation
                    }
                    stackInfo.LevelInfo[((RuleContext)termNode.Parent).Depth() + 1].ConvertedCode = s;
                }
                //else if the string starts with a string literal character and the node type is a primary expression, add it straight away as well
                else if (termNode.GetText()[0] == '\'' && termNode.Parent.GetType().Name == nameof(XPath1W3Parser.PrimaryExprContext))
                {
                    //strip the ' off and replace with ", but only on the frist and last chars because if they exist anywhere else, we need to retain them
                    //todo - does this also mean we need to look for \' characters and replace them with single 's?
                    var s = termNode.GetText().ToCharArray();
                    s[0]            = '"';
                    s[s.Length - 1] = '"';

                    stackInfo.LevelInfo[((RuleContext)termNode.Parent).Depth() + 1].ConvertedCode = new string(s);
                }
                else if (termNode.GetText() == "." && termNode.Parent.GetType().Name == nameof(XPath1W3Parser.ContextItemExprContext)) //this should
                {
                    string fullBackString = "";
                    int    index          = 0;
                    foreach (var lvl in stackInfo.LevelInfo.OrderByDescending(c => c.Key))
                    {
                        index++;

                        if (!string.IsNullOrWhiteSpace(lvl.Value.ConvertedCode))
                        {
                            fullBackString = lvl.Value.ConvertedCode;
                            break;
                        }
                    }

                    if (index != stackInfo.LevelInfo.Keys.Count())
                    {
                        Console.WriteLine("crap?");
                    }
                    else if (fullBackString.StartsWith("Where(c") && fullBackString.EndsWith("=>"))
                    {
                        //this should mean - there is only 1 level in the stackInfo that has convert code in one level, and it should be Where(c=> with c as the partial path
                        //this needs to be removed and reset to work on the parent element

                        stackInfo.LevelInfo[stackInfo.LevelInfo.Keys.ToList()[0]].ConvertedCode      = null;
                        stackInfo.LevelInfo[stackInfo.LevelInfo.Keys.ToList()[0]].CurrentPartialPath = stackInfo.CallingParentFullPath;
                    }

                    stackInfo.LevelInfo[((RuleContext)termNode.Parent).Depth() + 1].ConvertedCode += " " + stackInfo.RollUpPartialPath();
                }
                else
                {
                    //debug
                    Console.WriteLine("well what do we have here? prolly a value, like 5 or 11, or some other non-string literal, duh");
                }
            }

            //This feels like the same type of node as an EqualityExprContext, but it shouldn't ever have strings because string1>string2 doesn't make a lot of sense in XPath (or most other areas)
            //so, might as well make it a seperate chunk, just incase more differences are observed
            else if (BranchingParentName(termNode) == nameof(XPath1W3Parser.RelationalExprContext))
            {
                //if this is an operator, add it straight away
                if (termNode.GetText() == "<" || termNode.GetText() == ">" || termNode.GetText() == "<=" || termNode.GetText() == ">=")
                {
                    stackInfo.LevelInfo[((RuleContext)termNode.Parent).Depth() + 1].ConvertedCode = termNode.GetText();
                }
                else
                {
                    //debug
                    Console.WriteLine("I am really not sure if this is expected or not...why are we here future self?");
                }
            }

            else
            {
                //debug
                Console.WriteLine("Srsly, wtf are we doing here...someone didn't think that through enough");
            }
            #endregion
        }
Пример #10
0
        public void Parse(string plain_old_input_grammar, string ffn)
        {
            Text           = plain_old_input_grammar;
            full_file_name = ffn;

            // Set up Antlr to parse input grammar.
            byte[]            byteArray = Encoding.UTF8.GetBytes(plain_old_input_grammar);
            CommonTokenStream cts       = new CommonTokenStream(
                new ANTLRv4Lexer(
                    new AntlrInputStream(
                        new StreamReader(
                            new MemoryStream(byteArray)).ReadToEnd())));

            _ant_parser = new ANTLRv4Parser(cts);

            // Set up another token stream containing comments. This might be
            // problematic as the parser influences the lexer.
            CommonTokenStream cts_off_channel = new CommonTokenStream(
                new ANTLRv4Lexer(
                    new AntlrInputStream(
                        new StreamReader(
                            new MemoryStream(byteArray)).ReadToEnd())),
                ANTLRv4Lexer.OFF_CHANNEL);

            // Get all comments.
            _ant_comments = new List <IToken>();
            while (cts_off_channel.LA(1) != ANTLRv4Parser.Eof)
            {
                IToken token = cts_off_channel.LT(1);
                if (token.Type == ANTLRv4Parser.BLOCK_COMMENT ||
                    token.Type == ANTLRv4Parser.LINE_COMMENT)
                {
                    _ant_comments.Add(token);
                }
                cts_off_channel.Consume();
            }

            try
            {
                _ant_tree = _ant_parser.grammarSpec();
            }
            catch (Exception e)
            {
                // Parsing error.
            }

            _all_nodes = DFSVisitor.DFS(_ant_tree as ParserRuleContext).ToArray();

            {
                // Get all nonterminal names from the grammar.
                IEnumerable <IParseTree> nonterm_nodes_iterator = _all_nodes.Where((IParseTree n) =>
                {
                    TerminalNodeImpl nonterm = n as TerminalNodeImpl;
                    return(nonterm?.Symbol.Type == ANTLRv4Parser.RULE_REF);
                });
                _ant_nonterminals_names = nonterm_nodes_iterator.Select <IParseTree, string>(
                    (t) => (t as TerminalNodeImpl).Symbol.Text).ToArray();
            }

            {
                // Get all terminal names from the grammar.
                IEnumerable <IParseTree> term_nodes_iterator = _all_nodes.Where((IParseTree n) =>
                {
                    TerminalNodeImpl nonterm = n as TerminalNodeImpl;
                    return(nonterm?.Symbol.Type == ANTLRv4Parser.TOKEN_REF);
                });
                _ant_terminals_names = term_nodes_iterator.Select <IParseTree, string>(
                    (t) => (t as TerminalNodeImpl).Symbol.Text).ToArray();
            }

            {
                // Get all defining and applied occurences of nonterminal names in grammar.
                IEnumerable <IParseTree> nonterm_nodes_iterator = _all_nodes.Where((IParseTree n) =>
                {
                    TerminalNodeImpl nonterm = n as TerminalNodeImpl;
                    if (nonterm == null)
                    {
                        return(false);
                    }
                    if (!_ant_nonterminals_names.Contains(nonterm.GetText()))
                    {
                        return(false);
                    }
                    // The token must be part of parserRuleSpec context.
                    for (var p = nonterm.Parent; p != null; p = p.Parent)
                    {
                        if (p is ANTLRv4Parser.ParserRuleSpecContext)
                        {
                            return(true);
                        }
                    }
                    return(false);
                });
                _ant_nonterminals = nonterm_nodes_iterator.Select <IParseTree, IToken>(
                    (t) => (t as TerminalNodeImpl).Symbol).ToArray();
                // Get all defining and applied occurences of nonterminal names in grammar.
                var iterator = nonterm_nodes_iterator.Where((IParseTree n) =>
                {
                    TerminalNodeImpl term = n as TerminalNodeImpl;
                    if (term == null)
                    {
                        return(false);
                    }
                    IRuleNode parent = term.Parent;
                    for (int i = 0; i < parent.ChildCount; ++i)
                    {
                        if (parent.GetChild(i) == term &&
                            i + 1 < parent.ChildCount &&
                            parent.GetChild(i + 1).GetText() == ":")
                        {
                            return(true);
                        }
                    }
                    return(false);
                });
                _ant_nonterminals_defining = iterator.Select <IParseTree, IToken>(
                    (t) => (t as TerminalNodeImpl).Symbol).ToArray();
            }

            {
                // Get all defining and applied occurences of nonterminal names in grammar.
                IEnumerable <IParseTree> term_nodes_iterator = _all_nodes.Where((IParseTree n) =>
                {
                    TerminalNodeImpl term = n as TerminalNodeImpl;
                    if (term == null)
                    {
                        return(false);
                    }
                    if (!_ant_terminals_names.Contains(term.GetText()))
                    {
                        return(false);
                    }
                    // The token must be part of parserRuleSpec context.
                    for (var p = term.Parent; p != null; p = p.Parent)
                    {
                        if (p is ANTLRv4Parser.ParserRuleSpecContext ||
                            p is ANTLRv4Parser.LexerRuleSpecContext)
                        {
                            return(true);
                        }
                    }
                    return(false);
                });
                _ant_terminals = term_nodes_iterator.Select <IParseTree, IToken>(
                    (t) => (t as TerminalNodeImpl).Symbol).ToArray();
                // Get all defining nonterminal names in grammar.
                var iterator = term_nodes_iterator.Where((IParseTree n) =>
                {
                    TerminalNodeImpl term = n as TerminalNodeImpl;
                    if (term == null)
                    {
                        return(false);
                    }
                    IRuleNode parent = term.Parent;
                    for (int i = 0; i < parent.ChildCount; ++i)
                    {
                        if (parent.GetChild(i) == term &&
                            i + 1 < parent.ChildCount &&
                            parent.GetChild(i + 1).GetText() == ":")
                        {
                            return(true);
                        }
                    }
                    return(false);
                });
                _ant_terminals_defining = iterator.Select <IParseTree, IToken>(
                    (t) => (t as TerminalNodeImpl).Symbol).ToArray();
            }

            {
                // Get all keyword tokens in grammar.
                IEnumerable <IParseTree> keywords_interator = _all_nodes.Where((IParseTree n) =>
                {
                    TerminalNodeImpl nonterm = n as TerminalNodeImpl;
                    if (nonterm == null)
                    {
                        return(false);
                    }
                    for (var p = nonterm.Parent; p != null; p = p.Parent)
                    {
                        // "parser grammar" "lexer grammar" etc.
                        if (p is ANTLRv4Parser.GrammarTypeContext)
                        {
                            return(true);
                        }
                        if (p is ANTLRv4Parser.OptionsSpecContext)
                        {
                            return(true);
                        }
                        // "options ..."
                        if (p is ANTLRv4Parser.OptionContext)
                        {
                            return(false);
                        }
                        // "import ..."
                        if (p is ANTLRv4Parser.DelegateGrammarsContext)
                        {
                            return(true);
                        }
                        if (p is ANTLRv4Parser.DelegateGrammarContext)
                        {
                            return(false);
                        }
                        // "tokens ..."
                        if (p is ANTLRv4Parser.TokensSpecContext)
                        {
                            return(true);
                        }
                        if (p is ANTLRv4Parser.IdListContext)
                        {
                            return(false);
                        }
                        // "channels ..."
                        if (p is ANTLRv4Parser.ChannelsSpecContext)
                        {
                            return(true);
                        }
                        if (p is ANTLRv4Parser.ModeSpecContext)
                        {
                            return(true);
                        }
                    }
                    return(false);
                });
                _ant_keywords = keywords_interator.Select <IParseTree, IToken>(
                    (t) => (t as TerminalNodeImpl).Symbol).ToArray();
            }

            {
                // Get all defining and applied occurences of nonterminal names in grammar.
                IEnumerable <IParseTree> lit_nodes_iterator = _all_nodes.Where((IParseTree n) =>
                {
                    TerminalNodeImpl term = n as TerminalNodeImpl;
                    if (term == null)
                    {
                        return(false);
                    }
                    // Chicken/egg problem. Assume that literals are marked
                    // with the appropriate token type.
                    if (term.Symbol == null)
                    {
                        return(false);
                    }
                    if (!(term.Symbol.Type == ANTLRv4Parser.STRING_LITERAL ||
                          term.Symbol.Type == ANTLRv4Parser.INT ||
                          term.Symbol.Type == ANTLRv4Parser.LEXER_CHAR_SET))
                    {
                        return(false);
                    }

                    // The token must be part of parserRuleSpec context.
                    for (var p = term.Parent; p != null; p = p.Parent)
                    {
                        if (p is ANTLRv4Parser.ParserRuleSpecContext ||
                            p is ANTLRv4Parser.LexerRuleSpecContext)
                        {
                            return(true);
                        }
                    }
                    return(false);
                });
                _ant_literals = lit_nodes_iterator.Select <IParseTree, IToken>(
                    (t) => (t as TerminalNodeImpl).Symbol).ToArray();
            }

            //pp.ErrorHandler = new MyErrorStrategy();
        }