示例#1
0
文件: Partitions.cs 项目: kzyg/spark
        internal void FindClasses(AAST aast)
        {
            Accumulator visitor = new Accumulator(this);

            foreach (RuleDesc rule in aast.ruleList)
            {
                RegExTree reTree = rule.Tree;
                reTree.Visit(visitor);
            }
        }
示例#2
0
文件: Partitions.cs 项目: kzyg/spark
        internal override void Op(RegExTree tree)
        {
            Leaf leaf = tree as Leaf;

            if (leaf != null)
            {
                switch (leaf.op)
                {
                case RegOp.primitive:
                    DoSingleton(leaf.chVal);
                    break;

                case RegOp.litStr:
                    for (int index = 0; ;)
                    {
                        // Use CodePoint in case string has surrogate pairs.
                        int code = CharacterUtilities.CodePoint(leaf.str, ref index);
                        if (code == -1)
                        {
                            break;
                        }
                        else
                        {
                            DoSingleton(code);
                        }
                    }
                    break;

                case RegOp.charClass:
                    DoLiteral(leaf.rangeLit);
                    break;

                case RegOp.eof:     // no action required
                    break;

                default:
                    throw new GplexInternalException("Unknown RegOp");
                }
            }
            else if (tree.op == RegOp.rightAnchor)
            {
                DoLiteral(RangeLiteral.RightAnchors);
            }
        }
示例#3
0
文件: NFSA.cs 项目: parhelia512/gplex
            /// <summary>
            /// Create a transition path in the NFSA from the given
            /// start state to the given end state, corresponding to the
            /// RegEx tree value.  The method may (almost always does)
            /// create new NFSA states and recurses to make paths for
            /// the subtrees of the given tree.
            /// </summary>
            /// <param name="tree">The tree to encode</param>
            /// <param name="start">The start state for the pattern</param>
            /// <param name="end">The end state for the pattern</param>
            internal void MakePath(RegExTree tree, NState startState, NState endState)
            {
                NState tmp1 = null;

                switch (tree.op)
                {
                case RegOp.eof:
                    break;

                // Binary nodes ===================================
                case RegOp.context:
                case RegOp.concat:
                case RegOp.alt:
                    // Binary nodes ===================================
                    Binary binNode = tree as Binary;
                    switch (tree.op)
                    {
                    case RegOp.context:
                        int rLen = binNode.rKid.contextLength();
                        int lLen = binNode.lKid.contextLength();
                        if (rLen <= 0 && lLen <= 0)
                        {
                            throw new StringInterpretException("variable right context '/' not implemented");
                        }
                        else
                        {
                            endState.rhCntx = rLen;
                            endState.lhCntx = lLen;
                            tmp1            = MkState();
                            MakePath(binNode.lKid, startState, tmp1);
                            MakePath(binNode.rKid, tmp1, endState);
                        }
                        break;

                    case RegOp.concat:
                        tmp1 = MkState();
                        MakePath(binNode.lKid, startState, tmp1);
                        MakePath(binNode.rKid, tmp1, endState);
                        break;

                    case RegOp.alt:
                        tmp1 = MkState();
                        MakePath(binNode.lKid, startState, tmp1);
                        tmp1.AddEpsTrns(endState);
                        tmp1 = MkState();
                        MakePath(binNode.rKid, startState, tmp1);
                        tmp1.AddEpsTrns(endState);
                        break;
                    }
                    break;

                // Unary nodes ===================================
                case RegOp.closure:
                case RegOp.finiteRep:
                    // Unary nodes ===================================
                    Unary unaryNode = tree as Unary;
                    switch (tree.op)
                    {
                    case RegOp.closure:
                        NState tmp2 = MkState();
                        if (unaryNode.minRep == 0)
                        {
                            tmp1 = MkState();
                            startState.AddEpsTrns(tmp1);
                        }
                        else
                        {
                            NState dummy = startState;
                            for (int i = 0; i < unaryNode.minRep; i++)
                            {
                                tmp1 = MkState();
                                MakePath(unaryNode.kid, dummy, tmp1);
                                dummy = tmp1;
                            }
                        }
                        MakePath(unaryNode.kid, tmp1, tmp2);
                        tmp2.AddEpsTrns(tmp1);
                        tmp1.AddEpsTrns(endState);
                        break;

                    case RegOp.finiteRep:
                    {
                        NState dummy = tmp1 = startState;
                        for (int i = 0; i < unaryNode.minRep; i++)
                        {
                            tmp1 = MkState();
                            MakePath(unaryNode.kid, dummy, tmp1);
                            dummy = tmp1;
                        }
                        tmp1.AddEpsTrns(endState);
                        for (int i = unaryNode.minRep; i < unaryNode.maxRep; i++)
                        {
                            tmp1 = MkState();
                            MakePath(unaryNode.kid, dummy, tmp1);
                            dummy = tmp1;
                            dummy.AddEpsTrns(endState);
                        }
                    }
                    break;
                    }
                    break;

                // Leaf nodes ===================================
                case RegOp.litStr:
                case RegOp.primitive:
                case RegOp.charClass:
                    // Leaf nodes ===================================
                    Leaf leafNode = tree as Leaf;
                    switch (tree.op)
                    {
                    case RegOp.litStr:
                    {
                        // Make a linear sequence of states with successive
                        // transitions on successive string characters.
                        //
                        string text  = leafNode.str;
                        NState dummy = startState;
                        // Need to deal with special case of empty string
                        if (text.Length == 0)
                        {
                            dummy.AddEpsTrns(endState);
                        }
                        else
                        {
                            //  This code is complicated by the fact that unicode
                            //  escape substitution may have inserted surrogate
                            //  pairs of characters in the string.  We need
                            //  one transition for every unicode codepoint,
                            //  not one for every char value in this string.
                            //
                            int index = 0;
                            int code  = CharacterUtilities.CodePoint(text, ref index);            // First character
                            int next  = CharacterUtilities.CodePoint(text, ref index);            // Next, possibly -1
                            while (next >= 0)
                            {
                                tmp1 = MkState();
                                dummy.AddChrTrns(code, tmp1);
                                dummy = tmp1;
                                code  = next;
                                next  = CharacterUtilities.CodePoint(text, ref index);
                            }
                            // Postcondition ==> "code" is the last char.
                            dummy.AddChrTrns(code, endState);
                        }
                    }
                    break;

                    case RegOp.primitive:
                        startState.AddChrTrns(leafNode.chVal, endState);
                        break;

                    case RegOp.charClass:
                        startState.AddClsTrans(leafNode, endState);
                        break;
                    }
                    break;

                default: throw new GplexInternalException("unknown tree op");
                }
            }
