Exemplo n.º 1
0
 /// <summary>
 /// Initializes this unit
 /// </summary>
 /// <param name="grammar">The represented grammar</param>
 /// <param name="outputPath">The output path for compilation artifacts</param>
 /// <param name="compilationMode">The compilation mode to use for this</param>
 /// <param name="method">The parsing method to use</param>
 /// <param name="nmspace">The namespace for the artifacts</param>
 /// <param name="modifier">The modifier for the artifacts</param>
 public Unit(Grammar grammar, string outputPath, Mode compilationMode, ParsingMethod method, string nmspace, Modifier modifier)
 {
     this.grammar         = grammar;
     this.outputPath      = outputPath;
     this.compilationMode = compilationMode;
     this.method          = method;
     this.nmspace         = nmspace;
     this.modifier        = modifier;
 }
Exemplo n.º 2
0
        private IDisposable GetTokenizer(ParsingMethod method, Resources.FileSize fileSize)
        {
            switch (method)
            {
            case ParsingMethod.CsvHelper: return(GetCsvHelperReader(fileSize));

            case ParsingMethod.Pipeline: return(GetPipelineTokenizer(fileSize));

            case ParsingMethod.StreamReader: return(GetStreamReaderTokenizer(fileSize));

            case ParsingMethod.MemoryManaged: return(GetMemoryManagedTokenizer(fileSize));

            default: throw new InvalidOperationException();
            }
        }
Exemplo n.º 3
0
        public async Task <object> Parse(ParsingMethod method, Resources.FileSize fileSize)
        {
            switch (method)
            {
            case ParsingMethod.CsvHelper:
            {
                var reader = (CsvReader)Open(method, fileSize);

                int count = 0;

                while (await reader.ReadAsync())
                {
                    count++;
                    Debug.WriteLine(reader.ToString());
                }

                Debug.WriteLine($"Read {count} rows");

                return(reader);
            }

            default:
            {
                var tokenizer = (ITokenizer)GetTokenizer(method, fileSize);
                Debug.WriteLine($"Reading file, size = {fileSize}");

                ISection row;
                int      count = 0;
                while (true)
                {
                    row = await tokenizer.GetNextAsync();

                    if (row == null)
                    {
                        break;
                    }

                    count++;
                }

                Debug.WriteLine($"Read {count} rows");
                return(tokenizer);
            }
            }
        }
Exemplo n.º 4
0
 public IDisposable Open(ParsingMethod method, Resources.FileSize fileSize)
 {
     return(GetTokenizer(method, fileSize));
 }
Exemplo n.º 5
0
        public async Task <object> Find_Last_4MB(ParsingMethod method)
        {
            const string ToFind      = "596003.67";
            const int    ColumnIndex = 8;

            int rowNumber = 0;
            int rowCount  = 0;

            switch (method)
            {
            case ParsingMethod.CsvHelper:
            {
                var reader = (CsvReader)Open(method, Resources.FileSize.MB4);

                while (await reader.ReadAsync())
                {
                    rowCount++;

                    if (ToFind == reader.GetField(ColumnIndex))
                    {
                        rowNumber = rowCount;
                    }
                }

                if (rowCount != rowNumber)
                {
                    throw new Exception("Failing benchmark because invalid conclusion was made");
                }

                return(reader);
            }

            default:
            {
                var csv = (ITokenizer)GetTokenizer(method, Resources.FileSize.MB4);

                while (true)
                {
                    var row = await csv.GetNextAsync();

                    if (row == null)
                    {
                        break;
                    }

                    rowCount++;

                    if (ToFind.Equals(row[ColumnIndex].ToString()))
                    {
                        rowNumber = rowCount;
                    }
                }

                if (rowCount != rowNumber)
                {
                    throw new Exception("Failing benchmark because invalid conclusion was made");
                }
                return(csv);
            }
            }
        }
Exemplo n.º 6
0
        /// <summary>
        /// Build the specified grammar
        /// </summary>
        /// <param name="method">The parsing method to use</param>
        /// <returns>The resulting LR graph, or <c>null</c> if it could not be generated</returns>
        public Graph Build(ParsingMethod method)
        {
            // build the graph
            switch (method)
            {
            case ParsingMethod.LR0:
                graph = GetGraphLR0();
                break;

            case ParsingMethod.LR1:
                graph = GetGraphLR1();
                break;

            case ParsingMethod.LALR1:
                graph = GetGaphLALR1();
                break;

            case ParsingMethod.RNGLR1:
                graph = GetGraphRNGLR1();
                break;

            case ParsingMethod.RNGLALR1:
                graph = GetGraphRNGLALR1();
                break;
            }

            // builds the set of conflicts
            inverse = new GraphInverse(graph);
            foreach (State state in graph.States)
            {
                if (state.Conflicts.Count != 0)
                {
                    List <Phrase> samples = inverse.GetInputsFor(state);
                    foreach (Conflict conflict in state.Conflicts)
                    {
                        foreach (Phrase sample in samples)
                        {
                            Phrase temp = new Phrase(sample);
                            temp.Append(conflict.ConflictSymbol);
                            conflict.AddExample(temp);
                        }
                        conflicts.Add(conflict);
                    }
                }
                List <List <State> > paths = null;
                foreach (Symbol symbol in state.Transitions)
                {
                    Terminal terminal = symbol as Terminal;
                    if (terminal == null)
                    {
                        continue;
                    }
                    if (terminal.Context == 0)
                    {
                        continue;
                    }
                    // this is a contextual terminal, can we reach this state without the right context being available
                    if (paths == null)
                    {
                        paths = inverse.GetStatePathsTo(state);
                    }
                    foreach (List <State> path in paths)
                    {
                        path.Add(state);                         // append this state
                        bool found = false;
                        for (int i = 0; i != path.Count - 1; i++)
                        {
                            State element = path[i];
                            foreach (Item item in element.Items)
                            {
                                if (item.DotPosition == 0 && item.BaseRule.Context == terminal.Context)
                                {
                                    // this is the opening of a context only if we are not going to the next state using the associated variable
                                    found |= !state.HasTransition(item.BaseRule.Head) || state.GetChildBy(item.BaseRule.Head) != path[i + 1];
                                    break;
                                }
                            }
                            if (found)
                            {
                                break;
                            }
                        }
                        foreach (Item item in state.Items)
                        {
                            if (item.DotPosition == 0 && item.BaseRule.Context == terminal.Context)
                            {
                                found = true;
                                break;
                            }
                        }
                        if (!found)
                        {
                            // this is problematic path
                            ContextualError error = new ContextualError(state);
                            foreach (Item item in state.Items)
                            {
                                if (item.Action == LRActionCode.Shift && item.GetNextSymbol() == terminal)
                                {
                                    error.AddItem(item);
                                }
                            }
                            errors.Add(error);
                        }
                    }
                }
            }
            return(graph);
        }