private static Factor ParseFactor(Tree tree, Token token, Token[] tokens, ref int index) { if (token is Identifier) { var node = new ebnf.Nodes.Identifier { Value = token.Value }; return(node); } if (token is Literal) { var node = new Terminal { Value = token.Value }; return(node); } if (token is Operator && token.Value.Equals("(")) { token = NextToken(tokens, ref index); var node = new Grouping { Expression = ParseExpression(tree, ref token, tokens, ref index, ')') }; if (!(token is Operator) || !token.Value.Equals(")")) { tree.Errors.Add(CreateError(token, ErrorStrings.HangingGrouping)); } return(node); } if (token is Operator && token.Value.Equals("[")) { token = NextToken(tokens, ref index); var node = new Option { Expression = ParseExpression(tree, ref token, tokens, ref index, ']') }; if (!(token is Operator) || !token.Value.Equals("]")) { tree.Errors.Add(CreateError(token, ErrorStrings.HangingOption)); } return(node); } if (token is Operator && token.Value.Equals("{")) { token = NextToken(tokens, ref index); var node = new Repetition { Expression = ParseExpression(tree, ref token, tokens, ref index, '}') }; if (!(token is Operator) || !token.Value.Equals("}")) { tree.Errors.Add(CreateError(token, ErrorStrings.HangingRepetition)); } return(node); } if (token is Operator && token.Value.Equals("?")) { token = NextToken(tokens, ref index); var node = new SpecialSequence { Expression = ParseExpression(tree, ref token, tokens, ref index, '?') }; if (!(token is Operator) || !token.Value.Equals("?")) { tree.Errors.Add(CreateError(token, ErrorStrings.HangingSpecialSequence)); } return(node); } tree.Errors.Add(CreateError(token, string.Format(ErrorStrings.ExpectedFactor, token == null ? "nothing" : token.Value))); return(null); }
private static Factor ParseFactor(Tree tree, Token token, Token[] tokens, ref int index) { if(token is Identifier) { var node = new ebnf.Nodes.Identifier {Value = token.Value}; return node; } if(token is Literal) { var node = new Terminal { Value = token.Value }; return node; } if(token is Operator && token.Value.Equals("(")) { token = NextToken(tokens, ref index); var node = new Grouping { Expression = ParseExpression(tree, ref token, tokens, ref index, ')') }; if(!(token is Operator) || !token.Value.Equals(")")) { tree.Errors.Add(CreateError(token, ErrorStrings.HangingGrouping)); } return node; } if (token is Operator && token.Value.Equals("[")) { token = NextToken(tokens, ref index); var node = new Option { Expression = ParseExpression(tree, ref token, tokens, ref index, ']') }; if (!(token is Operator) || !token.Value.Equals("]")) { tree.Errors.Add(CreateError(token, ErrorStrings.HangingOption)); } return node; } if (token is Operator && token.Value.Equals("{")) { token = NextToken(tokens, ref index); var node = new Repetition { Expression = ParseExpression(tree, ref token, tokens, ref index, '}') }; if (!(token is Operator) || !token.Value.Equals("}")) { tree.Errors.Add(CreateError(token, ErrorStrings.HangingRepetition)); } return node; } if (token is Operator && token.Value.Equals("?")) { token = NextToken(tokens, ref index); var node = new SpecialSequence { Expression = ParseExpression(tree, ref token, tokens, ref index, '?') }; if (!(token is Operator) || !token.Value.Equals("?")) { tree.Errors.Add(CreateError(token, ErrorStrings.HangingSpecialSequence)); } return node; } tree.Errors.Add(CreateError(token, string.Format(ErrorStrings.ExpectedFactor, token == null ? "nothing" : token.Value))); return null; }