示例#4
0
文件: NFSA.cs 项目: parhelia512/gplex
        /// <summary>
        /// Build the NFSA from the abstract syntax tree.
        /// There is an NfsaInstance for each start state.
        /// Each rule starts with a new nfsa state, which
        /// is the target of a new epsilon transition from
        /// the real start state, nInst.Entry.
        /// </summary>
        /// <param name="ast"></param>
        public void Build(AAST ast)
        {
            int      index = 0;
            DateTime time0 = DateTime.Now;

            nfas = new NfsaInstance[ast.StartStateCount];
            foreach (KeyValuePair <string, StartState> p in ast.startStates)
            {
                StartState s    = p.Value;
                string     name = p.Key;
                if (!s.IsAll)
                {
                    NfsaInstance nInst = new NfsaInstance(s, this);
                    nfas[index++] = nInst;
                    nInst.key     = name;

                    // for each pattern do ...
                    for (int i = 0; i < s.rules.Count; i++)
                    {
                        RuleDesc  rule = s.rules[i];
                        RegExTree tree = rule.Tree;

                        // This test constructs the disjoint automata
                        // that test code points for predicate evaluation.
                        if (rule.isPredDummyRule)
                        {
                            NState entry = nInst.Entry;
                            nInst.MakePath(tree, entry, entry);
                        }
                        else
                        {
                            NState start = nInst.MkState();
                            NState endSt = nInst.MkState();

                            if (tree.op == RegOp.leftAnchor)     // this is a left anchored pattern
                            {
                                nInst.AnchorState.AddEpsTrns(start);
                                tree = ((Unary)tree).kid;
                            }
                            else                                // this is not a left anchored pattern
                            {
                                nInst.Entry.AddEpsTrns(start);
                            }
                            //
                            // Now check for right anchors, and add states as necessary.
                            //
                            if (tree.op == RegOp.eof)
                            {
                                //
                                // <<EOF>> rules are always emitted outside
                                // of the usual subset construction framework.
                                // We ensure that we do not get spurious warnings.
                                //
                                rule.useCount   = 1;
                                nInst.eofAction = rule.aSpan;
                                nInst.MakePath(tree, start, endSt);
                                nInst.MarkAccept(endSt, rule);
                            }
                            else if (tree.op == RegOp.rightAnchor)
                            {
                                tree = ((Unary)tree).kid;
                                nInst.MakePath(tree, start, endSt);
                                AddAnchorContext(nInst, endSt, rule);
                            }
                            else
                            {
                                nInst.MakePath(tree, start, endSt);
                                nInst.MarkAccept(endSt, rule);
                            }
                        }
                    }
                }
            }
            if (task.Verbose)
            {
                Console.Write("GPLEX: NFSA built");
                Console.Write((task.Errors ? ", errors detected" : " without error"));
                Console.Write((task.Warnings ? "; warnings issued. " : ". "));
                Console.WriteLine(TaskState.ElapsedTime(time0));
            }
            if (task.Summary)
            {
                WriteSummary(time0);
            }
        }
