Parser

source: bluebase/lp/parser.py


class LpParser(tokens: list[LpToken])[source]

Bases: object

A parser that converts input tokens into an AST.

Variables:
  • tokens (list[LpToken]) – the input tokens

  • position (int) – the current position

assignmentpeek() → LpToken | None[source]

Return the token at the current position.

Hint

If the current position is out of bounds, return None.

assignmentconsume(tag: str | None = None) → LpToken[source]

Consume the token at the current position and advance.

If tag is specified, consume only a token with the matching tag.

Raises:

Hint

Use peek.

assignmentmatch_tag(tag: str) → bool[source]

Match the token tag at the current position and advance on success.

Hint

Use peek.

assignmentmatch_keyword(keyword: str) → bool[source]

Match a WORD with the given keyword and advance on success.

Hint

Use peek.

assignmentexpect_keyword(keyword: str) → None[source]

Consume a WORD matching the given keyword.

Raises:

LpUnexpectedKeywordError – if the token value does not match

Hint

Use consume.

assignmentparse_table() → AstTable[source]

Parse a table.

Grammar

table
    : WORD
    | WORD "AS" WORD

Hint

Consume the WORD for the table name. If the "AS" keyword matches, consume the WORD for the alias.

assignmentparse_tables() → list[AstTable][source]

Parse a tables.

Grammar

tables
    : table
    | tables COMMA table

Hint

Use parse_table to parse each table. Whenever a COMMA is matched, parse another table.

assignmentparse_attr() → AstAttr[source]

Parse a attr.

Grammar

attr
    : WORD
    | WORD DOT WORD

Hint

First, consume a WORD. If a DOT matches, consume another WORD and return an AstAttr with a table name. If there is no DOT, return an AstAttr without a table name.

assignmentparse_attrs() → list[AstAttr][source]

Parse a attrs.

Grammar

attrs
    : attr
    | attrs COMMA attr

Hint

Use parse_attr to parse each attr. Whenever a COMMA is matched, parse another attr.

assignmentparse_value() → bool | int | float | str[source]

Parse a value.

Grammar

value
    : FLOAT
    | INT
    | BOOL
    | STRING
Raises:

LpUnexpectedTokenError – if the token tag is invalid

Hint

Consume a token and return the value based on its tag. Note that when parsing a STRING, strip the surrounding quotes.

assignmentparse_values() → tuple[bool | int | float | str, ...][source]

Parse a values.

Grammar

values
    : value
    | values COMMA value

Hint

Use parse_value to parse each value. Whenever a COMMA is matched, parse another value.

assignmentparse_hand() → AstAttr | bool | int | float | str[source]

Parse a hand.

Grammar

hand
    : attr
    | value
Raises:

LpUnexpectedEndOfInputError – if there are no more tokens

Hint

First, peek a token. If the token is a WORD, parse an attr. Otherwise, parse a value.

assignmentparse_cond() → AstCond[source]

Parse a cond.

Grammar

cond
    : attr COMP hand

Hint

First, parse the left attr, then consume a COMP, and then parse the right hand.

assignmentparse_conds() → list[AstCond][source]

Parse a conds.

Grammar

conds
    : cond
    | conds "AND" cond

Hint

Use parse_cond to parse each cond. Whenever a "AND" is matched, parse another cond.

assignmentparse_assign() → AstAssign[source]

Parse a assign.

Grammar

assign
    : attr COMP hand
Raises:

LpUnexpectedCompError – if the comparison operator is not '='

Hint

First, parse the left attr, then consume a COMP, and then parse the right hand. If the comparison operator is not '=', raise an error.

assignmentparse_assigns() → list[AstAssign][source]

Parse a assigns.

Grammar

assigns
    : assign
    | assigns COMMA assign

Hint

Use parse_assign to parse each assign. Whenever a COMMA is matched, parse another assign.

assignmentparse_domain() → Domain[source]

Parse a domain.

Grammar

domain
    : BOOL
    | INT
    | FLOAT
    | STRING LP INT RP
Raises:

LpUnexpectedKeywordError – if the token value is not a valid domain

Hint

Consume a WORD and return the domain based on its value. If the token value is "STRING", consume a LP, then consume an INT, and then consume a RP and return a StringDomain with the length.

