Beispiel #1
0
        public override SyntaxTreeNode Make(ObjectStream <MyToken> input, MyDiscardDelegate <MyToken> discarder)
        {
            Ensure(input, discarder);

            // Ignora código descartável inicialmente
            input.Discard(discarder);

            if (input.EndOfStream())
            {
                return(null);
            }

            var initialPos = input.GetPosition();
            var token      = input.Next();

            if (token == null || (CalcTwoNumbersTokenClass)token.Class != (CalcTwoNumbersTokenClass)GetTokenClass())
            {
                input.SetPosition(initialPos);
                return(null);
            }

            if (string.IsNullOrEmpty((string)token.Content))
            {
                throw new SyntaxAnalysisException("Invalid content for NUMBER element");
            }

            uint number = 0;

            if (!uint.TryParse((string)token.Content, out number))
            {
                throw new SyntaxAnalysisException("Invalid number value for NUMBER element");
            }

            return(new UIntegerTreeNode(number));
        }
        public override MyToken[] Eval(ObjectStream <Char> input, MyDiscardDelegate <char> discarder)
        {
            Ensure(input, discarder);

            // Ignora código descartável inicialmente
            input.Discard(discarder);

            if (input.EndOfStream())
            {
                return(null);
            }

            var  initialPos = input.GetPosition();
            char character  = input.Next();

            if (character != _character)
            {
                input.SetPosition(initialPos);

                return(null);
            }

            var token = new MyToken(
                GetTokenClass(),
                initialPos,
                input.GetPosition(),
                null
                );

            return(new MyToken[] { token });
        }
Beispiel #3
0
        public override SyntaxTreeNode Make(ObjectStream <MyToken> input, MyDiscardDelegate <MyToken> discarder)
        {
            Ensure(input, discarder);

            // Ignora código descartável inicialmente
            input.Discard(discarder);

            if (input.EndOfStream())
            {
                return(null);
            }

            var initialPos = input.GetPosition();

            // O primeiro token é só um marcador (etiqueta) pra indicar
            // um elemento válido
            var tag = input.Next();

            if (tag == null || (CalcTwoNumbersTokenClass)tag.Class != (CalcTwoNumbersTokenClass)GetTokenClass())
            {
                input.SetPosition(initialPos);
                return(null);
            }

            var numberLeftNode  = ExpressionElements[0].Make(input, discarder);
            var plusNode        = ExpressionElements[1].Make(input, discarder);
            var numberRightNode = ExpressionElements[2].Make(input, discarder);

            if (numberLeftNode == null || plusNode == null || numberRightNode == null)
            {
                input.SetPosition(initialPos);
                return(null);
            }

            var exprTreeNode = new SyntaxTreeNode();

            plusNode.AddChildNode(numberLeftNode);
            plusNode.AddChildNode(numberRightNode);

            exprTreeNode.AddChildNode(plusNode);

            return(exprTreeNode);
        }
Beispiel #4
0
        public override MyToken[] Eval(ObjectStream <Char> input, MyDiscardDelegate <char> discarder)
        {
            Ensure(input, discarder);

            // Ignora código descartável inicialmente
            input.Discard(discarder);

            var initialPos = input.GetPosition();
            var foundChars = new List <Char>();

            while (!input.EndOfStream())
            {
                var  pos = input.GetPosition();
                char c   = input.Next();

                if (!_validChars.Contains(c))
                {
                    input.SetPosition(pos);
                    break;
                }

                foundChars.Add(c);
            }

            if (!foundChars.Any())
            {
                input.SetPosition(initialPos);

                return(null);
            }

            var token = new MyToken(
                GetTokenClass(),
                initialPos,
                input.GetPosition(),
                new string(foundChars.ToArray())
                );

            return(new MyToken[] { token });
        }
Beispiel #5
0
        public override SyntaxTreeNode Make(ObjectStream <MyToken> input, MyDiscardDelegate <MyToken> discarder)
        {
            Ensure(input, discarder);

            // Ignora código descartável inicialmente
            input.Discard(discarder);

            if (input.EndOfStream())
            {
                return(null);
            }

            var initialPos = input.GetPosition();
            var token      = input.Next();

            if (token == null || (CalcTwoNumbersTokenClass)token.Class != (CalcTwoNumbersTokenClass)GetTokenClass())
            {
                input.SetPosition(initialPos);
                return(null);
            }

            return(new SumOperatorTreeNode());
        }