Ejemplo n.º 1
0
        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);
        }
Ejemplo n.º 2
0
        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);
            }
        }
Ejemplo n.º 3
0
        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));
        }
Ejemplo n.º 4
0
 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);
     }
 }
Ejemplo n.º 5
0
        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);
        }
Ejemplo n.º 6
0
        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);
        }
Ejemplo n.º 7
0
 public static DuplicateSourceVariablesException For <TToken>(
     IList <Token <TToken> > tokens,
     ITextIndexHelper translator)
 => new DuplicateSourceVariablesException(
     "Duplicate variable definitions found in source: ",
     TokenDetails(tokens, translator));
Ejemplo n.º 8
0
 public static IEnumerable <IParseTree <TokenType, ParserRuleType> > Parse(
     this IEnumerable <IList <Token <TokenType> > > tokens,
     ITextIndexHelper translator)
 => tokens.Select(o => Parser.Parse(o, translator));
Ejemplo n.º 9
0
 public static UnexpectedTokenException For <TToken>(Token <TToken> token, ITextIndexHelper translator)
 => new UnexpectedTokenException("Unexpected token: ", TokenDetails(token, translator));
Ejemplo n.º 10
0
 public static UndefinedVariablesException For <TToken>(
     IList <Token <TToken> > tokens,
     ITextIndexHelper translator)
 => new UndefinedVariablesException(
     "Undefined variables found in target: ",
     TokenDetails(tokens, translator));