Ejemplo n.º 1
0
        public Lexema GetNext()
        {
            Lexema lLex = null;

            try
            {
                lLex = GetNextInternal();
                if (mDebugMode && lLex != null)
                {
                    mDebugText += string.Format("{0}: '{1}'\r\n", lLex.Type, lLex.Text);
                }
            }
            catch (Exception ex)
            {
                if (mDebugMode)
                {
                    mDebugText += string.Format("\r\nException in lexer: {0}\r\n", ex.Message);
                }
                else
                {
                    throw;
                }
            }
            return(lLex);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Parses Acc to array or seq ([], .)
        /// and ListTrans call e.g. Param.Count()
        /// </summary>
        /// <param Name="lBaseAcc"></param>
        /// <returns>true - access found, continue chain of accs, false - stop parsing</returns>
        private bool Acc(ref IExpr lBaseAcc)
        {
            //arr
            if (null != lex)
            {
                if (lex.Text == "[")
                {
                    lex = lex.Next();
                    IExpr lIndExpr = Expr();
                    ExpectSign("]");
                    lBaseAcc = new AccessArray(mGrammar, (Access)lBaseAcc, lIndExpr);
                    return(true);
                }

                //struct
                if (lex.Text == ".")
                {
                    lex = lex.Next();
                    string lFieldName = lex.Text;
                    lex = lex.Next();
                    if (lex != null && lex.Type == LexemType.LeftBr)
                    {
// call of ListTrans
                        lex      = lex.Next();
                        lBaseAcc = (IExpr)TransCall1((Access)lBaseAcc, lFieldName);
                    }
                    else
                    {
                        lBaseAcc = new AccessSeq(mGrammar, lBaseAcc, lFieldName);
                        return(true);
                    }
                }
            }
            return(false);
        }
Ejemplo n.º 3
0
        private IExpr MulExpr()
        {
            IExpr lRes;

            if (lex.Type == LexemType.LeftBr)
            {
                lex  = lex.Next();
                lRes = Expr();
                Expect(LexemType.RightBr, ")");
            }
            else if (lex.Type == LexemType.NotTerminal)
            {
                string lName = lex.Text;
                lex = lex.Next();
                if (lex != null && lex.Type == LexemType.LeftBr)
                {
                    lex  = lex.Next();
                    lRes = (IExpr)TransCall1(null, lName);
                }
                else
                {
                    IExpr lAcc = new Access(mGrammar, lName);
                    Accs(ref lAcc);
                    return(lAcc);
                }
            }
            else
            {
                lRes = ParseExprValue();
            }
            return(lRes);
        }
Ejemplo n.º 4
0
        /// <summary>
        /// Can return ExprInt, ExprDouble, ExprString or variable
        /// 1, 1.2, "aaa", %VARNAME%
        /// </summary>
        /// <returns></returns>
        private IExpr ParseExprValue()
        {
            IExpr lRes;

            if (lex.Type == LexemType.Terminal)
            {
                lRes = new ExprString(lex.Text);
                lex  = lex.Next();
            }
            else
            {
                string lIntStr = lex.Text;
                lRes = ParseIntValue();
                //check if doble
                if (lex != null && lex.Text == ".")
                {
                    lex = lex.Next();
                    CheckType(LexemType.IntNumber);
                    Double d =
                        double.Parse(lIntStr
                                     + Thread.CurrentThread.CurrentCulture.NumberFormat.NumberDecimalSeparator
                                     + lex.Text);
                    lex  = lex.Next();
                    lRes = new ExprDouble(d);
                }
            }
            return(lRes);
        }
Ejemplo n.º 5
0
 // Seqs   := ",", Seq, Seqs
 //           | ""
 private void Seqs(ref IPhrase aPhr)
 {
     if (lex != null)
     {
         if (lex.Type == LexemType.Operation)
         {
             if (lex.Text == "," || lex.Text == ";")
             {
                 bool lIsComma = lex.Text == ",";
                 lex = lex.Next();
                 Seqence lSeq = aPhr as Seqence;
                 if (lSeq == null)
                 {
                     lSeq             = new Seqence(mGrammar, aPhr);
                     lSeq.InsertSpace = lIsComma;
                     aPhr             = lSeq;
                 }
                 IPhrase lSeqPhr = Seq();
                 lSeq.Add(lSeqPhr);
                 AsAlias(ref lSeqPhr);
                 Seqs(ref aPhr);
             }
         }
         //else
         //{
         //  throw new GrammarSynaxException(string.Format("'{0}' not expected here (,) operation expected", lex.Text));
         //}
     }
 }
Ejemplo n.º 6
0
        //protected Grammar mGrammar;

        public Rule(Grammar aGrammar, Lexema aLex)
        {
            if (aLex == null || aLex.Type != LexemType.NotTerminal)
            {
                throw new GrammarSyntaxException("Rule Name expected");
            }

            Name = aLex.Text;
            aLex = aLex.Next();
            if (aLex != null && aLex.Type == LexemType.Terminal)
            {
                mSeparator = aLex.Text;
                aLex       = aLex.Next();
            }

            if (aLex == null || aLex.Type != LexemType.IsSign)
            {
                throw new GrammarSyntaxException(":= expected");
            }

            aLex = aLex.Next();

            PhraseParser lParser = new PhraseParser(aGrammar, aLex);

            RightSide = lParser.ParseRule();
        }
Ejemplo n.º 7
0
 private void Quant(ref IPhrase aPhr)
 {
     // look for quantifier here
     if (lex != null && (lex.Text == "{" ||
                         lex.Text == "*" ||
                         lex.Text == "+" ||
                         lex.Text == "?"))
     {
         ExprInt lMin = null, lMax = null;
         //{1..5}
         if (lex.Text == "{")
         {
             lex  = lex.Next();
             lMin = ParseIntValue();
             if (lex.Text == ".")
             {
                 lex = lex.Next();
                 ExpectSign(".");
                 if (lex.Text == "*")
                 {
                     lMax = new ExprInt(Int32.MaxValue);
                 }
                 else
                 {
                     lMax = ParseIntValue();
                 }
             }
             else
             {
                 lMax = lMin;
             }
             ExpectSign("}");
         }
         else if (lex.Text == "*")
         {
             lMin = new ExprInt(0);
             lMax = new ExprInt(int.MaxValue);
             lex  = lex.Next();
         }
         else if (lex.Text == "+")
         {
             lMin = new ExprInt(1);
             lMax = new ExprInt(int.MaxValue);
             lex  = lex.Next();
         }
         else if (lex.Text == "?")
         {
             lMin = new ExprInt(0);
             lMax = new ExprInt(1);
             lex  = lex.Next();
         }
         else
         {
             throw new GrammarSyntaxException("Недолжно быть. Неподдерживаемый знак после нетерминала " + lex.Text);
         }
         QuantifiedPhrase lQuant = new QuantifiedPhrase(mGrammar, aPhr, lMin, lMax);
         aPhr = lQuant;
     }
 }
Ejemplo n.º 8
0
 private void Expect(LexemType aType, string aText)
 {
     if (lex.Type != aType || lex.Text != aText)
     {
         throw new GrammarSyntaxException(string.Format("{0} expexted, but {1} found", aText, lex.Text));
     }
     lex = lex.Next();
 }
Ejemplo n.º 9
0
 private void TransParams(ref TransCallPhrase aPhr)
 {
     if (lex.Type == LexemType.Operation && lex.Text == ",")
     {
         lex = lex.Next();
         TransParam(ref aPhr);
         TransParams(ref aPhr);
     }
 }
Ejemplo n.º 10
0
        private void ParseLine(string aLine)
        {
            string lInputLine = aLine.Trim();
            //Обрежем комментарии в конце строки
            int liComment = lInputLine.IndexOf("//");

            if (liComment >= 0)
            {
                lInputLine = lInputLine.Substring(0, liComment);
            }
            // если строка оканчивается \, то объединяем ее со следующей
            if (lInputLine.EndsWith(@"\"))
            {
                mPrevS += lInputLine.Substring(0, lInputLine.Length - 1);
            }
            else
            {
                lInputLine = mPrevS + lInputLine;
                mPrevS     = "";
                // создать правило.
                if (!string.IsNullOrEmpty(lInputLine))
                {
                    Lexer  lexer = new Lexer(lInputLine);
                    Lexema lex   = lexer.GetNext();
                    try
                    {
                        Rule lRull = new Rule(this, lex);
                        // проверить, если есть такое правило с этим именем, то не добавлять правило, а добавлять альтернативы
                        if (Rules.ContainsKey(lRull.Name))
                        {
                            throw new GrammarSyntaxException("правило с этим именем уже определено!", aLine);
                            //объединить правило
                            //Rules[lRull.mLeftSide].mRightSides.mAlternatives.AddRange(lRull.mRightSides.mAlternatives);
                        }
                        else
                        {
                            Rules.Add(lRull.Name, lRull);
                        }
                    }
                    catch (GrammarSyntaxException ex)
                    {
                        ex.Line     = mLine;
                        ex.Col      = lexer.Col;
                        ex.LineText = lInputLine;
                        SyntaxErrors.Add(ex);
                    }
                    finally
                    {
                        LexerDebugText += lexer.DebugText;
                    }
                }
            }
            mLine++;
        }
Ejemplo n.º 11
0
        //Returns simple Nonterminal symbol or TransCallPhrase
        private IPhrase TransCall()
        {
            string Name = lex.Text;

            lex = lex.Next();
            if (lex != null && lex.Type == LexemType.LeftBr)
            {
                lex = lex.Next();
                return(TransCall1(null, Name));
            }
            return(NonTerminal.Create(mGrammar, null, Name)); //TODO!!!
        }
Ejemplo n.º 12
0
 private void AsAlias(ref IPhrase aPhr)
 {
     if (null != lex)
     {
         if (lex.Type == LexemType.NotTerminal && lex.Text.ToLower() == "as")
         {
             lex = lex.Next();
             CheckType(LexemType.NotTerminal);
             aPhr.Alias = lex.Text;
             lex        = lex.Next();
         }
     }
 }
Ejemplo n.º 13
0
 private void MulExprs(ref IExpr aExpr)
 {
     if (lex != null)
     {
         if (lex.Type == LexemType.MulOp)
         {
             eOperation lOp = ExprOp.ParseOperation(lex.Text);
             lex = lex.Next();
             ExprOp lExprOp = aExpr as ExprOp;
             if (lExprOp == null || lExprOp.Operation != lOp)
             {
                 lExprOp = new ExprOp(lOp);
                 lExprOp.AddOperand(aExpr);
                 aExpr = lExprOp;
             }
             lExprOp.AddOperand(Expr());
             MulExprs(ref aExpr);
         }
     }
 }
Ejemplo n.º 14
0
 private PlaceHolderPhrase PlaceHolderAssign(string aName)
 {
     if (lex.Type == LexemType.Sign)
     {
         if (lex.Text == "=")
         {
             lex = lex.Next();
             IPhrase lPhr = Seq();
             AsAlias(ref lPhr);
             return(new PlaceHolderAssignPhrase(mGrammar, aName, lPhr, false));
         }
         else if (lex.Text == "+")
         {
             lex = lex.Next();
             IPhrase lPhr = Seq();
             AsAlias(ref lPhr);
             return(new PlaceHolderAssignPhrase(mGrammar, aName, lPhr, true));
         }
     }
     return(new PlaceHolderPhrase(mGrammar, aName));
 }
Ejemplo n.º 15
0
 private ExprInt ParseIntValue()
 {
     //check vars
     if (lex.Type == LexemType.Sign && lex.Text == "%")
     {
         //variable
         lex = lex.Next();
         CheckType(LexemType.NotTerminal);
         string lVarName = lex.Text;
         ExpectSign("%");
         throw new NotImplementedException();
         //return new ExprConfVariable(lVarName);
     }
     else
     {
         //number
         CheckType(LexemType.IntNumber);
         int r = int.Parse(lex.Text);
         lex = lex.Next();
         return(new ExprInt(r));
     }
 }
Ejemplo n.º 16
0
 //  Alts   := "|", Alt, Alts
 //           | ""
 private void Alts(ref IPhrase aPhr)
 {
     if (lex != null)
     {
         if (lex.Type == LexemType.Operation && lex.Text == "|")
         {
             lex = lex.Next();
             AlternativeSet lAlt = aPhr as AlternativeSet;
             if (lAlt == null)
             {
                 lAlt = new AlternativeSet(mGrammar, aPhr);
                 aPhr = lAlt;
             }
             lAlt.Add(Alt()); //ParsePhrase());
             Alts(ref aPhr);
         }
         //else
         //{
         //  throw new GrammarSynaxException(string.Format("'{0}' not expected here (|) operation expected",lex.Text));
         //}
     }
 }
Ejemplo n.º 17
0
        //Seq    := T | NT | "(",Phrase,")"
        private IPhrase Seq()
        {
            if (lex == null)
            {
                throw new GrammarSyntaxException("unexpected end of rule");
            }
            IPhrase lPhr;

            if (lex.Type == LexemType.LeftBr)
            {
                lex  = lex.Next();
                lPhr = ParsePhrase();
                Expect(LexemType.RightBr, ")");
                Quant(ref lPhr);
            }
            else if (lex.Type == LexemType.Sign && lex.Text == "[")
            {
                lex  = lex.Next();
                lPhr = new QuantifiedPhrase(mGrammar, ParsePhrase(), 0, 1);
                ExpectSign("]");
            }
            else if (lex.Type == LexemType.Sign && lex.Text == "{")
            {
                lex  = lex.Next();
                lPhr = new QuantifiedPhrase(mGrammar, ParsePhrase());
                ExpectSign("}");
            }
            else if (lex.Type == LexemType.Sign && lex.Text == "<")
            {
                lex = lex.Next();
                if (lex.Type != LexemType.NotTerminal)
                {
                    throw new GrammarSyntaxException("Name expected");
                }
                string lName = lex.Text;
                lex = lex.Next();
                ExpectSign(">");
                lPhr = PlaceHolderAssign(lName);
                Quant(ref lPhr);
            }
            else if (lex.Text == "#")
            {
                lex  = lex.Next();
                lPhr = Expr();
            }
            else
            {
                //терминальный или не терминальный
                if (lex.Type == LexemType.Terminal)
                {
                    lPhr = new Terminal(mGrammar, lex.Text);
                    lex  = lex.Next();
                }
                else if (lex.Type == LexemType.NotTerminal)
                {
                    lPhr = TransCall();
                }
                else
                {
                    throw new GrammarSyntaxException("Terminal or Non-Terminal symbol expected");
                }
                Quant(ref lPhr);
            }

            return(lPhr);
        }
Ejemplo n.º 18
0
 public PhraseParser(Grammar aGrammar, Lexema aLex)
 {
     mGrammar = aGrammar;
     lex      = aLex;
 }