// Atom : Value | LParen Expression RParen internal static INode Produce(TokenBuffer tokens) { var value = Value.Produce(tokens); if (value != null) return value; // HACK if (tokens.Current is LeftParenthesis) tokens.SavePosition(); var lParen = tokens.GetTerminal<LeftParenthesis>(); if (lParen == null) return null; var expression = Expression.Produce(tokens); if (expression == null) { tokens.RestorePosition(); return null; } var rParen = tokens.GetTerminal<RightParenthesis>(); if (rParen == null) { throw new ParseException("Missing closing parenthesis"); } return expression; }
// ExpRoot (MulDivOp ExpRoot)* internal static INode Produce(TokenBuffer tokens) { var expRoot = ExpRoot.Produce(tokens); if (expRoot == null) return null; tokens.SavePosition(); return BuildSubNodes(expRoot, tokens); }
// AddSub : MulDiv (AddSubOp MulDiv)* internal static INode Produce(TokenBuffer tokens) { var mulDiv = MulDiv.Produce(tokens); if (mulDiv == null) return null; tokens.SavePosition(); return BuildSubNodes(mulDiv, tokens); }
// ExpRoot : Unary ExpRootOp ExpRoot | Unary internal static INode Produce(TokenBuffer tokens) { var unary = Unary.Produce(tokens); if (unary == null) return null; tokens.SavePosition(); var expRootOp = ExpRootOp.Produce(tokens); if (expRootOp == null) return unary; var rhs = ExpRoot.Produce(tokens); if (rhs != null) return new ExpRoot(unary, expRootOp, rhs); tokens.RestorePosition(); return null; }
// Unary : AddSubOp? Atom internal static INode Produce(TokenBuffer tokens) { // HACK if (tokens.Current is AdditionOperator || tokens.Current is SubtractionOperator) tokens.SavePosition(); var op = AddSubOp.Produce(tokens); var atom = Atom.Produce(tokens); if (atom != null) { if (op != null && op.IsMinus) return new Unary(op, atom); return atom; } tokens.RestorePosition(); return null; }