示例#5
0
文件: AAST.cs 项目: dbremner/gplex
        internal int minRep; // min repetitions for closure/finiteRep

        #endregion Fields

        #region Constructors

        internal Unary( RegOp op, RegExTree l )
            : base(op)
        {
            kid = l;
        }
示例#6
0
文件: AAST.cs 项目: dbremner/gplex
 internal Binary( RegOp op, RegExTree l, RegExTree r )
     : base(op)
 {
     lKid = l; rKid = r;
 }
示例#7
0
文件: AAST.cs 项目: dbremner/gplex
 void Check( AAST aast, RegExTree tree )
 {
     if (tree == null) return;
     switch (tree.op) {
         case RegOp.charClass:
         case RegOp.primitive:
         case RegOp.litStr:
         case RegOp.eof:
             break;
         case RegOp.context:
         case RegOp.concat:
         case RegOp.alt:
             Binary bnryTree = (Binary)tree;
             Check( aast, bnryTree.lKid );
             Check( aast, bnryTree.rKid );
             if (tree.op == RegOp.context &&
                 bnryTree.lKid.contextLength() == 0 &&
                 bnryTree.rKid.contextLength() == 0) aast.hdlr.ListError( pSpan, 75 );
             break;
         case RegOp.closure:
         case RegOp.finiteRep:
             Unary unryTree = (Unary)tree;
             Check( aast, unryTree.kid );
             break;
         case RegOp.leftAnchor:
         case RegOp.rightAnchor:
             aast.hdlr.ListError( pSpan, 69 );
             break;
     }
 }
示例#8
0
文件: AAST.cs 项目: dbremner/gplex
 // internal void Dump() { Console.WriteLine(pattern); }
 internal void ParseRE( AAST aast )
 {
     reAST = new AAST.ReParser( pattern, pSpan, aast ).Parse();
     SemanticCheck( aast );
 }
示例#9
0
文件: AAST.cs 项目: dbremner/gplex
 internal abstract void Op( RegExTree tree );
示例#10
0
文件: AAST.cs 项目: dbremner/gplex
 internal void ParseRE( AAST aast )
 {
     regX = new AAST.ReParser( verb, vrbSpan, aast ).Parse();
 }
