Ejemplo n.º 1
0
        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");
            }
        }
Ejemplo n.º 2
0
 public ParseResult ApplyFlags(ParserFlags Flags)
 {
     return(new ParseResult
     {
         ResultType = ResultType,
         Node = Node,
         StreamState = StreamState,
         Flags = Flags,
         FailReason = FailReason,
     });
 }
Ejemplo n.º 3
0
 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));
     }
 }
Ejemplo n.º 4
0
 public override IParser Create(IConfig config, ParserFlags flags) => new CharSet(Ranges, config, flags);
Ejemplo n.º 5
0
 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
Ejemplo n.º 6
0
 public LiteralParser(string literal, IConfig config, ParserFlags flags) : base(config, flags) => this.literal = literal;
Ejemplo n.º 7
0
 public AlternativeParser(Type outputType, IConfig config, ParserFlags flags, IEnumerable <IParser> alternatives) : base(config, flags)
 {
     OutputType = outputType;
     _options   = alternatives.ToArray();
 }
Ejemplo n.º 8
0
 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)));
Ejemplo n.º 9
0
 public LocationWriter(IConfig config, ParserFlags flags) : base(config, flags)
 {
 }
Ejemplo n.º 10
0
 public virtual IParser Modify(IParser input, IConfig config, ParserFlags parserFlags) => input;
Ejemplo n.º 11
0
 public ConcatParser(IConfig config, ParserFlags flags, params IParser[] children) : base(config, flags) => this._children = children;
Ejemplo n.º 12
0
 public override IParser Create(IConfig config, ParserFlags flags) => new OptionalParser(inner.Create(config, flags), config, flags);
Ejemplo n.º 13
0
 public override IParser Create(IConfig config, ParserFlags flags) => modifier?.Modify(inner.Create(config, flags), config, flags) ?? inner.Create(config, flags);
Ejemplo n.º 14
0
        [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);
Ejemplo n.º 15
0
 public abstract IParser Create(IConfig config, ParserFlags flags);
Ejemplo n.º 16
0
 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());
Ejemplo n.º 17
0
                                                                                                    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);
Ejemplo n.º 18
0
 protected ParserBase(IConfig config, ParserFlags flags) => Flags = flags | config.Get(ParserFlags.None);
Ejemplo n.º 19
0
 public SubstringParser(IParser inner, IConfig config, ParserFlags flags) : base(config, flags) => this._inner = inner;
Ejemplo n.º 20
0
        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();
        }
Ejemplo n.º 21
0
 public CharParser(IConfig config, ParserFlags flags) : base(config, flags)
 {
 }
Ejemplo n.º 22
0
 public bool CheckFlag(ParserFlags Flag)
 {
     return((Flags & Flag) == Flag);
 }
Ejemplo n.º 23
0
 public CharSet(CharIntervalSet set, IConfig config, ParserFlags flags) : base(config, flags)
 {
     _ranges = set.Intervals.ToArray();
 }
Ejemplo n.º 24
0
 public override IParser Create(IConfig config, ParserFlags flags) => new CharSet(new CharIntervalSet().Include(Value, Value), config, flags);
Ejemplo n.º 25
0
 public Pass(IConfig config, ParserFlags flags) : base(config, flags)
 {
 }
Ejemplo n.º 26
0
 public override IParser Create(IConfig config, ParserFlags flags) => throw new NotSupportedException();
Ejemplo n.º 27
0
 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;
 }
Ejemplo n.º 28
0
 public override IParser Create(IConfig config, ParserFlags flags)
 {
     throw new NotImplementedException();
 }
Ejemplo n.º 29
0
        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;
        }
Ejemplo n.º 30
0
 public Parser(IParser inner, string pattern, IConfig config, ParserFlags flags) : base(config, flags)
 {
     _inner   = inner;
     _pattern = pattern;
 }