Exemplo n.º 1
0
        /// <summary>
        /// Parse a ruleref
        /// </summary>
        private IRuleRef ParseRuleRef(SrgsRuleRef srgsRuleRef, IElement parent)
        {
            IRuleRef ruleRef         = null;
            bool     fSpecialRuleRef = true;

            if (srgsRuleRef == SrgsRuleRef.Null)
            {
                ruleRef = _parser.Null;
            }
            else if (srgsRuleRef == SrgsRuleRef.Void)
            {
                ruleRef = _parser.Void;
            }
            else if (srgsRuleRef == SrgsRuleRef.Garbage)
            {
                ruleRef = _parser.Garbage;
            }
            else
            {
                ruleRef         = _parser.CreateRuleRef(parent, srgsRuleRef.Uri, srgsRuleRef.SemanticKey, srgsRuleRef.Params);
                fSpecialRuleRef = false;
            }

            if (fSpecialRuleRef)
            {
                _parser.InitSpecialRuleRef(parent, ruleRef);
            }

            ruleRef.PostParse(parent);
            return(ruleRef);
        }
Exemplo n.º 2
0
 public RuleOperation Complete(IRuleRef rule, RuleResult result)
 {
     return(() =>
     {
         _parser.Current.Result.Resolve(result);
     });
 }
Exemplo n.º 3
0
        internal override IElement CreateElement(IElementFactory elementFactory, IElement parent, IRule rule, IdentifierCollection ruleIds)
        {
            IRuleRef garbage = elementFactory.Garbage;

            elementFactory.InitSpecialRuleRef(parent, garbage);
            return(garbage);
        }
Exemplo n.º 4
0
 public Repeat(string?name, IRuleRef rule, int min = 0, int max = -1)
 {
     Name = name;
     Rule = rule;
     _min = min;
     _max = max;
 }
Exemplo n.º 5
0
 public RuleOperation Token(IRuleRef rule, IToken token)
 {
     return(() =>
     {
         DebugHooks.OnToken(_parser.Current, token);
         _parser.Current.NodeContext.Add(rule.RefName, _parser._grammar.TerminalReducer(rule, token));
     });
 }
Exemplo n.º 6
0
        public RuleOperation Call(IRuleRef rule, TokenStreamIndex index, out CoroutineResult <RuleResult> result)
        {
            var operation = _operations.Call(rule, index, out result);

            // accumulate step counts of called child rules
            result.OnResolve += value => { stepCount += value.StepCount; };
            return(operation);
        }
        internal override IElement CreateElement(IElementFactory elementFactory, IElement parent, IRule rule, IdentifierCollection ruleIds)
        {
            // Return a ruleref to Garbage
            IRuleRef ruleRef = elementFactory.Garbage;

            elementFactory.InitSpecialRuleRef(parent, ruleRef);

            return(ruleRef);
        }
Exemplo n.º 8
0
 internal RuleState(IRuleRef rule, TokenStreamIndex index, IRuleOperations coroutines, RuleState <TNode>?parent)
 {
     Rule        = (IRuleRef <TNode>)rule;
     Result      = new CoroutineResult <RuleResult>();
     NodeContext = new NodeContext <TNode>();
     Parent      = parent;
     RuleContext = new RuleContext(Rule, index, coroutines);
     Iterator    = rule.Grab(RuleContext).GetEnumerator();
 }
Exemplo n.º 9
0
        public RuleOperation Call(IRuleRef rule, TokenStreamIndex index, out CoroutineResult <RuleResult> result)
        {
            // Make sure that each "nested" call gets its own result
            result = new CoroutineResult <RuleResult>();
            _results.Push(result);

            var context = new RuleContext(rule, index, this);

            rule.Grab(context).ToList();

            return(_noOpRuleOperation);
        }
