Beispiel #1
0
        static void CopyFramePart(string stop)
        {
            char startCh         = stop[0];
            int  endOfStopString = stop.Length - 1;
            int  ch = fram.ReadByte();

            while (ch != EOF)
            {
                if (ch == startCh)
                {
                    int i = 0;
                    do
                    {
                        if (i == endOfStopString)
                        {
                            return;                   // stop[0..i] found
                        }
                        ch = fram.ReadByte();
                        i++;
                    }while (ch == stop[i]);
                    // stop[0..i-1] found; continue with last read character
                    gen.Write(stop.Substring(0, i));
                }
                else
                {
                    gen.Write((char)ch);
                    ch = fram.ReadByte();
                }
            }
            Errors.Exception(" -- incomplete or corrupt scanner frame file");
        }
Beispiel #2
0
        public static void Init(String dir)
        {
            FileStream s;

            try {
                s     = new FileStream(dir + FILENAME, FileMode.Create);      /* AW use FILENAME */
                trace = new StreamWriter(s);
            } catch (IOException) {
                Errors.Exception("-- could not open trace file");
            }
        }
Beispiel #3
0
        public static void WriteScanner()
        {
            int i, j;

            int[]  startTab = new int[CharClass.charSetSize];
            string dir      = System.Environment.CurrentDirectory;
            string fr       = Path.Combine(dir, "Scanner.frame");

            if (!File.Exists(fr))
            {
                string frameDir = Environment.GetEnvironmentVariable("crframes");
                if (frameDir != null)
                {
                    fr = Path.Combine(frameDir.Trim(), "Scanner.frame");
                }
                if (!File.Exists(fr))
                {
                    Errors.Exception("-- Cannot find Scanner.frame");
                }
            }
            try
            {
                fram = new FileStream(fr, FileMode.Open, FileAccess.Read, FileShare.Read);
            }
            catch (FileNotFoundException)
            {
                Errors.Exception("-- Cannot open Scanner.frame.");
            }
            try
            {
                string fn = dir + "\\Scanner.cs";
                if (File.Exists(fn))
                {
                    File.Copy(fn, fn + ".old", true);
                }
                FileStream s = new FileStream(fn, FileMode.Create);
                gen = new StreamWriter(s);
            }
            catch (IOException)
            {
                Errors.Exception("-- Cannot generate scanner file.");
            }
            if (dirtyDFA)
            {
                MakeDeterministic();
            }
            FillStartTab(startTab);

            CopyFramePart("-->namespace");
            /* AW add namespace, if it exists */
            if (Tab.nsName != null && Tab.nsName.Length > 0)
            {
                gen.Write("namespace ");
                gen.Write(Tab.nsName);
                gen.Write(" {");
            }
            CopyFramePart("-->constants");
            gen.WriteLine("\tconst int maxT = {0};", Symbol.terminals.Count - 1);
            CopyFramePart("-->declarations");
            gen.WriteLine("\tconst int noSym = {0};", Tab.noSym.n);
            gen.WriteLine("\tstatic short[] start = {");
            for (i = 0; i < CharClass.charSetSize / 16; i++)
            {
                gen.Write("\t");
                for (j = 0; j < 16; j++)
                {
                    gen.Write("{0,3},", startTab[16 * i + j]);
                }
                gen.WriteLine();
            }
            gen.WriteLine("\t  0};");
            CopyFramePart("-->initialization");
            gen.WriteLine("\t\tignore = new BitArray({0});", CharClass.charSetSize);
            gen.Write("\t\t");
            if (Tab.ignored == null)
            {
                gen.Write("ignore[' '] = true;");
            }
            else
            {
                j = 0;
                for (i = 0; i < Tab.ignored.Count; i++)
                {
                    if (Tab.ignored[i])
                    {
                        gen.Write("ignore[{0}] = true; ", i);
                        if (++j % 4 == 0)
                        {
                            gen.WriteLine();
                            gen.Write("\t\t");
                        }
                    }
                }
            }
            CopyFramePart("-->comment");
            Comment com = Comment.first;

            i = 0;
            while (com != null)
            {
                GenComment(com, i);
                com = com.next;
                i++;
            }
            CopyFramePart("-->literals");
            GenLiterals();
            CopyFramePart("-->scan1");
            if (Comment.first != null)
            {
                gen.Write("\t\tif (");
                com = Comment.first;
                i   = 0;
                while (com != null)
                {
                    gen.Write(ChCond(com.start[0]));
                    gen.Write(" && Comment{0}()", i);
                    if (com.next != null)
                    {
                        gen.Write(" ||");
                    }
                    com = com.next;
                    i++;
                }
                gen.Write(") return NextToken();");
            }
            if (hasCtxMoves)
            {
                gen.WriteLine("\t\tint apx = 0;");
            }
            CopyFramePart("-->scan2");
            for (State state = firstState.next; state != null; state = state.next)
            {
                WriteState(state);
            }
            gen.Write("\t\t\tcase " + (State.lastNr + 1) + ": {t.kind = 0; goto done;}");
            CopyFramePart("$$$");
            /* AW 12-20-02 close namespace, if it exists */
            if (Tab.nsName != null && Tab.nsName.Length > 0)
            {
                gen.Write("}");
            }
            gen.Close();
        }
        public static void WriteParser()
        {
            FileStream s;

            symSet.Add(Tab.allSyncSets);
            string fr = srcDir + "Parser.frame";

            if (!File.Exists(fr))
            {
                string frameDir = Environment.GetEnvironmentVariable("crframes");
                if (frameDir != null)
                {
                    fr = frameDir.Trim() + "\\Parser.frame";
                }
                if (!File.Exists(fr))
                {
                    Errors.Exception("-- Cannot find Parser.frame");
                }
            }
            try
            {
                fram = new FileStream(fr, FileMode.Open, FileAccess.Read, FileShare.Read);
            }
            catch (IOException)
            {
                Errors.Exception("-- Cannot open Parser.frame.");
            }
            try
            {
                string fn = srcDir + "Parser.cs";
                if (File.Exists(fn))
                {
                    File.Copy(fn, fn.Replace(".cs", ".old.cs"), true);
                }
                s   = new FileStream(fn, FileMode.Create);
                gen = new StreamWriter(s);
            }
            catch (IOException)
            {
                Errors.Exception("-- Cannot generate parser file");
            }
            err = new StringWriter();
            foreach (Symbol sym in Symbol.terminals)
            {
                GenErrorMsg(tErr, sym);
            }
            if (usingPos != null)
            {
                CopySourcePart(usingPos, 0);
            }
            gen.WriteLine();
            CopyFramePart("-->namespace");
            /* AW open namespace, if it exists */
            if (Tab.nsName != null && Tab.nsName.Length > 0)
            {
                gen.Write("namespace ");
                gen.Write(Tab.nsName);
                gen.Write(" {");
            }
            CopyFramePart("-->tokens");
            GenTokens(); /* ML 2002/09/07 write the tokenkinds */
            CopyFramePart("-->constants");
            gen.WriteLine("\tconst int maxT = {0};", Symbol.terminals.Count - 1);
            CopyFramePart("-->declarations");
            CopySourcePart(Tab.semDeclPos, 0);
            CopyFramePart("-->pragmas");
            GenCodePragmas();
            CopyFramePart("-->productions");
            GenProductions();
            CopyFramePart("-->parseRoot");
            gen.WriteLine("\t\t{0}();", Tab.gramSy.name);
            CopyFramePart("-->errors");
            gen.Write(err.ToString());
            CopyFramePart("-->initialization");
            InitSets();
            CopyFramePart("$$$");
            /* AW 2002-12-20 close namespace, if it exists */
            if (Tab.nsName != null && Tab.nsName.Length > 0)
            {
                gen.Write("}");
            }
            gen.Close();
        }