Exemplo n.º 1
0
 public void WriteData(Dfa dfa, Generator generator)
 {
     generator.Indent();
     generator.WriteLine("private static readonly byte[] _charToClassCompressed = ");
     generator.WriteCompressedArray(dfa.MaxClass, dfa.MatchToClass);
     generator.UnIndent();
 }
Exemplo n.º 2
0
        public DfaTableWriter(Dfa dfa,
                              ITableWriterFactory tableWriterFactory,
                              ISyntaxErrorHandler syntaxErrorHandler)
        {
            this.dfa    = dfa ?? throw new ArgumentNullException(nameof(dfa));
            classWriter = tableWriterFactory.CreateClassWriter();
            var tableFactory = syntaxErrorHandler.CreateStateTable(dfa);

            stateTableWriter = tableWriterFactory.CreateStateWriter(dfa, tableFactory);
        }
Exemplo n.º 3
0
        public void Write(Generator generator, Dfa dfa)
        {
            generator.WriteLine("public class TokenStates")
            .WriteLine("{")
            .Indent();

            Write(generator, dfa.AcceptingStates);
            generator.UnIndent()
            .WriteLine("}");
        }
Exemplo n.º 4
0
 public ScannerWriter(Nfa.Graph graph,
                      Dfa.Dfa dfa,
                      IDfaWriter scannerStatesWriter,
                      TokenStateWriterBase tokenStatesWriter)
 {
     symbols            = graph.Machine.AcceptingStates.Symbols.ToArray();
     this.dfa           = dfa;
     this.scannerStates = scannerStatesWriter;
     this.tokenStates   = tokenStatesWriter;
 }
Exemplo n.º 5
0
        public void WriteData(Dfa dfa, Generator generator)
        {
            //(char, int)[] charClasses =
            //{
            //    ('a', 0), ('b', 1)
            //};

            //Write map
            generator.Indent();
            generator.WriteLine("private static readonly (char ch, int cls)[] _charToClass = ");
            generator.StartBlock();

            var first  = true;
            int column = 0;

            for (var i = 0; i < dfa.MatchToClass.Length; i++)
            {
                var state = dfa.MatchToClass[i];
                if (state == 0)
                {
                    continue;
                }
                if (first)
                {
                    first = false;
                }
                else
                {
                    generator.Write(", ");
                }

                if (column == 8)
                {
                    generator.WriteLine();
                    column = 0;
                }
                column++;

                generator.Write("('")
                .WriteEscChar(i)
                .Write("', ")
                .Write(state)
                .Write(")");
            }
            generator.WriteLine();
            generator.EndBlock(";");
            generator.UnIndent();
        }
Exemplo n.º 6
0
 public CompressStateWriter(Dfa dfa, ScannerStateTable tableFactory)
 {
     this.dfa          = dfa;
     this.tableFactory = tableFactory;
 }
Exemplo n.º 7
0
 public void WriteInit(Dfa dfa, Generator generator) =>
 generator.Write("Opal.CharClasses.ToArray(_charToClass)");
Exemplo n.º 8
0
 public void WriteInit(Dfa dfa, Generator generator)
 {
     generator.Write("CharClasses.")
     .Write(Dfa.GetMethod("Decompress", dfa.MaxClass))
     .Write("(_charToClassCompressed)");
 }
Exemplo n.º 9
0
        public bool Compile()
        {
            var fileName = Path.GetFileName(inPath);
            var fileDir  = Path.GetDirectoryName(fileName);

            parser = Parser.FromString(args.InputContents);
            parser.SetOptions(options);
            args.LogMessage(Importance.Normal, "Parsing {0}", fileName);
            var isOk = parser.Parse();

            if (!isOk)
            {
                return(isOk);
            }

            var lang = parser.Language;

            if (lang == null)
            {
                return(false);
            }
            prods = lang.Productions;

            var nfa = parser.Graph;

            if (!prods.SetStates(args, parser.Graph.Machine.AcceptingStates))
            {
                return(false);
            }

            if (options.TryGetValue("nfa", out var nfaPath))
            {
                if (string.IsNullOrEmpty(nfaPath))
                {
                    nfaPath = Path.ChangeExtension(inPath, ".nfa.txt");
                }
                File.WriteAllText(nfaPath, nfa.ToString());
            }

            args.LogMessage(Importance.Normal, "Building dfa");
            dfa = nfa.ToDfa();

            scannerWriter = (!options.TryGetValue("scanner", out var scannerValue) ||
                             scannerValue.Equals("state", StringComparison.InvariantCultureIgnoreCase)) ?
                            new DfaStateWriter(dfa) as IGeneratable :
                            new DfaSwitchWriter(dfa);


            args.LogMessage(Importance.Normal, "Building LR1");
            var grammar     = new LR1.Grammar(lang.Productions);
            var grammarPath = Path.ChangeExtension(inPath, ".grammar.txt");
            var grammarText = grammar.ToString();

            File.WriteAllText(grammarPath, grammarText);

            lr1Parser = new LR1.LR1Parser(args, grammar);

            var statesPath = Path.ChangeExtension(inPath, ".states.txt");

            File.WriteAllText(statesPath, lr1Parser.States.ToString());


            using (var csharp = new Generator(args.Output))
            {
                if (!string.IsNullOrEmpty(args.DefaultNamespace))
                {
                    lang.Namespace = new ParseTree.Identifier(args.DefaultNamespace);
                }

                var frameSpecified = false;
                if (options.TryGetValue("frame", out var frameFile) && !string.IsNullOrEmpty(frameFile))
                {
                    frameSpecified = File.Exists(frameFile);
                    if (!frameSpecified)
                    {
                        frameFile      = Path.Combine(fileDir, frameFile);
                        frameSpecified = File.Exists(frameFile);
                        if (!frameSpecified)
                        {
                            args.LogWarning("Unable to find frame file {0}", frameFile);
                        }
                    }
                }
                if (frameSpecified)
                {
                    args.LogMessage(Importance.Normal, "Using frame file {0}", frameFile);
                    //args.LogMessage(Importance.Normal, "Writing output {0}", OutPath);
                    TemplateProcessor.FromFile(csharp, this, frameFile);
                }
                else
                {
                    args.LogMessage(Importance.Normal, "Using internal frame file");
                    //args.LogMessage(Importance.Normal, "Writing output {0}", OutPath);
                    TemplateProcessor.FromAssembly(csharp, this, "Opal.FrameFiles.Parser.txt");
                }
            }

            return(isOk);
        }
Exemplo n.º 10
0
 public DfaSwitchWriter(Dfa dfa, ISyntaxErrorHandler syntaxErrorHandler)
 {
     this.dfa = dfa;
     states   = syntaxErrorHandler.GetStates(dfa);
 }
Exemplo n.º 11
0
 public IEnumerable <DfaNode> GetStates(Dfa dfa) =>
 dfa.States;
Exemplo n.º 12
0
 public ScannerStateTable CreateStateTable(Dfa dfa) =>
 new ScannerStateTable(dfa.States);
Exemplo n.º 13
0
 public IEnumerable <DfaNode> GetStates(Dfa dfa) =>
 dfa.States.AddSyntaxError();
Exemplo n.º 14
0
 public ScannerStateTable CreateStateTable(Dfa dfa) =>
 new ScannerStateTableWithSyntaxErrors(dfa.States);