Exemplo n.º 10
0
            public RuleOperation Call(IRuleRef rule, TokenStreamIndex index, out CoroutineResult <RuleResult> result)
            {
                var state = new RuleState <TNode>(rule, index, this, _parser._ruleStack.Peek());

                result = state.Result;

                return(() =>
                {
                    DebugHooks.OnBeforeCall(state, rule, index);
                    _parser._ruleStack.Push(state);
                });
            }
Exemplo n.º 11
0
        public void StartWith(string name)
        {
            foreach (var kvp in _unresolvedRules)
            {
                if (!_definedRules.ContainsKey(kvp.Key))
                {
                    throw new GrammarException(GrammarExceptionId.GrammarContainsUnresolvedRule, kvp.Key);
                }

                var ruleRef = _definedRules[kvp.Key];
                foreach (var unresolved in kvp.Value)
                {
                    unresolved.Resolve(ruleRef);
                }
            }
            _unresolvedRules.Clear();

            StartRule = Ref(name);
        }
 void IElementFactory.InitSpecialRuleRef(IElement parent, IRuleRef specialRule)
 {
     ((RuleRef)specialRule).InitSpecialRuleRef(_backend, (ParseElementCollection)parent);
 }
Exemplo n.º 13
0
 public static IRuleRef <TNode> ReduceWith <TNode>(this IRuleRef <TNode> ruleRef, SingleReducer <TNode> reducer)
 {
     return(ruleRef.ReduceWith((rule, page) => EnumSequence.Of(reducer(rule, page))));
 }
Exemplo n.º 14
0
 public RuleContext(IRuleRef rule, TokenStreamIndex index, IRuleOperations operations)
 {
     _rule       = rule;
     Index       = index;
     _operations = operations;
 }
Exemplo n.º 15
0
 public Not(string?name, IRuleRef target)
 {
     Name   = name;
     Target = target;
 }
 public static IRuleRef <TNode> Optional <TNode>(this IGrammarBuilder <TNode> grammar, string?name, IRuleRef rule)
 {
     return(grammar.DefineRule(name, new Repeat(name, rule, 0, 1)));
 }
Exemplo n.º 17
0
 void IElementFactory.InitSpecialRuleRef(IElement parent, IRuleRef special)
 {
 }
Exemplo n.º 18
0
 public RuleOperation Complete(IRuleRef rule, RuleResult result)
 {
     _results.Pop().Resolve(result);
     return(_noOpRuleOperation);
 }
Exemplo n.º 19
0
 public RuleOperation Token(IRuleRef rule, IToken token)
 {
     Tokens.Add(token);
     return(_noOpRuleOperation);
 }
Exemplo n.º 20
0
 public Task OnAfterCall(RuleState <TNode> state, IRuleRef rule, TokenStreamIndex idx, RuleResult res)
 {
     return(Task.CompletedTask);
 }
Exemplo n.º 21
0
 public static IRuleRef <TNode> Lift <TNode>(this IRuleRef <TNode> ruleRef)
 {
     return(ruleRef.As(IRuleRef.LiftRefName));
 }
 public static IRuleRef <TNode> OneOrMore <TNode>(this IGrammarBuilder <TNode> grammar, string?name, IRuleRef rule)
 {
     return(grammar.DefineRule(name, new Repeat(name, rule, 1, -1)));
 }
 public static IRuleRef <TNode> Not <TNode>(this IGrammarBuilder <TNode> grammar, string?name, IRuleRef target)
 {
     return(grammar.DefineRule(name, new Not(name, target)));
 }
Exemplo n.º 24
0
 public Task OnBeforeCall(RuleState <TNode> state, IRuleRef rule, TokenStreamIndex idx)
 {
     return(Task.CompletedTask);
 }
Exemplo n.º 25
0
 public NullRule(IRuleRef root)
 {
     _root = root;
 }
 public static IRuleRef <TNode> Repeat <TNode>(this IGrammarBuilder <TNode> grammar, string?name, IRuleRef rule, int min = 0, int max = -1)
 {
     return(grammar.DefineRule(name, new Repeat(name, rule, min, max)));
 }