Exemplo n.º 1
0
        static string Str(Node p)
        {
            StringBuilder s = new StringBuilder();

            while (p != null)
            {
                if (p.typ == Node.chr)
                {
                    s.Append((char)p.val);
                }
                else if (p.typ == Node.clas)
                {
                    BitArray set = CharClass.Set(p.val);
                    if (Sets.Elements(set) != 1)
                    {
                        Parser.SemErr("character set contains more than 1 character");
                    }
                    s.Append((char)Sets.First(set));
                }
                else
                {
                    Parser.SemErr("comment delimiters may not be structured");
                }
                p = p.next;
            }
            if (s.Length == 0 || s.Length > 2)
            {
                Parser.SemErr("comment delimiters must be 1 or 2 characters long");
                s = new StringBuilder("?");
            }
            return(s.ToString());
        }
Exemplo n.º 2
0
        private static bool Overlap(Action a, Action b)
        {
            BitArray seta, setb;

            if (a.typ == Node.chr)
            {
                if (b.typ == Node.chr)
                {
                    return(a.sym == b.sym);
                }
                else
                {
                    setb = CharClass.Set(b.sym);
                    return(setb[a.sym]);
                }
            }
            else
            {
                seta = CharClass.Set(a.sym);
                if (b.typ == Node.chr)
                {
                    return(seta[b.sym]);
                }
                else
                {
                    setb = CharClass.Set(b.sym);
                    return(Sets.Intersect(seta, setb));
                }
            }
        }
Exemplo n.º 3
0
        public BitArray Symbols()
        {
            BitArray s;

            if (typ == Node.clas)
            {
                s = (BitArray)CharClass.Set(sym).Clone();
            }
            else
            {
                s = new BitArray(CharClass.charSetSize); s[sym] = true;
            }
            return(s);
        }
Exemplo n.º 4
0
        public Action TheAction(char ch)
        {
            BitArray s;

            for (Action a = firstAction; a != null; a = a.next)
            {
                if (a.typ == Node.chr && ch == a.sym)
                {
                    return(a);
                }
                else if (a.typ == Node.clas)
                {
                    s = CharClass.Set(a.sym);
                    if (s[ch])
                    {
                        return(a);
                    }
                }
            }
            return(null);
        }
Exemplo n.º 5
0
 static void FillStartTab(int[] startTab)
 {
     startTab[0] = State.lastNr + 1; // eof
     for (Action action = firstState.firstAction; action != null; action = action.next)
     {
         int targetState = action.target.state.nr;
         if (action.typ == Node.chr)
         {
             startTab[action.sym] = targetState;
         }
         else
         {
             BitArray s = CharClass.Set(action.sym);
             for (int i = 0; i < s.Count; i++)
             {
                 if (s[i])
                 {
                     startTab[i] = targetState;
                 }
             }
         }
     }
 }
Exemplo n.º 6
0
        static void WriteState(State state)
        {
            Symbol endOf = state.endOf;

            gen.WriteLine("\t\t\tcase {0}:", state.nr);
            bool ctxEnd = state.ctx;

            for (Action action = state.firstAction; action != null; action = action.next)
            {
                if (action == state.firstAction)
                {
                    gen.Write("\t\t\t\tif (");
                }
                else
                {
                    gen.Write("\t\t\t\telse if (");
                }
                if (action.typ == Node.chr)
                {
                    gen.Write(ChCond((char)action.sym));
                }
                else
                {
                    PutRange(CharClass.Set(action.sym));
                }
                gen.Write(") {");
                if (action.tc == Node.contextTrans)
                {
                    gen.Write("apx++; ");
                    ctxEnd = false;
                }
                else if (state.ctx)
                {
                    gen.Write("apx = 0; ");
                }
                gen.Write("buf.Append(ch); NextCh(); goto case {0};", action.target.state.nr);
                gen.WriteLine("}");
            }
            if (state.firstAction == null)
            {
                gen.Write("\t\t\t\t{");
            }
            else
            {
                gen.Write("\t\t\t\telse {");
            }
            if (ctxEnd) // final context state: cut appendix
            {
                gen.WriteLine();
                gen.WriteLine("\t\t\t\t\tbuf.Length = buf.Length - apx;");
                gen.WriteLine("\t\t\t\t\tpos = pos - apx - 1; line = t.line;");
                gen.WriteLine("\t\t\t\t\tBuffer.Pos = pos+1; NextCh();");
                gen.Write("\t\t\t\t\t");
            }
            if (endOf == null)
            {
                gen.WriteLine("t.kind = noSym; goto done;}");
            }
            else
            {
                gen.Write("t.kind = {0}; ", endOf.n);
                if (endOf.tokenKind == Symbol.classLitToken)
                {
                    gen.WriteLine("t.val = buf.ToString(); CheckLiteral(); return t;}");
                }
                else
                {
                    gen.WriteLine("goto done;}");
                }
            }
        }