Exemple #1
0
        TokenTable.Token ExpectedToken(uint tokenCode)
        {
            CheckUnexpectedEnd();

            if (GetTokenCode() != tokenCode)
            {
                throw new Exception($"Expected \"{TokenTable.GetLexemeName(tokenCode)}\" but found \"{TokenTable.GetLexemeName(GetTokenCode())}\" in line {tokenListNode.Value.Line}.");
            }

            var token = tokenListNode.Value;

            TransitToNextToken();
            return(token);
        }
Exemple #2
0
        TokenTable.Token ExpectedOneOfTokens(string expected, params uint[] tokenCodes)
        {
            CheckUnexpectedEnd();

            if (tokenCodes.All(tokenCode => GetTokenCode() != tokenCode))
            {
                throw new Exception($"Expected {expected} but found \"{TokenTable.GetLexemeName(GetTokenCode())}\" in line {tokenListNode.Value.Line}.");
            }

            var token = tokenListNode.Value;

            TransitToNextToken();
            return(token);
        }
        private CustomTree DeclarationStatement()
        {
            CustomTree node = new CustomTree("declaration");

            //"int" | "float"
            var token = ExpectedOneOfTokens("TypeSpecifier", (uint)TokenTable.Code.Int, 100);

            node.AddChild(new CustomTree(TokenTable.GetLexemeName(token.Code)));
            // identifier
            token = ExpectedToken((uint)TokenTable.Code.Id);
            node.AddChild(new CustomTree("identifier", token.ForeignId));

            ExpectedToken((uint)TokenTable.Code.Semicolon); // ';'

            return(node);
        }
Exemple #4
0
        TokenTable.Token ExpectLogicalOp()
        {
            CheckUnexpectedEnd();

            if (!TokenTable.LogicOpCodes.Contains(GetTokenCode()))
            {
                throw new Exception($"Expected \"{String.Join(", ", TokenTable.LogicOpCodes)}\" but found \"{TokenTable.GetLexemeName(GetTokenCode())}\" in line {tokenListNode.Value.Line}.");
            }

            var token = tokenListNode.Value;

            TransitToNextToken();
            return(token);
        }