Example #1
0
        public ExprOp Parse(InputTextStream inStream, AmountParseFlagsEnum flags = AmountParseFlagsEnum.PARSE_DEFAULT, string originalString = null)
        {
            try
            {
                ExprOp topNode = ParseValueExpr(inStream, flags);

                if (UseLookahead)
                {
                    UseLookahead = false;
                    Lookahead.Rewind(inStream);
                }
                Lookahead.Clear();

                return(topNode);
            }
            catch
            {
                if (!String.IsNullOrEmpty(originalString))
                {
                    ErrorContext.Current.AddErrorContext("While parsing value expression:");
                    int endPos = inStream.Pos;
                    int pos    = endPos > 0 ? endPos - Lookahead.Length : 0;

                    Logger.Current.Debug("parser.error", () => String.Format("original_string = '{0}'", originalString));
                    Logger.Current.Debug("parser.error", () => String.Format("            pos = {0}", pos));
                    Logger.Current.Debug("parser.error", () => String.Format("        end_pos = {0}", endPos));
                    Logger.Current.Debug("parser.error", () => String.Format("     token kind = {0}", Lookahead.Kind));
                    Logger.Current.Debug("parser.error", () => String.Format("   token length = {0}", Lookahead.Length));

                    ErrorContext.Current.AddErrorContext(ErrorContext.LineContext(originalString, pos, endPos));
                }
                throw;
            }
        }
Example #2
0
        public ExprOp Parse(InputTextStream inStream, AmountParseFlagsEnum flags = AmountParseFlagsEnum.PARSE_DEFAULT, string originalString = null)
        {
            try
            {
                ExprOp topNode = ParseValueExpr(inStream, flags);

                if (UseLookahead)
                {
                    UseLookahead = false;
                    Lookahead.Rewind(inStream);
                }
                Lookahead.Clear();

                return(topNode);
            }
            catch
            {
                if (!String.IsNullOrEmpty(originalString))
                {
                    ErrorContext.Current.AddErrorContext("While parsing value expression:");
                    int endPos = inStream.Pos;
                    int pos    = endPos > 0 ? endPos - Lookahead.Length : 0;
                    ErrorContext.Current.AddErrorContext(ErrorContext.LineContext(originalString, pos, endPos));
                }
                throw;
            }
        }
Example #3
0
 public void AddLookaheads(IEnumerable <TerminalExpressionDefinition> lookaheads)
 {
     foreach (TerminalExpressionDefinition lookahead in lookaheads)
     {
         Lookahead.Add(lookahead);
     }
 }
Example #4
0
        private Expression ArithmeticExpression()
        {
            Expression FirstPointer = Term();

            while (Lookahead.Is("+") || Lookahead.Is("-"))
            {
                string Operator = Lookahead.Lexeme;
                Match(Lookahead.Lexeme);
                Expression SecondPointer = Term();
                FirstPointer = new BinaryOperator(Operator, FirstPointer, SecondPointer);
            }
            return(FirstPointer);
        }
Example #5
0
        private Expression ComparisonExpression()
        {
            Expression FirstPointer = ArithmeticExpression();

            while (Lookahead.Is(">") || Lookahead.Is("?") || Lookahead.Is("<"))
            {
                string Operator = Lookahead.Lexeme;
                Match(Lookahead.Lexeme);
                Expression SecondPointer = ArithmeticExpression();
                FirstPointer = new BinaryOperator(Operator, FirstPointer, SecondPointer);
            }
            return(FirstPointer);
        }
Example #6
0
        private Expression Term()
        {
            Expression FirstPointer = Factor();

            while (Lookahead.Is("*") || Lookahead.Is("/"))
            {
                string Operator = Lookahead.Lexeme;
                Match(Lookahead.Lexeme);
                Expression SecondPointer = Factor();
                FirstPointer = new BinaryOperator(Operator, FirstPointer, SecondPointer);
            }
            return(FirstPointer);
        }
Example #7
0
        private Token PeekNextToken()
        {
            // Если подглядываем первый раз, то необходимо сохранить _lookahead иначе _tokenizer перезатрет
            if (_peekTokens.Count == 0)
            {
                Lookahead = Lookahead.Clone();
            }
            else
            {
                _peekTokens.Last.Value = _peekTokens.Last.Value.Clone();
            }
            var result = _tokenizer.ReadToken();

            _peekTokens.AddLast(result);
            return(result);
        }
