public AlternativeParser(Type baseType, IConfig config, ParserFlags flags, IEnumerable <Type>?optionTypes = null) : base(config, flags) { OutputType = baseType; if (optionTypes != null) { optionTypes = optionTypes.ToArray(); if (baseType != typeof(Unnamed)) { foreach (var type in optionTypes) { if (!baseType.IsAssignableFrom(type)) { throw new ArgumentException($"Option '{type}' is not assignable to base type '{baseType}'"); } } } } else { optionTypes = baseType.Assembly.GetTypes().Where(t => (t.IsClass || t.IsValueType) && !t.IsAbstract && baseType.IsAssignableFrom(t)); } // ReSharper disable once PossibleMultipleEnumeration _options = optionTypes.Select(ParserCache.GetParser).ToArray(); if (_options.Length == 0) { throw new ArgumentException($"{baseType} has no concrete flags"); } }
public ParseResult ApplyFlags(ParserFlags Flags) { return(new ParseResult { ResultType = ResultType, Node = Node, StreamState = StreamState, Flags = Flags, FailReason = FailReason, }); }
public override IParser Modify(IParser input, IConfig config, ParserFlags parserFlags) { if (behind != null) { throw new NotSupportedException("Look-behind not supported"); } if (direction == '!') { return(new NotParser(input, config, parserFlags)); } else { return(new LookAheadParser(input, config, parserFlags)); } }
public override IParser Create(IConfig config, ParserFlags flags) => new CharSet(Ranges, config, flags);
public IParser Create(string regexPattern, IConfig config, ParserFlags flags) => new Parser(new SubstringParser(endAnchor == null ? pattern.Create(config, flags) : new ConcatParser(config, flags, pattern.Create(config, flags), new EOFParser(config, flags)), config, flags), regexPattern, config, flags); // TODO: Don't pass in regexPattern, retrieve it by unparsing
public LiteralParser(string literal, IConfig config, ParserFlags flags) : base(config, flags) => this.literal = literal;
public AlternativeParser(Type outputType, IConfig config, ParserFlags flags, IEnumerable <IParser> alternatives) : base(config, flags) { OutputType = outputType; _options = alternatives.ToArray(); }
public IParser Create(IConfig config, ParserFlags flags) => Items.Count == 1 ? Items[0].Create(config, flags) : new AlternativeParser(typeof(string), config, flags, Items.Select(s => s.Create(config, flags)));
public LocationWriter(IConfig config, ParserFlags flags) : base(config, flags) { }
public virtual IParser Modify(IParser input, IConfig config, ParserFlags parserFlags) => input;
public ConcatParser(IConfig config, ParserFlags flags, params IParser[] children) : base(config, flags) => this._children = children;
public override IParser Create(IConfig config, ParserFlags flags) => new OptionalParser(inner.Create(config, flags), config, flags);
public override IParser Create(IConfig config, ParserFlags flags) => modifier?.Modify(inner.Create(config, flags), config, flags) ?? inner.Create(config, flags);
[Optional, Literal("?")] string lazy; // TODO: Support 'lazy' qualifiers public override IParser Create(IConfig config, ParserFlags flags) => new RepeatParser(typeof(string), inner.Create(config, flags), null, repeater == '+' ? 1 : 0, null, config, flags);
public abstract IParser Create(IConfig config, ParserFlags flags);
public IParser Create(IConfig config, ParserFlags flags) => Items.Count == 1 ? Items[0].Create(config, flags) : new ConcatParser(config, flags, Items.Select(s => s.Create(config, flags)).ToArray());
regexPattern, config, flags); // TODO: Don't pass in regexPattern, retrieve it by unparsing public static IParser Parse(string pattern, IConfig config, ParserFlags flags) => Lexico.Parse <Regex>(pattern).Create(pattern, config, flags);
protected ParserBase(IConfig config, ParserFlags flags) => Flags = flags | config.Get(ParserFlags.None);
public SubstringParser(IParser inner, IConfig config, ParserFlags flags) : base(config, flags) => this._inner = inner;
public SequenceParser(Type type, IParser?separator, bool checkZeroLength, IConfig config, ParserFlags flags) : base(config, flags) { this.CheckZeroLength = checkZeroLength; this.Type = type ?? throw new ArgumentNullException(nameof(type)); this._separator = separator; var typeHierachy = new List <Type>(); var current = type; while (current != null && current != typeof(object)) { typeHierachy.Add(current); current = current.BaseType; } typeHierachy.Reverse(); var rawMembers = typeHierachy.SelectMany(t => t.GetMembers(Instance | Public | NonPublic) .Where(m => m is FieldInfo || m is PropertyInfo) .Where(m => m.GetCustomAttributes <TermAttribute>(true).Any()) ); var members = new List <MemberInfo>(); // Combine virtual/override members into one list foreach (var m in rawMembers) { if (!IsPrivate(m)) { int i; for (i = 0; i < members.Count; i++) { if (!IsPrivate(members[i]) && members[i].Name == m.Name) { members[i] = m; break; } } if (i < members.Count) { continue; } } members.Add(m); } if (members.Count == 0) { throw new ArgumentException($"Sequence {type} has no Terms"); } this._members = members .Select(m => ( m.GetMemberType() == typeof(Unnamed) ? null : m, ParserCache.GetParser(m) )) .ToArray(); }
public CharParser(IConfig config, ParserFlags flags) : base(config, flags) { }
public bool CheckFlag(ParserFlags Flag) { return((Flags & Flag) == Flag); }
public CharSet(CharIntervalSet set, IConfig config, ParserFlags flags) : base(config, flags) { _ranges = set.Intervals.ToArray(); }
public override IParser Create(IConfig config, ParserFlags flags) => new CharSet(new CharIntervalSet().Include(Value, Value), config, flags);
public Pass(IConfig config, ParserFlags flags) : base(config, flags) { }
public override IParser Create(IConfig config, ParserFlags flags) => throw new NotSupportedException();
public SurroundParser(IParser?prefix, IParser inner, IParser?suffix, IConfig config, ParserFlags flags) : base(config, flags) { this._prefix = prefix; this._inner = inner ?? throw new ArgumentNullException(nameof(inner)); this._suffix = suffix; }
public override IParser Create(IConfig config, ParserFlags flags) { throw new NotImplementedException(); }
public NumberParser(IConfig config, ParserFlags flags, NumberStyles styles, Type numberType) : base(config, flags) { parseMethod = numberType.GetMethod(nameof(int.Parse), new [] { typeof(string), typeof(NumberStyles) }) ?? throw new ArgumentException($"{numberType} has no Parse method"); // TODO: Able to set CultureInfo? var formatInfo = CultureInfo.InvariantCulture.NumberFormat; var pattern = new StringBuilder("^"); bool Has(NumberStyles ns) => (styles & ns) != 0; if (Has(AllowThousands)) { throw new NotSupportedException(AllowThousands.ToString()); } if (Has(AllowLeadingWhite)) { pattern.Append(@"\s*"); } if (Has(AllowLeadingSign)) { pattern.Append(@"[\-\+]?"); } if (Has(AllowCurrencySymbol)) { pattern.Append($"(?>{Regex.Escape(formatInfo.CurrencySymbol)}?"); } if (Has(AllowParentheses)) { pattern.Append(@"((?'Open'\())?"); } if (Has(AllowHexSpecifier)) { pattern.Append(@"[0-9A-Fa-f]+"); } else { if (Has(AllowDecimalPoint)) { pattern.Append(@"[0-9]+(\.[0-9]+)?"); } else { pattern.Append(@"[0-9]+"); } } if (Has(AllowExponent)) { pattern.Append(@"(?>[eE][\-\+]?[0-9]+)?"); } if (Has(AllowParentheses)) { pattern.Append(@"(?(Open)\)|)"); } if (Has(AllowTrailingSign)) { pattern.Append(@"[\-\+]?"); } if (Has(AllowTrailingWhite)) { pattern.Append(@"\s*"); } regex = RegexImpl.Regex.Parse(pattern.ToString(), config, flags); this.styles = styles; }
public Parser(IParser inner, string pattern, IConfig config, ParserFlags flags) : base(config, flags) { _inner = inner; _pattern = pattern; }