namespace Parsley { using System.Collections.Generic; using System.Linq; using System.Reflection; using Primitives; public abstract class Grammar { protected void InferGrammarRuleNames() { var grammarRules = GetType() .GetRuntimeFields() .Where(rule => rule.FieldType.GetTypeInfo().IsGenericType && rule.FieldType.GetGenericTypeDefinition() == typeof (GrammarRule<>)); foreach (var rule in grammarRules) { var value = rule.GetValue(this); if (value != null) { var nameProperty = value.GetType().GetRuntimeProperty("Name"); var name = nameProperty.GetValue(value, null); if (name as string == null) nameProperty.SetValue(value, rule.Name, null); } } } public static Parser Fail() { return new FailingParser(); } public static Parser EndOfInput { get { return Token(TokenKind.EndOfInput); } } public static Parser Token(TokenKind kind) { return new TokenByKindParser(kind); } public static Parser Token(string expectation) { return new TokenByLiteralParser(expectation); } /// /// ZeroOrMore(p) repeatedly applies an parser p until it fails, returing /// the list of values returned by successful applications of p. At the /// end of the sequence, p must fail without consuming input, otherwise the /// sequence will fail with the error reported by p. /// public static Parser> ZeroOrMore(Parser item) { return new ZeroOrMoreParser(item); } /// /// OneOrMore(p) behaves like ZeroOrMore(p), except that p must succeed at least one time. /// public static Parser> OneOrMore(Parser item) { return from first in item from rest in ZeroOrMore(item) select List(first, rest); } /// /// ZeroOrMore(p, s) parses zero or more occurrences of p separated by occurrences of s, /// returning the list of values returned by successful applications of p. /// public static Parser> ZeroOrMore(Parser item, Parser separator) { return Choice(OneOrMore(item, separator), Zero()); } /// /// OneOrMore(p, s) behaves like ZeroOrMore(p, s), except that p must succeed at least one time. /// public static Parser> OneOrMore(Parser item, Parser separator) { return from first in item from rest in ZeroOrMore(from sep in separator from next in item select next) select List(first, rest); } /// /// Between(left, goal, right) parses its arguments in order. If all three /// parsers succeed, the result of the goal parser is returned. /// public static Parser Between(Parser left, Parser goal, Parser right) { return from L in left from G in goal from R in right select G; } /// /// Optional(p) is equivalent to p whenever p succeeds or when p fails after consuming input. /// If p fails without consuming input, Optional(p) succeeds. /// public static Parser Optional(Parser parser) { var nothing = default(T).SucceedWithThisValue(); return Choice(parser, nothing); } /// /// The parser Attempt(p) behaves like parser p, except that it pretends /// that it hasn't consumed any input when an error occurs. This combinator /// is used whenever arbitrary look ahead is needed. /// public static Parser Attempt(Parser parse) { return new AttemptParser(parse); } /// /// Choice() always fails without consuming input. /// /// Choice(p) is equivalent to p. /// /// For 2 or more inputs, parsers are applied from left /// to right. If a parser succeeds, its reply is returned. /// If a parser fails without consuming input, the next parser /// is attempted. If a parser fails after consuming input, /// subsequent parsers will not be attempted. As long as /// parsers conume no input, their error messages are merged. /// /// Choice is 'predictive' since p[n+1] is only tried when /// p[n] didn't consume any input (i.e. the look-ahead is 1). /// This non-backtracking behaviour allows for both an efficient /// implementation of the parser combinators and the generation /// of good error messages. /// public static Parser Choice(params Parser[] parsers) { if (parsers.Length == 0) return Fail(); return new ChoiceParser(parsers); } /// /// When parser p consumes any input, Label(p, e) is the same as p. /// When parser p does not consume any input, Label(p, e) is the same /// as p, except any messages are replaced with expectation e. /// public static Parser Label(Parser parser, string expectation) { return new LabeledParser(parser, expectation); } private static IEnumerable List(T first, IEnumerable rest) { yield return first; foreach (T item in rest) yield return item; } private static Parser> Zero() { return Enumerable.Empty().SucceedWithThisValue(); } } }