//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(); }
/// <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); }
/// <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); }
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); }
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; } }
//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!!! }
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(); } } }
//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); }