示例#11
0
        static void Main(string[] args)
        {
            // Header
            QCol.Red("Kitty "); QCol.Magenta("Coded by: Tricky\n");
            QCol.Yellow($"(c) {MKL.CYear(2019)} Jeroen P. Broks\n\n");
            // Init
            Dirry.InitAltDrives();
            KittyHigh.Init();
            new KittyHighCS();
            new KittyHighNIL();
            new KittyHighLua();
            new KittyHighGINI();
            new KittyHighScyndi();
            new KittyBlitzMax();
            new KittyHighC();
            new KittyHighPascal();
            new KittyHighBrainFuck();
            new KittyHighGo();
            new KittyHighBlitzBasic();
            new KittyHighSASKIA();
            new KittyHighPython();
            new KittyHighJavaScript();
            new KittyHighWhiteSpace();
            new KittyHighBASIC();
            new KittyHighJava();
            new KittyHighINI();
            new KittyHighVB();
            new KittyHighCobra();
            new KittyHighHtml();
            new KittyHighXml();
            new KittyHighNeil();
            var slin = true;

            if (args.Length == 0)
            {
                QCol.Green("Kitty is a simple program which will help you view source files in syntax highlight\n");
                QCol.Magenta("Usage:\t");
                QCol.Yellow("Kitty ");
                QCol.Cyan("<switches> ");
                QCol.White("<files>");
                QCol.Green("[");
                QCol.Cyan("<switches> ");
                QCol.White("<files>");
                QCol.Green("..]");
                Console.WriteLine("\n\n");
                QCol.Yellow("Please note that switches affect all files defined after it not those that come before it. This allows you to configure each file shown\n\n");
                QCol.Cyan("-ln              "); QCol.Yellow("Toggle line numbers on/off (default is on)\n");
                QCol.Cyan("-nolinenumbers   "); QCol.Yellow("Turn line numbers off\n");
                QCol.Cyan("-Showlinenumbers "); QCol.Yellow("Turn line numbers on\n");
                QCol.Cyan("-re              "); QCol.Yellow("Toggle searching by RegEx (this allows limited support for Wild Cards and more nice things)\n");
                QCol.Cyan("-p, -more        "); QCol.Yellow("Turn \"more\" mode on/off. (Read note below)\n");
                QCol.Cyan("-support         "); QCol.Yellow("Show a list of all supported file formats\n");
                QCol.Red("\n\nThe \"more\" mode!\n");
                QCol.Yellow("Does not entirely work the same as the 'more' utility, but has the same primary function!\n");
                QCol.Yellow("When the \"more\" bar appears you can hit space to show the next line, Enter/Return to show the entire next page and escape to turn the more mode off\n");
                QCol.White("\n\nKitty can be used as as CLI tool, but the integry has been made to be included in your own projects, and has been released under the terms of the zlib license\n\n");
                return;
            }
            // Go for it
            QCol.Doing("Called from:", System.IO.Directory.GetCurrentDirectory());
            void ViewFile(string a)
            {
                try {
                    var arg = Dirry.AD(a).Replace("\\", "/");
                    QCol.Doing("Reading", arg); KittyHigh.PageBreak();
                    var src  = QuickStream.LoadString(arg);
                    var eoln = qstr.EOLNType(arg);
                    // QCol.Doing("EOLN", eoln); // didn't work anyway
                    //QCol.OriCol();
                    var       ext    = qstr.ExtractExt(arg).ToLower();
                    KittyHigh Viewer = KittyHigh.Langs["OTHER"];
                    if (KittyHigh.Langs.ContainsKey(ext))
                    {
                        Viewer = KittyHigh.Langs[ext];
                    }
                    QCol.Doing("Type", Viewer.Language); KittyHigh.PageBreak();
                    KittyHigh.WriteLine();
                    Viewer.Show(src, slin);
                } catch (Exception ex) {
                    QCol.QuickError($"{ex.Message}\n");
#if DEBUG
                    QCol.Magenta($"{ex.StackTrace}\n\n");
#endif
                }
            }

            var aregex = false;
            foreach (string a in args)
            {
                if (qstr.Prefixed(a, "-"))
                {
                    switch (a.ToLower())
                    {
                    case "-ln": slin = !slin; break;

                    case "-nolinenumbers": slin = false; break;

                    case "-showlinenumbers": slin = true; break;

                    case "-p":
                    case "-more": KittyHigh.BrkLines = !KittyHigh.BrkLines; break;

                    case "-re": aregex = !aregex; break;

                    case "-support":
                        foreach (string ext in KittyHigh.Langs.Keys)
                        {
                            QCol.Cyan(qstr.Left($"{ext}                    ", 20));
                            QCol.Yellow($"{KittyHigh.Langs[ext].Language}\n");
                        }
                        break;

                    default: QCol.QuickError($"Unknown switch: {a}"); break;
                    }
                }
                else if (aregex)
                {
                    QCol.Doing("Searching for RegEx", a);
                    var rgxl = RegExTree.Tree(a);
                    foreach (string af in rgxl)
                    {
                        ViewFile(af);
                    }
                }
                else
                {
                    ViewFile(a);
                }
            }
            TrickyDebug.AttachWait();
        }