Example #1
0
    def parse_declaration(self):
        token = self.token_current()

        if (token.type == scanner.TK_VAR):
            self.token_accept(scanner.TK_VAR)
            token = self.token_current()

            if (token.type == scanner.TK_IDENTIFIER):
                self.token_accept(scanner.TK_IDENTIFIER)
                self.token_accept(scanner.TK_COLON)
                e1 = self.parse_type_denoter()
                return ast.VarDeclaration(token.val, e1)

            else:
                raise ParserError(self.curtoken.pos, self.curtoken.type)

        if (token.type == scanner.TK_CONST):

            self.token_accept_any()
            token = self.token_current()

            if (token.type == scanner.TK_IDENTIFIER):
                self.token_accept_any()
                self.token_accept(scanner.TK_IS)
                e1 = self.parse_expr()
                return ast.ConstDeclaration(token.val, e1)

            else:
                raise ParserError(self.curtoken.pos, self.curtoken.type)

        else:  #just added
            raise ParserError(self.curtoken.pos, self.curtoken.type)
    def parse_single_declaration(self):
        """const Identifier ~ Expression ';'
        |  var Identifier : Type-denoter ';'
        |  func Identifier '(' parameter-list ')' ':'' Type-denoter single-Command
        """

        # Constant Declaration
        if self.curtoken.type == scanner.TK_CONST:
            self.token_accept_any()
            name = self.token_current().val
            self.token_accept(scanner.TK_IDENTIFIER)
            self.token_accept(scanner.TK_IS)
            expr = self.parse_expression()
            self.token_accept(scanner.TK_SEMICOLON)
            return ast.ConstDeclaration(name, expr)
        # Variable Declaration
        elif self.curtoken.type == scanner.TK_VAR:
            self.token_accept_any()
            name = self.token_current().val
            self.token_accept(scanner.TK_IDENTIFIER)
            self.token_accept(scanner.TK_COLON)
            type_d = ast.TypeDenoter(self.token_current().val)
            self.token_accept(scanner.TK_IDENTIFIER)
            self.token_accept(scanner.TK_SEMICOLON)
            return ast.VarDeclaration(name, type_d)
        # Function Declaration
        elif self.curtoken.type == scanner.TK_FUNCDEF:
            return self.parse_function_declaration()
        # Unexpected tokens
        else:
            raise ParserException(self.curtoken.pos, self.curtoken.type)
Example #3
0
    def parse_const_declaration(self):
        self.token_accept_any()
        token = self.token_current()

        if (token.type == scanner.TK_IDENTIFIER):
            self.token_accept_any()
            self.token_accept(scanner.TK_IS)
            e1 = self.parse_expr()
            return ast.ConstDeclaration(token.val, e1)

        else:
            raise ParserError(self.curtoken.pos, self.curtoken.type)
Example #4
0
    def parse_single_declaration(self):
        """ single-Declaration ::=  const Identifier ~ Expression
                                |   var Identifier : Type-denoter
                                |   func Identifier '(' Identifier ':' Type-denoter ',*)' ':' Type-denoter single-Command
        """

        token = self.token_current()
        if token.type == scanner.TK_CONST:
            self.token_accept_any()
            token = self.token_current()
            self.token_accept(scanner.TK_IDENTIFIER)
            ident = token.val
            self.token_accept(scanner.TK_IS)
            expr = self.parse_expression()
            decl = ast.ConstDeclaration(ident, expr)
        elif token.type == scanner.TK_VAR:
            self.token_accept_any()
            token = self.token_current()
            self.token_accept(scanner.TK_IDENTIFIER)
            ident = token.val
            self.token_accept(scanner.TK_COLON)
            type_denoter = self.parse_type_denoter()
            decl = ast.VarDeclaration(ident, type_denoter)
        elif token.type == scanner.TK_FUNCDEF:
            self.token_accept_any()
            tk_ident = self.token_current()
            self.token_accept(scanner.TK_IDENTIFIER)
            self.token_accept(scanner.TK_LPAREN)
            if self.token_current != scanner.TK_RPAREN:
                argrs = self.parse_argr()
            else:
                argrs = ''

            self.token_accept(scanner.TK_RPAREN)
            self.token_accept(scanner.TK_COLON)
            func_type = self.parse_type_denoter()
            command = self.parse_single_command()
            #decl = ast.FunctionDeclaration(tk_ident.val, tk_argident.val, arg_type, func_type, command)
            decl = ast.FunctionDeclaration(tk_ident.val, argrs, func_type,
                                           command)
        else:
            raise ParserError(self.curtoken.pos, self.curtoken.type)

        return decl
Example #5
0
    def parse_secdeclaration(self):
        """
        sec-Declaration ::=  const Identifier ~ Expression 
                            |   var Identifier : Identifier                      
        """
        token = self.token_current()
        if token.type == scanner.TK_CONST:
            self.token_accept_any()
            ident = self.token_current().val

            self.token_accept(scanner.TK_IDENTIFIER)
            self.token_accept(scanner.TK_IS)
            expr = self.parse_expr()
            return ast.ConstDeclaration(ident, expr)
        elif token.type == scanner.TK_VAR:
            self.token_accept_any()
            ident = self.token_current().val
            self.token_accept(scanner.TK_IDENTIFIER)
            self.token_accept(scanner.TK_COLON)
            type_denoter = self.parse_typedenoter()
            return ast.VarDeclaration(ident, type_denoter)
        else:
            raise ParserError(self.curtoken.pos, self.curtoken.val,
                              self.curtoken.type)