Example #8
0
        public ExprToken NextToken(InputTextStream inStream, AmountParseFlagsEnum tFlags, ExprTokenKind?expecting = null)
        {
            if (UseLookahead)
            {
                UseLookahead = false;
            }
            else
            {
                Lookahead.Next(inStream, tFlags);
            }

            if (expecting.HasValue && Lookahead.Kind != expecting.Value)
            {
                Lookahead.Expected(expecting.Value);
            }

            return(Lookahead);
        }
Example #9
0
        private Expression Expression()
        {
            Expression FirstPointer = ComparisonExpression();


            while (Lookahead.Is("&") || Lookahead.Is("|") || Lookahead.Is("="))
            {
                string Operator = Lookahead.Lexeme;
                Match(Lookahead.Lexeme);
                Expression SecondPointer = ComparisonExpression();
                FirstPointer = new BinaryOperator(Operator, FirstPointer, SecondPointer);
            }



            if (Lookahead.Is(";"))
            {
                Lookahead = NextToken();
                //ParserOutput = FirstPointer.IntoString();
                //ParserFinalOutput.Add(ParserOutput);

                if (MoreTokens())
                {
                    if (Lookahead.Type == TokenType.Token_IDENTIFIER || Lookahead.Type == TokenType.Token_RESERVEDWORD)// && Lookahead.Type!=TokenType.Token_EOF&& Lookahead.Lexeme!="}" &&  Lookahead.Lexeme!=")")
                    {
                        Expression();
                    }
                }
            }

            if (Lookahead.Type == TokenType.Token_EOF)
            {
                ParserOutput = FirstPointer.IntoString();
                ParserFinalOutput.Add(ParserOutput);
                return(FirstPointer);
            }

            return(FirstPointer);
        }
Example #10
0
 public override int GetHashCode() =>
 Production.Id.GetHashCode() ^ Position.GetHashCode() ^ Lookahead.GetHashCode();
Example #11
0
 public bool Equals(LR1Item?other) =>
 (other != null) &&
 (Production.Id == other.Production.Id) && (Position == other.Position) &&
 Lookahead.Equals(other.Lookahead);