assignmentparse_define() → AstDefine[source]

Parse a define.

Grammar

define
    : attr domain

Hint

First, parse the attr, then parse the domain.

assignmentparse_defines() → list[AstDefine][source]

Parse a defines.

Grammar

defines
    : define
    | defines COMMA define

Hint

Use parse_define to parse each define. Whenever a COMMA is matched, parse another define.

assignmentparse_where_clause() → list[AstCond][source]

Parse a where_clause.

Grammar

where_clause
    :
    | "WHERE" conds

Hint

If the "WHERE" keyword matches, parse the conds. Otherwise, return an empty list.

assignmentparse_select_statement() → AstSelect[source]

Parse a select_statement.

Grammar

select_statement
    : "SELECT" attrs "FROM" tables where_clause SC

Hint

Don’t forget to consume a SC at the end.

assignmentparse_insert_statement() → AstInsert[source]

Parse a insert_statement.

Grammar

insert_statement
    : "INSERT" "INTO" table "VALUES" LP values RP SC

Hint

Don’t forget to consume a SC at the end.

assignmentparse_delete_statement() → AstDelete[source]

Parse a delete_statement.

Grammar

delete_statement
    : "DELETE" "FROM" table where_clause SC

Hint

Don’t forget to consume a SC at the end.

assignmentparse_update_statement() → AstUpdate[source]

Parse a update_statement.

Grammar

update_statement
    : "UPDATE" table "SET" assigns where_clause SC

Hint

Don’t forget to consume a SC at the end.

assignmentparse_create_statement() → AstCreateTable | AstCreateIndex[source]

Parse a create_statement.

Grammar

create_statement
    : "CREATE" "TABLE" create_table_body SC
    | "CREATE" "INDEX" create_index_body SC
Raises:

LpUnknownStatementError – if the statement is unknown

Hint

After matching the "CREATE" keyword, consume a WORD and match the statement. Don’t forget to consume a SC at the end.

assignmentparse_create_table_body() → AstCreateTable[source]

Parse a create_table_body.

Grammar

create_table_body
    : table LP defines RP

Hint

Note that "CREATE" and "TABLE" keywords are already consumed.

assignmentparse_create_index_body() → AstCreateIndex[source]

Parse a create_index_body.

Grammar

create_index_body
    : attr "OF" table

Hint

Note that "CREATE" and "INDEX" keywords are already consumed.

assignmentparse_drop_statement() → AstDropTable | AstDropIndex[source]

Parse a drop_statement.

Grammar

drop_statement
    : "DROP" "TABLE" drop_table_body SC
    | "DROP" "INDEX" drop_index_body SC
Raises:

LpUnknownStatementError – if the statement is unknown

Hint

After matching the "DROP" keyword, consume a WORD and match the statement. Don’t forget to consume a SC at the end.

assignmentparse_drop_table_body() → AstDropTable[source]

Parse a drop_table_body.

Grammar

drop_table_body
    : table

Hint

Note that "DROP" and "TABLE" keywords are already consumed.

assignmentparse_drop_index_body() → AstDropIndex[source]

Parse a drop_index_body.

Grammar

drop_index_body
    : attr "OF" table

Hint

Note that "DROP" and "INDEX" keywords are already consumed.

assignmentparse_show_tables_statement() → AstShowTables[source]

Parse a show_tables_statement.

Grammar

show_tables_statement
    : "SHOW" "TABLES" SC

Hint

Don’t forget to consume a SC at the end.

assignmentparse_describe_table_statement() → AstDescribeTable[source]

Parse a describe_table_statement.

Grammar

describe_table_statement
    : "DESCRIBE" "TABLE" table SC

Hint

Don’t forget to consume a SC at the end.

assignmentparse_dump_table_statement() → AstDumpTable[source]

Parse a dump_table_statement.

Grammar

dump_table_statement
    : "DUMP" "TABLE" table SC

Hint

Don’t forget to consume a SC at the end.

assignmentparse_exit_statement() → AstExit[source]

Parse a exit_statement.

Grammar

exit_statement
    : "EXIT" SC

Hint

Don’t forget to consume a SC at the end.

parse() → Ast[source]

Parse the input tokens into an AST.

Raises: