예제 #1
0
        private static MNode lexInput(MInputStream input)
        {
            ByteAutomata ba = new ByteAutomata();

            automata = ba;
            defineTransitions(ba);
            ba.next((byte)1); // set first state
            root         = new MNode(null, 0, "<ROOT>");
            currentExpr  = root;
            currentBlock = null;
            currentToken = null;
            lastStart    = 0;
            ba.run(input);
            ba.step((byte)' '); // ended cleanly: last white space
            if (currentBlock != null)
            {
                MeanCS.print("closing parenthesis missing at the end");
                ba.ok = false;
            }
            if (!ba.ok)
            {
                ba.printError();
                root = null;
                return(null);
            }
            MeanCS.verbose("\nFINISHED!");
            return(root);
        }
예제 #2
0
 static internal void printArgInfo()
 {
     MeanCS.print("Command line arguments:");
     MeanCS.print("    [cmd] -i            read standard input, eg. [cmd] -i < ../test_script.txt");
     MeanCS.print("    [cmd] -t            run default test");
     MeanCS.print("    [cmd] \"[string]\"    parse [string]");
 }
예제 #3
0
        private static void endBlock()
        {
            MeanCS.assertion((currentBlock != null), null, "unexpected block end");
            byte inputByte = automata.getInputByte();

            // Check that block-end character is the right one.
            // The 'type' is block start/end character's ASCII code.
            if (currentBlock.type == NODE_PARENTHESIS)
            {
                MeanCS.assertion(inputByte == ')', null, "invalid block end; parenthesis was expected");
            }
            else if (currentBlock.type == NODE_SQUARE_BRACKETS)
            {
                MeanCS.assertion(inputByte == ']', null, "invalid block end; square bracket was expected");
            }
            else if (currentBlock.type == NODE_CURLY_BRACKETS)
            {
                MeanCS.assertion(inputByte == '}', null, "invalid block end; curly bracket was expected");
            }
            else
            {
                MeanCS.assertion(false, null, "unhandled block end: " + inputByte);
            }
            lastStart    = -1;
            currentToken = currentBlock;
            currentExpr  = currentToken.parent;
            currentBlock = currentExpr.parent;
        }
예제 #4
0
 public void run(MInputStream input)
 {
     inputByte    = 0;
     index        = 0;
     lineNumber   = 1;
     stayNextStep = false;
     running      = true;
     while ((!input.end() || stayNextStep) && running && ok)
     {
         if (!stayNextStep)
         {
             index++;
             inputByte = input.readByte();
             buffer[index % BUFFER_SIZE] = inputByte;
             if (inputByte == '\n')
             {
                 lineNumber++;
             }
         }
         else
         {
             stayNextStep = false;
         }
         MeanCS.printn("[ ").print((char)(inputByte)).print(" ]").print("\n");
         running = step(inputByte);
     }
     if (!stayNextStep)
     {
         index++;
     }
 }
예제 #5
0
 public void printTree(bool deep)
 {
     printTree(this, 0, deep);
     if (!deep)
     {
         MeanCS.verbose("");
     }
 }
예제 #6
0
 public void readArray(int [] trg, int numInts)
 {
     MeanCS.assertion(numInts <= (getByteCount() * 4) + 1, null, "readArray: buffer overflow");
     for (int i = 0; i < numInts; i++)
     {
         trg[i] = readInt();
     }
     System.Diagnostics.Debug.Assert(end(), "all bytes not read");
 }
예제 #7
0
 public void next(byte nextState)
 {
     currentState = nextState;
     { if (MeanCS.debug)
       {
           MeanCS.verbosen("Next state: ").print(stateNames[(int)currentState]).print("\n");
       }
     };
 }
예제 #8
0
        public string getString(int start, int length)
        {
            MeanCS.assertion(length < CFG_MAX_NAME_LENGTH, null, "name is too long");
            int i = 0;

            for (; i < length; i++)
            {
                tmp[i] = buffer[start++ % BUFFER_SIZE];
            }
            return(System.Text.Encoding.UTF8.GetString(tmp, 0, length));
        }