Example #12
0
 private static String phase1ExpansionGen(Expansion e)
 {
     String retval = "";
     Token t = null;
     Lookahead[] conds;
     String[] actions;
     if (e is RegularExpression) {
         RegularExpression e_nrw = (RegularExpression) e;
         retval += "\n";
         if (e_nrw.LhsTokens.Count != 0) {
             CSharpCCGlobals.PrintTokenSetup(e_nrw.LhsTokens[0]);
             foreach (var token in e_nrw.LhsTokens) {
                 t = token;
                 retval += CSharpCCGlobals.PrintToken(t);
             }
             retval += CSharpCCGlobals.PrintTrailingComments(t);
             retval += " = ";
         }
         String tail = e_nrw.RhsToken == null ? ");" : ")." + e_nrw.RhsToken.image + ";";
         if (e_nrw.Label.Equals("")) {
             string label;
             if (CSharpCCGlobals.names_of_tokens.TryGetValue(e_nrw.Ordinal, out label)) {
                 retval += "cc_consume_token(" + label + tail;
             } else {
                 retval += "cc_consume_token(" + e_nrw.Ordinal + tail;
             }
         } else {
             retval += "cc_consume_token(" + e_nrw.Label + tail;
         }
     } else if (e is NonTerminal) {
         NonTerminal e_nrw = (NonTerminal) e;
         retval += "\n";
         if (e_nrw.LhsTokens.Count != 0) {
             CSharpCCGlobals.PrintTokenSetup(e_nrw.LhsTokens[0]);
             foreach (var token in e_nrw.LhsTokens) {
                 t = token;
                 retval += CSharpCCGlobals.PrintToken(t);
             }
             retval += CSharpCCGlobals.PrintTrailingComments(t);
             retval += " = ";
         }
         retval += e_nrw.Name + "(";
         if (e_nrw.ArgumentTokens.Count != 0) {
             CSharpCCGlobals.PrintTokenSetup(e_nrw.ArgumentTokens[0]);
             foreach (var token in e_nrw.ArgumentTokens) {
                 t = token;
                 retval += CSharpCCGlobals.PrintToken(t);
             }
             retval += CSharpCCGlobals.PrintTrailingComments(t);
         }
         retval += ");";
     } else if (e is Action) {
         Action e_nrw = (Action) e;
         retval += "\u0003\n";
         if (e_nrw.ActionTokens.Count != 0) {
             CSharpCCGlobals.PrintTokenSetup(e_nrw.ActionTokens[0]);
             CSharpCCGlobals.ccol = 1;
             foreach (var token in e_nrw.ActionTokens) {
                 t = token;
                 retval += CSharpCCGlobals.PrintToken(t);
             }
             retval += CSharpCCGlobals.PrintTrailingComments(t);
         }
         retval += "\u0004";
     } else if (e is Choice) {
         Choice e_nrw = (Choice) e;
         conds = new Lookahead[e_nrw.Choices.Count];
         actions = new String[e_nrw.Choices.Count + 1];
         actions[e_nrw.Choices.Count] = "\n" + "cc_consume_token(-1);\n" + "throw new ParseException();";
         // In previous line, the "throw" never throws an exception since the
         // evaluation of cc_consume_token(-1) causes ParseException to be
         // thrown first.
         Sequence nestedSeq;
         for (int i = 0; i < e_nrw.Choices.Count; i++) {
             nestedSeq = (Sequence) (e_nrw.Choices[i]);
             actions[i] = phase1ExpansionGen(nestedSeq);
             conds[i] = (Lookahead) (nestedSeq.Units[0]);
         }
         retval = buildLookaheadChecker(conds, actions);
     } else if (e is Sequence) {
         Sequence e_nrw = (Sequence) e;
         // We skip the first element in the following iteration since it is the
         // Lookahead object.
         foreach (var unit in e_nrw.Units) {
             retval += phase1ExpansionGen(unit);
         }
     } else if (e is OneOrMore) {
         OneOrMore e_nrw = (OneOrMore) e;
         Expansion nested_e = e_nrw.Expansion;
         Lookahead la;
         if (nested_e is Sequence) {
             la = (Lookahead) (((Sequence) nested_e).Units[0]);
         } else {
             la = new Lookahead();
             la.Amount = Options.getLookahead();
             la.Expansion = nested_e;
         }
         retval += "\n";
         int labelIndex = ++gensymindex;
         retval += "while (true) {\u0001";
         retval += phase1ExpansionGen(nested_e);
         conds = new Lookahead[1];
         conds[0] = la;
         actions = new String[2];
         actions[0] = "\n;";
         actions[1] = "\ngoto label_" + labelIndex + ";";
         retval += buildLookaheadChecker(conds, actions);
         retval += "\u0002\n" + "}";
         retval += "label_" + labelIndex + ":;\n";
     } else if (e is ZeroOrMore) {
         ZeroOrMore e_nrw = (ZeroOrMore) e;
         Expansion nested_e = e_nrw.Expansion;
         Lookahead la;
         if (nested_e is Sequence) {
             la = (Lookahead) (((Sequence) nested_e).Units[0]);
         } else {
             la = new Lookahead();
             la.Amount = Options.getLookahead();
             la.Expansion = nested_e;
         }
         retval += "\n";
         int labelIndex = ++gensymindex;
         retval += "while (true) {\u0001";
         conds = new Lookahead[1];
         conds[0] = la;
         actions = new String[2];
         actions[0] = "\n;";
         actions[1] = "\ngoto label_" + labelIndex + ";";
         retval += buildLookaheadChecker(conds, actions);
         retval += phase1ExpansionGen(nested_e);
         retval += "\u0002\n" + "}";
         retval += "label_" + labelIndex + ":;\n";
     } else if (e is ZeroOrOne) {
         ZeroOrOne e_nrw = (ZeroOrOne) e;
         Expansion nested_e = e_nrw.Expansion;
         Lookahead la;
         if (nested_e is Sequence) {
             la = (Lookahead) (((Sequence) nested_e).Units[0]);
         } else {
             la = new Lookahead();
             la.Amount = Options.getLookahead();
             la.Expansion = nested_e;
         }
         conds = new Lookahead[1];
         conds[0] = la;
         actions = new String[2];
         actions[0] = phase1ExpansionGen(nested_e);
         actions[1] = "\n;";
         retval += buildLookaheadChecker(conds, actions);
     } else if (e is TryBlock) {
         TryBlock e_nrw = (TryBlock) e;
         Expansion nested_e = e_nrw.Expansion;
         IList<Token> list;
         retval += "\n";
         retval += "try {\u0001";
         retval += phase1ExpansionGen(nested_e);
         retval += "\u0002\n" + "}";
         for (int i = 0; i < e_nrw.CatchBlocks.Count; i++) {
             retval += " catch (";
             list = e_nrw.Types[i];
             if (list.Count != 0) {
                 CSharpCCGlobals.PrintTokenSetup(list[0]);
                 foreach (var token in list) {
                     t = token;
                     retval += CSharpCCGlobals.PrintToken(t);
                 }
                 retval += CSharpCCGlobals.PrintTrailingComments(t);
             }
             retval += " ";
             t = e_nrw.Ids[i];
             CSharpCCGlobals.PrintTokenSetup(t);
             retval += CSharpCCGlobals.PrintToken(t);
             retval += CSharpCCGlobals.PrintTrailingComments(t);
             retval += ") {\u0003\n";
             list = e_nrw.CatchBlocks[i];
             if (list.Count != 0) {
                 CSharpCCGlobals.PrintTokenSetup(list[0]);
                 CSharpCCGlobals.ccol = 1;
                 foreach (var token in list) {
                     t = token;
                     retval += CSharpCCGlobals.PrintToken(t);
                 }
                 retval += CSharpCCGlobals.PrintTrailingComments(t);
             }
             retval += "\u0004\n" + "}";
         }
         if (e_nrw.FinallyBlocks != null) {
             retval += " finally {\u0003\n";
             if (e_nrw.FinallyBlocks.Count != 0) {
                 CSharpCCGlobals.PrintTokenSetup(e_nrw.FinallyBlocks[0]);
                 CSharpCCGlobals.ccol = 1;
                 foreach (var token in e_nrw.FinallyBlocks) {
                     t = token;
                     retval += CSharpCCGlobals.PrintToken(t);
                 }
                 retval += CSharpCCGlobals.PrintTrailingComments(t);
             }
             retval += "\u0004\n" + "}";
         }
     }
     return retval;
 }
Example #13
0
 private static void buildPhase2Routine(Lookahead la)
 {
     Expansion e = la.Expansion;
     ostr.WriteLine("  private " + CSharpCCGlobals.staticOpt() + " bool cc_2" + e.InternalName + "(int xla) {");
     ostr.WriteLine("    cc_la = xla; cc_lastpos = cc_scanpos = token;");
     ostr.WriteLine("    try { return !cc_3" + e.InternalName + "(); }");
     ostr.WriteLine("    catch(LookaheadSuccess) { return true; }");
     if (Options.getErrorReporting())
         ostr.WriteLine("    finally { cc_save(" + (Int32.Parse(e.InternalName.Substring(1), CultureInfo.InvariantCulture) - 1) + ", xla); }");
     ostr.WriteLine("  }");
     ostr.WriteLine("");
     Phase3Data p3d = new Phase3Data(e, la.Amount);
     phase3list.Add(p3d);
     phase3table[e] = p3d;
 }
Example #14
0
        private static String buildLookaheadChecker(Lookahead[] conds, String[] actions)
        {
            // The state variables.
            int state = NOOPENSTM;
            int indentAmt = 0;
            bool[] casedValues = new bool[CSharpCCGlobals.tokenCount];
            String retval = "";
            Lookahead la;
            Token t = null;
            int tokenMaskSize = (CSharpCCGlobals.tokenCount - 1)/32 + 1;
            int[] tokenMask = null;

            // Iterate over all the conditions.
            int index = 0;
            while (index < conds.Length) {

                la = conds[index];
                cc2LA = false;

                if ((la.Amount == 0) ||
                    Semanticize.EmptyExpansionExists(la.Expansion) ||
                    CodeCheck(la.Expansion)) {

                    // This handles the following cases:
                    // . If syntactic lookahead is not wanted (and hence explicitly specified
                    //   as 0).
                    // . If it is possible for the lookahead expansion to recognize the empty
                    //   string - in which case the lookahead trivially passes.
                    // . If the lookahead expansion has a JAVACODE production that it directly
                    //   expands to - in which case the lookahead trivially passes.
                    if (la.ActionTokens.Count == 0) {
                        // In addition, if there is no semantic lookahead, then the
                        // lookahead trivially succeeds.  So break the main loop and
                        // treat this case as the default last action.
                        break;
                    } else {
                        // This case is when there is only semantic lookahead
                        // (without any preceding syntactic lookahead).  In this
                        // case, an "if" statement is generated.
                        switch (state) {
                            case NOOPENSTM:
                                retval += "\n" + "if (";
                                indentAmt++;
                                break;
                            case OPENIF:
                                retval += "\u0002\n" + "} else if (";
                                break;
                            case OPENSWITCH:
                                retval += "\u0002\n" + "default:" + "\u0001";
                                if (Options.getErrorReporting()) {
                                    retval += "\ncc_la1[" + CSharpCCGlobals.maskindex + "] = cc_gen;";
                                    CSharpCCGlobals.maskindex++;
                                }
                                CSharpCCGlobals.maskVals.Add(tokenMask);
                                retval += "\n" + "if (";
                                indentAmt++;
                                break;
                        }

                        CSharpCCGlobals.PrintTokenSetup(la.ActionTokens[0]);
                        foreach (var token in la.ActionTokens) {
                            t = token;
                            retval += CSharpCCGlobals.PrintToken(t);
                        }

                        retval += CSharpCCGlobals.PrintTrailingComments(t);
                        retval += ") {\u0001" + actions[index];
                        state = OPENIF;
                    }

                } else if (la.Amount == 1 && la.ActionTokens.Count == 0) {
                    // Special optimal processing when the lookahead is exactly 1, and there
                    // is no semantic lookahead.

                    if (firstSet == null) {
                        firstSet = new bool[CSharpCCGlobals.tokenCount];
                    }
                    for (int i = 0; i < CSharpCCGlobals.tokenCount; i++) {
                        firstSet[i] = false;
                    }
                    // cc2LA is set to false at the beginning of the containing "if" statement.
                    // It is checked immediately after the end of the same statement to determine
                    // if lookaheads are to be performed using calls to the cc2 methods.
                    GenFirstSet(la.Expansion);
                    // GenFirstSet may find that semantic attributes are appropriate for the next
                    // token.  In which case, it sets cc2LA to true.
                    if (!cc2LA) {

                        // This case is if there is no applicable semantic lookahead and the lookahead
                        // is one (excluding the earlier cases such as JAVACODE, etc.).
                        switch (state) {
                            case OPENIF:
                                retval += "\u0002\n" + "} else {\u0001";
                                // Control flows through to next case.
                                goto case NOOPENSTM;
                            case NOOPENSTM:
                                retval += "\n" + "switch (";
                                if (Options.getCacheTokens()) {
                                    retval += "cc_nt.Kind) {\u0001";
                                } else {
                                    retval += "(cc_ntKind==-1)?cc_ntk():cc_ntKind) {\u0001";
                                }
                                for (int i = 0; i < CSharpCCGlobals.tokenCount; i++) {
                                    casedValues[i] = false;
                                }
                                indentAmt++;
                                tokenMask = new int[tokenMaskSize];
                                for (int i = 0; i < tokenMaskSize; i++) {
                                    tokenMask[i] = 0;
                                }
                                // Don't need to do anything if state is OPENSWITCH.
                                break;
                        }
                        for (int i = 0; i < CSharpCCGlobals.tokenCount; i++) {
                            if (firstSet[i]) {
                                if (!casedValues[i]) {
                                    casedValues[i] = true;
                                    retval += "\u0002\ncase ";
                                    int j1 = i/32;
                                    int j2 = i%32;
                                    tokenMask[j1] |= 1 << j2;
                                    string s;
                                    if (!CSharpCCGlobals.names_of_tokens.TryGetValue(i, out s)) {
                                        retval += i;
                                    } else {
                                        retval += s;
                                    }
                                    retval += ":\u0001";
                                }
                            }
                        }
                        retval += actions[index];
                        retval += "\nbreak;";
                        state = OPENSWITCH;
                    }

                } else {
                    // This is the case when lookahead is determined through calls to
                    // jj2 methods.  The other case is when lookahead is 1, but semantic
                    // attributes need to be evaluated.  Hence this crazy control structure.

                    cc2LA = true;

                }

                if (cc2LA) {
                    // In this case lookahead is determined by the jj2 methods.

                    switch (state) {
                        case NOOPENSTM:
                            retval += "\n" + "if (";
                            indentAmt++;
                            break;
                        case OPENIF:
                            retval += "\u0002\n" + "} else if (";
                            break;
                        case OPENSWITCH:
                            retval += "\u0002\n" + "default:" + "\u0001";
                            if (Options.getErrorReporting()) {
                                retval += "\ncc_la1[" + CSharpCCGlobals.maskindex + "] = cc_gen;";
                                CSharpCCGlobals.maskindex++;
                            }
                            CSharpCCGlobals.maskVals.Add(tokenMask);
                            retval += "\n" + "if (";
                            indentAmt++;
                            break;
                    }
                    CSharpCCGlobals.cc2index++;
                    // At this point, la.la_expansion.InternalName must be "".
                    la.Expansion.InternalName = "_" + CSharpCCGlobals.cc2index;
                    phase2list.Add(la);
                    retval += "cc_2" + la.Expansion.InternalName + "(" + la.Amount + ")";
                    if (la.ActionTokens.Count != 0) {
                        // In addition, there is also a semantic lookahead.  So concatenate
                        // the semantic check with the syntactic one.
                        retval += " && (";
                        CSharpCCGlobals.PrintTokenSetup(la.ActionTokens[0]);
                        foreach (var token in la.ActionTokens) {
                            t = token;
                            retval += CSharpCCGlobals.PrintToken(t);
                        }

                        retval += CSharpCCGlobals.PrintTrailingComments(t);
                        retval += ")";
                    }

                    retval += ") {\u0001" + actions[index];
                    state = OPENIF;
                }

                index++;
            }

            // Generate code for the default case.  Note this may not
            // be the last entry of "actions" if any condition can be
            // statically determined to be always "true".

            switch (state) {
                case NOOPENSTM:
                    retval += actions[index];
                    break;
                case OPENIF:
                    retval += "\u0002\n" + "} else {\u0001" + actions[index];
                    break;
                case OPENSWITCH:
                    retval += "\u0002\n" + "default:" + "\u0001";
                    if (Options.getErrorReporting()) {
                        retval += "\ncc_la1[" + CSharpCCGlobals.maskindex + "] = cc_gen;";
                        CSharpCCGlobals.maskVals.Add(tokenMask);
                        CSharpCCGlobals.maskindex++;
                    }
                    retval += actions[index];
                    break;
            }

            for (int i = 0; i < indentAmt; i++) {
                retval += "\u0002\n}";
            }

            return retval;
        }
Example #15
0
        /// <summary>
        /// Прочитать очередную лексему
        /// </summary>
        /// <param name="isRegexAllowed">Регулярные выражения допустимы? Влияет на обработку /</param>
        public Token ReadToken(bool isRegexAllowed = false)
        {
            Lookahead.SetUnknown();
            // Используется цикл так как могут встретится комментарии, которые необходимо пропускать
            while (!_input.IsEOF && Lookahead.Type == TokenType.Unknown)
            {
                // Пропускаем пробельные символы
                while (char.IsWhiteSpace((char)_input.CurChar))
                {
                    // Запоминаем если встретили символ окончания строки (нужно для автоматической расстановки ;)
                    // см. http://ecma-international.org/ecma-262/5.1/#sec-7.9
                    if (_input.CurChar == '\n')
                    {
                        Lookahead.IsAfterLineTerminator = true;
                    }
                    _input.ReadChar();
                }

                Lookahead.StartPosition = new TokenPosition(_input.LineNo, _input.ColumnNo);

                if (IsIdentStartChar((char)_input.CurChar))
                {
                    ReadIdentOrKeyword();
                }
                else
                {
                    switch (_input.CurChar)
                    {
                    case '{':
                        Lookahead.Type = TokenType.LCurlyBrace;
                        _input.ReadChar();
                        break;

                    case '}':
                        Lookahead.Type = TokenType.RCurlyBrace;
                        _input.ReadChar();
                        break;

                    case '(':
                        Lookahead.Type = TokenType.LParenthesis;
                        _input.ReadChar();
                        break;

                    case ')':
                        Lookahead.Type = TokenType.RParenthesis;
                        _input.ReadChar();
                        break;

                    case '[':
                        Lookahead.Type = TokenType.LBracket;
                        _input.ReadChar();
                        break;

                    case ']':
                        Lookahead.Type = TokenType.RBracket;
                        _input.ReadChar();
                        break;

                    case '.':
                        // Число также может начинаться с точки
                        if (char.IsDigit((char)_input.PeekChar()))
                        {
                            ReadIntegerOrFloatNumber();
                        }
                        else
                        {
                            Lookahead.Type = TokenType.Dot;
                            _input.ReadChar();
                        }
                        break;

                    case ';':
                        Lookahead.Type = TokenType.Semicolon;
                        _input.ReadChar();
                        break;

                    case ',':
                        Lookahead.Type = TokenType.Comma;
                        _input.ReadChar();
                        break;

                    case '<':
                        Read_Lt_or_Lte_or_Shl_or_ShlAssign();
                        break;

                    case '>':
                        Read_Gt_or_Gte_or_ShrS_or_ShrSAssign_or_ShrU_or_ShrUAssign();
                        break;

                    case '=':
                        Read_Eq_or_Assign_or_StrictEq();
                        break;

                    case '+':
                        Read_Plus_or_PlusAssign_or_Inc();
                        break;

                    case '-':
                        Read_Minus_or_MinusAssign_or_Dec();
                        break;

                    case '*':
                        Read_Star_or_StarAssign();
                        break;

                    case '/':
                        TryRead_Slash_or_SlashAssign(isRegexAllowed);
                        break;

                    case '%':
                        Read_Mod_or_ModAssign();
                        break;

                    case '~':
                        Lookahead.Type = TokenType.BitNot;
                        _input.ReadChar();
                        break;

                    case '&':
                        Read_BitAnd_or_BitAndAssign_or_And();
                        break;

                    case '|':
                        Read_BitOr_or_BitOrAssign_or_Or();
                        break;

                    case '^':
                        Read_BitXor_or_BitXorAssign();
                        break;

                    case '!':
                        Read_Not_or_Neq_or_StrictNeq();
                        break;

                    case '?':
                        Lookahead.Type = TokenType.QuestionMark;
                        _input.ReadChar();
                        break;

                    case ':':
                        Lookahead.Type = TokenType.Colon;
                        _input.ReadChar();
                        break;

                    case '0':
                        _input.ReadChar();
                        if (_input.CurChar == 'x' || _input.CurChar == 'X')
                        {
                            ReadHexIntegerNumber();
                        }
                        else
                        {
                            ReadIntegerOrFloatNumber();
                        }
                        break;

                    case '1':
                    case '2':
                    case '3':
                    case '4':
                    case '5':
                    case '6':
                    case '7':
                    case '8':
                    case '9':
                        ReadIntegerOrFloatNumber();
                        break;

                    case '\'':
                        ReadString('\'');
                        break;

                    case '\"':
                        ReadString('\"');
                        break;

                    default:
                        if (_input.CurChar != -1)
                        {
                            ThrowUnexpectedChar();
                        }
                        break;
                    }
                }
            }
            return(Lookahead);
        }
