public IParseTree <TToken, TRule> Parse(IList <Token <TToken> > tokens, ITextIndexHelper translator) { if (!this.configCompleted) { throw ParserConfigurationException.NotCompleted(); } var parseTree = this.rulesByType[this.rootType].TryParse(tokens, 0, this.rulesByType); if (parseTree.IsError || parseTree.NextTokenIndex != tokens.Count) { if (parseTree.MaxMatchedNextTokenIndex == tokens.Count) { throw new EndOfTokensException(); } if (parseTree.MaxMatchedNextTokenIndex < 0 || parseTree.MaxMatchedNextTokenIndex > tokens.Count) { throw new ApplicationException("An unexpected error was encountered."); } throw UnexpectedTokenException.For(tokens[parseTree.MaxMatchedNextTokenIndex], translator); } return(parseTree.TreeItems); }
public static IEnumerable <IParseTree <TokenType, ParserRuleType> > Validate( this IEnumerable <IParseTree <TokenType, ParserRuleType> > parseTrees, ITextIndexHelper translator) { foreach (var tree in parseTrees) { // Check for duplicate variable definitions in source, each // variable should be defined once only. var sourceVariables = GetVariables(tree, ParserRuleType.Source); var duplicateVariables = sourceVariables .GroupBy(o => o.Value) .Where(o => o.Count() != 1) .ToList(); if (duplicateVariables.Count != 0) { throw DuplicateSourceVariablesException.For( duplicateVariables.SelectMany(o => o.OrderBy(v => v.StartIndex).Skip(1)).ToList(), translator); } // Check for undefined variable definitions in target (i.e. in target but not source) var targetVariables = GetVariables(tree, ParserRuleType.Source); var sourceVariableNames = sourceVariables.Select(o => o.Value).ToHashSet(); var undefinedVariables = targetVariables.Where(o => !sourceVariableNames.Contains(o.Value)).ToList(); if (undefinedVariables.Count != 0) { throw UndefinedVariablesException.For(undefinedVariables, translator); } // Return the tree. yield return(tree); } }
protected static BadTokenWithPositionContext TokenDetails <TToken>( Token <TToken> token, ITextIndexHelper translator) { var location = translator?.LinePosition(token.StartIndex); return(new BadTokenWithPositionContext( token.Type.ToString(), token.Value, token.StartIndex, location)); }
public static IList <LineColumn> LinePosition(this ITextIndexHelper helper, IEnumerable <int> indexes) { try { return(indexes.Select(helper.LinePosition).ToList()); } catch (Exception) { /* Ignore exception since it is just trying to add context. */ return(null); } }
protected static IList <BadTokenWithPositionContext> TokenDetails <TToken>( IList <Token <TToken> > tokens, ITextIndexHelper translator) { var details = new BadTokenWithPositionContext[tokens.Count]; for (int i = 0; i < details.Length; ++i) { details[i] = TokenDetails(tokens[i], translator); } return(details); }
protected static IList <BadTokenWithPositionContext> TokenDetails( IList <BadToken> tokens, ITextIndexHelper translator) { var details = new BadTokenWithPositionContext[tokens.Count]; for (int i = 0; i < details.Length; ++i) { var baseDetails = tokens[i]; var location = translator?.LinePosition(baseDetails.StartIndex); details[i] = new BadTokenWithPositionContext(baseDetails, location); } return(details); }
public static DuplicateSourceVariablesException For <TToken>( IList <Token <TToken> > tokens, ITextIndexHelper translator) => new DuplicateSourceVariablesException( "Duplicate variable definitions found in source: ", TokenDetails(tokens, translator));
public static IEnumerable <IParseTree <TokenType, ParserRuleType> > Parse( this IEnumerable <IList <Token <TokenType> > > tokens, ITextIndexHelper translator) => tokens.Select(o => Parser.Parse(o, translator));
public static UnexpectedTokenException For <TToken>(Token <TToken> token, ITextIndexHelper translator) => new UnexpectedTokenException("Unexpected token: ", TokenDetails(token, translator));
public static UndefinedVariablesException For <TToken>( IList <Token <TToken> > tokens, ITextIndexHelper translator) => new UndefinedVariablesException( "Undefined variables found in target: ", TokenDetails(tokens, translator));