Parser
source: bluebase/lp/parser.py
- class LpParser(tokens: list[LpToken])[source]
Bases:
objectA 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:
LpUnexpectedEndOfInputError – if there are no more tokens
LpUnexpectedTokenError – if the token tag does not match
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
WORDwith the given keyword and advance on success.Hint
Use
peek.
- assignmentexpect_keyword(keyword: str) None[source]
Consume a
WORDmatching 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
WORDfor the table name. If the"AS"keyword matches, consume theWORDfor the alias.
- assignmentparse_tables() list[AstTable][source]
Parse a
tables.Grammar
tables : table | tables COMMA table
Hint
Use
parse_tableto parse eachtable. Whenever aCOMMAis matched, parse anothertable.
- assignmentparse_attrs() list[AstAttr][source]
Parse a
attrs.Grammar
attrs : attr | attrs COMMA attr
Hint
Use
parse_attrto parse eachattr. Whenever aCOMMAis matched, parse anotherattr.
- 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_valueto parse eachvalue. Whenever aCOMMAis matched, parse anothervalue.
- 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 anattr. Otherwise, parse avalue.
- assignmentparse_cond() AstCond[source]
Parse a
cond.Grammar
cond : attr COMP hand
Hint
First, parse the left
attr, then consume aCOMP, and then parse the righthand.
- assignmentparse_conds() list[AstCond][source]
Parse a
conds.Grammar
conds : cond | conds "AND" cond
Hint
Use
parse_condto parse eachcond. Whenever a"AND"is matched, parse anothercond.
- 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 aCOMP, and then parse the righthand. 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_assignto parse eachassign. Whenever aCOMMAis matched, parse anotherassign.
- 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
WORDand return the domain based on its value. If the token value is"STRING", consume aLP, then consume anINT, and then consume aRPand return aStringDomainwith the length.
- assignmentparse_define() AstDefine[source]
Parse a
define.Grammar
define : attr domain
Hint
First, parse the
attr, then parse thedomain.
- assignmentparse_defines() list[AstDefine][source]
Parse a
defines.Grammar
defines : define | defines COMMA define
Hint
Use
parse_defineto parse eachdefine. Whenever aCOMMAis matched, parse anotherdefine.
- assignmentparse_where_clause() list[AstCond][source]
Parse a
where_clause.Grammar
where_clause : | "WHERE" conds
Hint
If the
"WHERE"keyword matches, parse theconds. 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
SCat 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
SCat 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
SCat 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
SCat 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 aWORDand match the statement. Don’t forget to consume aSCat 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 aWORDand match the statement. Don’t forget to consume aSCat 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
SCat 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
SCat 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
SCat the end.
- assignmentparse_exit_statement() AstExit[source]
Parse a
exit_statement.Grammar
exit_statement : "EXIT" SC
Hint
Don’t forget to consume a
SCat the end.
- parse() Ast[source]
Parse the input tokens into an
AST.- Raises:
LpUnexpectedEndOfInputError – if there are no more tokens
LpUnexpectedTokenError – if the first token is not a
WORDLpUnknownStatementError – if the statement is unknown