예제 #9
0
    public static void Main(string [] args)
    {
        try
        {
            MicroLexer.printTitle("Java");

            if (args.Length == 1)
            {
                MNode root = null;
                if (args[0].Equals("-i"))
                {
                    MicroLexer.printStdinInfo();
                    string input = "";
                    while (true)
                    {
                        string line = Console.ReadLine();
                        if (line == null)
                        {
                            break;
                        }
                        input += line + '\n';
                    }
                    root = MicroLexer.lex(input);
                }
                else if (args[0].Equals("-t"))
                {
                    root = MicroLexer.lex("abc 123 (def 456 [ghi])");
                }
                else
                {
                    root = MicroLexer.lex(args[0]);
                }
                MeanCS.print("TOKEN TREE:");
                if (root != null)
                {
                    root.printTree(true);
                }
            }
            else
            {
                MicroLexer.printArgInfo();
            }
        }
        catch (Exception e)
        {
            MeanCS.print(e.ToString());
        }
    }
예제 #10
0
        public void printError()
        {
            MeanCS.printn("ERROR: parser state [").print(stateNames[(int)currentState]).print("]").print("\n");
            MeanCS.printn("Line ").print(lineNumber).print(": \"");
            // print nearby code
            int start = index - 1;

            while (start > 0 && index - start < BUFFER_SIZE && (char)buffer[start % BUFFER_SIZE] != '\n')
            {
                start--;
            }
            while (++start < index)
            {
                MeanCS.verbosen((char)(buffer[start % BUFFER_SIZE]));
            }
            MeanCS.print("\"");
        }
예제 #11
0
        private static void addToken(int tokenType)
        {
            string data = automata.getString(lastStart, automata.getIndex() - lastStart);

            MeanCS.verbosen("NEW TOKEN: ").print(data).print("\n");
            MNode token = new MNode(currentExpr, tokenType, data);

            if (currentToken == null)
            {
                currentExpr.child = token;
            }
            else
            {
                currentToken.next = token;
            }
            currentExpr.numChildren++;
            currentToken = token;
            lastStart    = automata.getIndex();
        }
예제 #12
0
 public void print()
 {
     for (int i = 0; i <= stateCounter; i++)
     {
         MeanCS.verbosen("state: ").print(i).print("\n");
         for (int n = 0; n < 256; n++)
         {
             byte foo = tr[(i * 256) + n];
             if (foo == 0xff)
             {
                 MeanCS.verbosen(".");
             }
             else
             {
                 MeanCS.verbosen(foo);
             }
         }
         MeanCS.verbose("");
     }
 }
예제 #13
0
        private static void addBlock()
        {
            byte inputByte = automata.getInputByte();
            int  blockType = 0;

            if (inputByte == '(')
            {
                blockType = NODE_PARENTHESIS;
            }
            else if (inputByte == '[')
            {
                blockType = NODE_SQUARE_BRACKETS;
            }
            else if (inputByte == '{')
            {
                blockType = NODE_CURLY_BRACKETS;
            }
            else
            {
                MeanCS.assertion(false, null, "unhandled block start: " + inputByte);
            }
            lastStart = -1;
            MNode block = new MNode(currentExpr, blockType, "<BLOCK>");

            if (currentToken == null)
            {
                currentExpr.child = block;
            }
            else
            {
                currentToken.next = block;
            }
            currentExpr.numChildren++;
            currentBlock = block;
            MNode expr = new MNode(currentBlock, 0, "<EXPR>");

            currentBlock.child = expr;
            currentExpr        = expr;
            currentToken       = null;
        }
예제 #14
0
        public void printTree(MNode _node, int depth, bool deep)
        {
            System.Diagnostics.Debug.Assert(_node != null, "<printTree: empty node>");
            MNode node = _node;

            for (int i = 0; i < depth; i++)
            {
                MeanCS.verbosen("  ");
            }
            MeanCS.verbosen("[").print(node.data).print("]");
            // if (node.numChildren > 0) { VR(" + ")X(node.numChildren); }
            if (deep)
            {
                MeanCS.verbose("");
            }
            if (node.child != null && deep)
            {
                printTree(node.child, depth + 1, deep);
            }
            if (node.next != null)
            {
                printTree(node.next, depth, deep);
            }
        }
예제 #15
0
 static internal void printTitle(string s)
 {
     MeanCS.print("\n    ByteAutomata (c) 2020, Meanwhale");
     MeanCS.print("    https://github.com/Meanwhale/ByteAutomata\n");
 }
예제 #16
0
 public byte readByte()
 {
     MeanCS.assertion(!end(), null, "readInt: buffer overflow");
     return(buffer[index++]);
 }
예제 #17
0
 static internal void printStdinInfo()
 {
     MeanCS.print("Hit Ctrl-Z (Windows) or ^D (Linux/Mac) at the start of a line and press enter to finish.");
     MeanCS.print("Read from standard input...");
 }