示例#1
0
            public void Bind <TResultNode>(ParserProduction parserProduction) where TResultNode : TActualNode
            {
                parserProduction.Name    = typeof(TResultNode).Name;
                parserProduction.Binding = ConstructorParserProductionBinding.Bind <TResultNode>();

                Productions.Add(parserProduction);
            }
示例#2
0
            internal ParserProductionCollectionBuilder(ParserProduction parserEntry)
            {
                _collection = null;
                _entry0     = parserEntry;
                _entry1     = null;
                _entry2     = null;
                _entry3     = null;

                EntryCount = 1;
            }
示例#3
0
            public void BindReturn <TResultNode, TBaseNode>(Grammar <TToken, TBaseNode> .ParserRule <TResultNode> rule) where TResultNode : TBaseNode where TBaseNode : class
            {
                var transition = new ParserProduction(new ParserEntry[] { rule })
                {
                    Name    = typeof(TResultNode).Name,
                    Binding = ConstructorParserProductionBinding.Bind <TResultNode>(),
                    Unwrap  = true
                };

                Productions.Add(transition);
            }
 private static void FlattenEntry(Entry entry, ParserProduction owner, List <ParserEntryData> flatEntries)
 {
     while (true)
     {
         if (entry is ParserStateEntry parserStateEntry && parserStateEntry.Fragment)
         {
             foreach (var parserTransition in parserStateEntry.State.Productions)
             {
                 FlattenProduction(owner, parserTransition, flatEntries);
             }
         }
                public ProductionInstanceBuilderPool(ParserProduction production)
                {
                    _production = production;
                    _poolArray  = new ProductionInstanceBuilder[16];

                    for (var i = 0; i < _poolArray.Length; i++)
                    {
                        _poolArray[i] = new ProductionInstanceBuilder(_production, this);
                    }

                    _tail = _poolArray.Length - 1;
                }
                public SyntaxFactoryProductionBinder(ParserProduction parserProduction)
                {
                    var flatEntries = parserProduction.FlatEntries;
                    var binding     = (Grammar <TToken> .SyntaxFactoryParserProductionBinding)parserProduction.GrammarParserProduction.Binding;
                    var nodeType    = binding.NodeType;

                    Template = new ArgumentBuilder[flatEntries.Count];

                    //var lambdaExpression = ((LambdaExpression) binding.Expression);
                    //var methodCallExpression = (MethodCallExpression) lambdaExpression.Body;
                    //var methodInfo = methodCallExpression.Method;
                    var methodInfo = binding.Method;
                    var parameters = methodInfo.GetParameters();
                    var arguments  = new List <int>();

                    foreach (var parameterInfo in parameters)
                    {
                        if (typeof(SyntaxFactory).IsAssignableFrom(parameterInfo.ParameterType))
                        {
                            continue;
                        }

                        var flatEntry = parserProduction.GetEntry(parameterInfo.Name);

                        if (flatEntry == null)
                        {
                            throw new InvalidOperationException(
                                      $"GrammarTransition '{parserProduction.GrammarParserProduction}' node type '{nodeType.Name}' factory has argument '{parameterInfo.Name}' without corresponding transition entry.");
                        }

                        var argumentBuilder = CreateArgumentBuilder(parameterInfo.ParameterType, parameterInfo, flatEntry);

                        Template[flatEntry.FlatIndex] = argumentBuilder;

                        arguments.Add(flatEntry.FlatIndex);
                    }

                    Arguments     = arguments.ToArray();
                    FactoryTarget = binding.MethodTarget;
                    FactoryInfo   = methodInfo;
                }
示例#7
0
            private void Add(ParserProduction parserEntry)
            {
                if (EntryCount >= Capacity)
                {
                    _collection.Add(parserEntry);

                    EntryCount++;

                    return;
                }

                if (EntryCount == 0)
                {
                    _entry0 = parserEntry;
                }
                else if (EntryCount == 1)
                {
                    _entry1 = parserEntry;
                }
                else if (EntryCount == 2)
                {
                    _entry2 = parserEntry;
                }
                else if (EntryCount == 3)
                {
                    _entry3 = parserEntry;
                }

                EntryCount++;

                if (EntryCount == Capacity)
                {
                    _collection = new List <ParserProduction>(Capacity);

                    for (var i = 0; i < EntryCount; i++)
                    {
                        _collection.Add(this[i]);
                    }
                }
            }
                public ProductionInstanceBuilder(ParserProduction parserProduction, ProductionInstanceBuilderPool pool)
                {
                    var binder = parserProduction.Binder;

                    _pool      = pool;
                    _tryReturn = binder.TryReturn;

                    if (binder.ConstValue != null)
                    {
                        Arguments = Array.Empty <Argument>();

                        _constValue             = binder.ConstValue;
                        _createInstanceDelegate = (builder, factory) => builder._constValue;
                    }
                    else
                    {
                        var template = binder.Template;

                        Arguments        = new Argument[template.Length];
                        ArgumentBuilders = binder.Template;

                        for (var index = 0; index < template.Length; index++)
                        {
                            var argumentBuilder = template[index];

                            if (argumentBuilder == null)
                            {
                                continue;
                            }

                            Arguments[index] = argumentBuilder.CreateArgument(this, index);
                        }

                        var flatIndex = parserProduction.LeftRecursionEntry?.ParserEntryData.FlatIndex ?? -1;

                        LeftRecursionArgument = flatIndex != -1 ? Arguments[flatIndex] : null;

                        _createInstanceDelegate = binder.CreateInstanceDelegate;
                    }
                }
示例#9
0
            private static LeftRecursionClassifier Classify(ParserProduction production, ParserState parserState)
            {
                if (production.Entries.Length == 0)
                {
                    return(LeftRecursionClassifier.Primary);
                }

                var firstRecursion = IsRecursion(production.Entries[0], parserState);
                var lastRecursion  = IsRecursion(production.Entries[production.Entries.Length - 1], parserState);

                if (production.Entries.Length == 1)
                {
                    return(firstRecursion ? LeftRecursionClassifier.Generic : LeftRecursionClassifier.Primary);
                }

                if (production.Entries.Length == 3 && IsRecursion(production.Entries[1], parserState) == false && firstRecursion && lastRecursion)
                {
                    return(LeftRecursionClassifier.Binary);
                }

                if (production.Entries.Length > 3 && firstRecursion && lastRecursion)
                {
                    return(LeftRecursionClassifier.Ternary);
                }

                if (firstRecursion && lastRecursion == false)
                {
                    return(LeftRecursionClassifier.Suffix);
                }

                if (firstRecursion == false && lastRecursion)
                {
                    return(LeftRecursionClassifier.Prefix);
                }

                return(firstRecursion ? LeftRecursionClassifier.Generic : LeftRecursionClassifier.Primary);
            }
            public void BindFactory <TArg1, TArg2, TResult>(SyntaxBindFunc <TSyntaxFactory, TArg1, TArg2, TResult> expression, ParserProduction parserProduction)
            {
                parserProduction.Binding = SyntaxFactoryParserProductionBinding.Bind <TNode, TSyntaxFactory>(expression);

                Productions.Add(parserProduction);
            }
            public void BindFactory <TArg1, TArg2, TArg3, TArg4, TArg5, TArg6, TArg7, TArg8, TArg9, TArg10, TArg11, TArg12, TArg13, TArg14, TArg15, TArg16, TArg17, TArg18, TArg19, TArg20, TArg21, TArg22, TArg23, TArg24, TArg25, TArg26, TArg27, TArg28, TArg29, TArg30, TArg31, TArg32, TResult>(SyntaxBindFunc <TSyntaxFactory, TArg1, TArg2, TArg3, TArg4, TArg5, TArg6, TArg7, TArg8, TArg9, TArg10, TArg11, TArg12, TArg13, TArg14, TArg15, TArg16, TArg17, TArg18, TArg19, TArg20, TArg21, TArg22, TArg23, TArg24, TArg25, TArg26, TArg27, TArg28, TArg29, TArg30, TArg31, TArg32, TResult> expression, ParserProduction parserProduction)
            {
                parserProduction.Binding = SyntaxFactoryParserProductionBinding.Bind <TNode, TSyntaxFactory>(expression);

                Productions.Add(parserProduction);
            }
                public CtorParserProductionBinder(ParserProduction parserProduction)
                {
                    var flatEntries = parserProduction.FlatEntries;
                    var unwrap      = parserProduction.GrammarParserProduction.Unwrap;
                    var binding     = (Grammar <TToken> .ConstructorParserProductionBinding)parserProduction.GrammarParserProduction.Binding;
                    var nodeType    = binding.NodeType;

                    Template = new ArgumentBuilder[flatEntries.Count];

                    if (unwrap)
                    {
                        Return      = true;
                        Template[0] = CreateArgumentBuilder(nodeType, null, flatEntries[0]);
                        Arguments   = new[] { 0 };

                        return;
                    }

                    if (nodeType == null)
                    {
                        return;
                    }

                    TryReturn = flatEntries.Count > 0 && ((flatEntries[0].ParserEntry as ParserStateEntry)?.TryReturn ?? false);

                    var ctor = nodeType.GetConstructors().SingleOrDefault();

                    if (ctor == null)
                    {
                        var instanceProperty = nodeType.GetProperty("Instance", BindingFlags.Static | BindingFlags.Public);
                        var instanceField    = nodeType.GetField("Instance", BindingFlags.Static | BindingFlags.Public);

                        if (instanceProperty != null && instanceProperty.PropertyType == nodeType)
                        {
                            ConstValue = instanceProperty.GetValue(null);
                        }
                        else if (instanceField != null && instanceField.FieldType == nodeType)
                        {
                            ConstValue = instanceField.GetValue(null);
                        }
                        else
                        {
                            throw new InvalidOperationException("No public ctor or singleton accessor");
                        }

                        return;
                    }

                    var parameters = ctor.GetParameters();
                    var arguments  = new List <int>();

                    foreach (var parameterInfo in parameters)
                    {
                        var flatEntry = parserProduction.GetEntry(parameterInfo.Name);

                        if (flatEntry == null)
                        {
                            throw new InvalidOperationException(
                                      $"GrammarTransition '{parserProduction.GrammarParserProduction}' node type '{nodeType.Name}' ctor has argument '{parameterInfo.Name}' without corresponding transition entry.");
                        }

                        var argumentBuilder = CreateArgumentBuilder(parameterInfo.ParameterType, parameterInfo, flatEntry);

                        Template[flatEntry.FlatIndex] = argumentBuilder;

                        arguments.Add(flatEntry.FlatIndex);
                    }

                    Arguments       = arguments.ToArray();
                    ConstructorInfo = ctor;
                }
示例#13
0
 public void AddProduction(ParserProduction parserProduction)
 {
     Productions.Add(parserProduction);
 }