Example #16
0
 public override int GetHashCode()
 {
     return(HashCode.Combine(SubProductionId, DotIndex, Lookahead.GetSequenceHashCode()));
 }
Example #17
0
        private Expression Factor()
        {
            Expression FirstPointer = null;

            if (Lookahead.Is("("))
            {
                Match("(");
                FirstPointer = Expression();
                Match(")");
                return(FirstPointer);
            }

            if (Lookahead.Is(TokenType.Token_INT_LITERAL))
            {
                int Value = int.Parse(Lookahead.Lexeme);
                Match(ExpressionsID.IntLiteral);
                FirstPointer = new IntLiteral(Value);
                return(FirstPointer);
            }

            if (Lookahead.Is(TokenType.Token_IDENTIFIER))
            {
                FirstPointer = Identifier();
                return(FirstPointer);
            }

            if (Lookahead.Is("if"))
            {
                Match("if");
                Match("(");
                Expression Condition = Expression();
                Match(")");
                Match("{");
                Expression Then = Expression();
                Match("}");
                if (Lookahead.Lexeme != "else")
                {
                    FirstPointer = new If(Condition, Then);
                    return(FirstPointer);
                }
                Match("else");
                Match("{");
                Expression Else = Expression();
                Match("}");
                FirstPointer = new If(Condition, Then, Else);
                return(FirstPointer);
            }

            if (Lookahead.Is("while"))
            {
                Match("while");
                Match("(");
                Expression Condition = Expression();
                Match(")");
                Match("{");
                Expression Body = new Expression();
                while (!Lookahead.Is("}"))
                {
                    Body = Expression();
                }
                Match("}");
                FirstPointer = new While(Condition, Body);
                return(FirstPointer);
            }
            if (Lookahead.Type == TokenType.Token_EOF)
            {
                return(FirstPointer);
            }

            return(FirstPointer);
        }
Example #18
0
        public Item Clone(bool incrementDotIndex = false)
        {
            Item item = new Item(SubProduction, incrementDotIndex ? DotIndex + 1 : DotIndex, Lookahead.ToHashSet());

            return(item);
        }
Example #19
0
		public Sequence(Token token, Lookahead la) {
			Line = token.beginLine;
			Column = token.beginColumn;
			Units.Add(la);
		}
Example #20
0
 private Parser(IEnumerable <Token> tokens)
 {
     _tokenEnumerator = new Lookahead <Token>(tokens);
     _constantsSet    = new ConstantsSet();
 }
Example #21
0
            public void Action(Expansion e)
            {
                if (e is Sequence) {
                    if (e.Parent is Choice || e.Parent is ZeroOrMore ||
                        e.Parent is OneOrMore || e.Parent is ZeroOrOne)
                        return;
                    Sequence seq = (Sequence) e;
                    Lookahead la = (Lookahead) (seq.Units[0]);
                    if (!la.IsExplicit)
                        return;

                    // Create a singleton choice with an empty action.
                    Choice ch = new Choice();
                    ch.Line = la.Line;
                    ch.Column = la.Column;
                    ch.Parent = seq;
                    Sequence seq1 = new Sequence();
                    seq1.Line = la.Line;
                    seq1.Column = la.Column;
                    seq1.Parent = ch;
                    seq1.Units.Add(la);
                    la.Parent = seq1;
                    Action act = new Action();
                    act.Line = la.Line;
                    act.Column = la.Column;
                    act.Parent = seq1;
                    seq1.Units.Add(act);
                    ch.Choices.Add(seq1);
                    if (la.Amount != 0) {
                        if (la.ActionTokens.Count != 0) {
                            CSharpCCErrors.Warning(la,
                                "Encountered LOOKAHEAD(...) at a non-choice location.  " +
                                "Only semantic lookahead will be considered here.");
                        } else
                            CSharpCCErrors.Warning(la, "Encountered LOOKAHEAD(...) at a non-choice location.  This will be ignored.");
                    }
                    // Now we have moved the lookahead into the singleton choice.  Now create
                    // a new dummy lookahead node to replace this one at its original location.
                    Lookahead la1 = new Lookahead();
                    la1.IsExplicit = false;
                    la1.Line = la.Line;
                    la1.Column = la.Column;
                    la1.Parent = seq;
                    // Now set the la_expansion field of la and la1 with a dummy expansion (we use EOF).
                    la.Expansion = new REndOfFile();
                    la1.Expansion = new REndOfFile();
                    seq.Units[0] = la1;
                    seq.Units[1] = ch;
                }
            }