myTinySQL v0.2.0-2-g5000098
A tiny SQL interpreter over fixed-width text files
Loading...
Searching...
No Matches
Public Member Functions | Private Member Functions | Private Attributes | List of all members
tinysql::anonymous_namespace{Parser.cpp}::Parser Class Reference

A recursive-descent parser: one method per statement type, reading the tokens left to right. More...

Public Member Functions

 Parser (std::vector< Token > tokens)
 Prepares to parse a statement.
 
Statement parse ()
 Parses the whole statement, choosing the method from its first word.
 

Private Member Functions

const Token & peek () const
 Looks at the next token without consuming it.
 
Token next ()
 Consumes the next token (the End token is never consumed).
 
void fail (std::string_view expected) const
 Reports a syntax error at the current token.
 
bool acceptKeyword (std::string_view keyword)
 Consumes the next token if it is a given keyword.
 
void expectKeyword (std::string_view keyword)
 Consumes a keyword that must come next.
 
bool acceptSymbol (std::string_view symbol)
 Consumes the next token if it is a given symbol.
 
void expectSymbol (std::string_view symbol)
 Consumes a symbol that must come next.
 
std::string expectWord (std::string_view what)
 Consumes a word (a name) that must come next.
 
std::string expectValue ()
 Consumes a value that must come next: a word or a quoted string.
 
void expectEnd ()
 Checks that the statement ends here, after any trailing ; or @.
 
Condition parseCondition ()
 Parses <column> <op> <value>, after the WHERE keyword; <op> is a comparison or LIKE.
 
SelectStmt parseSelect ()
 Parses SELECT <items> FROM <table> [WHERE ...].
 
InsertStmt parseInsert ()
 Parses INSERT INTO <table> VALUES (...). A value is a quoted string, or one or more bare words joined by spaces.
 
UpdateStmt parseUpdate ()
 Parses UPDATE <table> SET <col> = <value>, ... WHERE ....
 
DeleteStmt parseDelete ()
 Parses DELETE FROM <table> WHERE ....
 
CreateTableStmt parseCreateTable ()
 Parses CREATE TABLE <table> (<col> <type>(<width>), ...), where <type> is CHAR or NUMBER.
 
ColumnFormatStmt parseColumn ()
 Parses COLUMN <col> FORMAT A<width>.
 
DescribeStmt parseDescribe ()
 Parses DESCRIBE [<table>].
 

Private Attributes

std::vector< Token > tokens_
 The tokens, ending with TokenKind::End.
 
std::size_t pos_ = 0
 The position of the next token to read.
 

Detailed Description

A recursive-descent parser: one method per statement type, reading the tokens left to right.

Definition at line 165 of file Parser.cpp.

Constructor & Destructor Documentation

◆ Parser()

tinysql::anonymous_namespace{Parser.cpp}::Parser::Parser ( std::vector< Token >  tokens)
inlineexplicit

Prepares to parse a statement.

Parameters
tokensThe statement's tokens, from tokenize().

Definition at line 169 of file Parser.cpp.

Member Function Documentation

◆ acceptKeyword()

bool tinysql::anonymous_namespace{Parser.cpp}::Parser::acceptKeyword ( std::string_view  keyword)
inlineprivate

Consumes the next token if it is a given keyword.

Parameters
keywordThe keyword (case-insensitive).
Returns
True if it was there and was consumed.

Definition at line 214 of file Parser.cpp.

◆ acceptSymbol()

bool tinysql::anonymous_namespace{Parser.cpp}::Parser::acceptSymbol ( std::string_view  symbol)
inlineprivate

Consumes the next token if it is a given symbol.

Parameters
symbolThe symbol, e.g. ",".
Returns
True if it was there and was consumed.

Definition at line 236 of file Parser.cpp.

◆ expectEnd()

void tinysql::anonymous_namespace{Parser.cpp}::Parser::expectEnd ( )
inlineprivate

Checks that the statement ends here, after any trailing ; or @.

Exceptions
SqlErrorIf more tokens follow.

Definition at line 280 of file Parser.cpp.

◆ expectKeyword()

void tinysql::anonymous_namespace{Parser.cpp}::Parser::expectKeyword ( std::string_view  keyword)
inlineprivate

Consumes a keyword that must come next.

Parameters
keywordThe keyword (case-insensitive).
Exceptions
SqlErrorIf the next token is something else.

Definition at line 226 of file Parser.cpp.

◆ expectSymbol()

void tinysql::anonymous_namespace{Parser.cpp}::Parser::expectSymbol ( std::string_view  symbol)
inlineprivate

Consumes a symbol that must come next.

Parameters
symbolThe symbol, e.g. "(".
Exceptions
SqlErrorIf the next token is something else.

Definition at line 248 of file Parser.cpp.

◆ expectValue()

std::string tinysql::anonymous_namespace{Parser.cpp}::Parser::expectValue ( )
inlineprivate

Consumes a value that must come next: a word or a quoted string.

Returns
The value's text.
Exceptions
SqlErrorIf the next token is neither.

Definition at line 270 of file Parser.cpp.

◆ expectWord()

std::string tinysql::anonymous_namespace{Parser.cpp}::Parser::expectWord ( std::string_view  what)
inlineprivate

Consumes a word (a name) that must come next.

Parameters
whatWhat the word is, for the error message, e.g. "a column name".
Returns
The word.
Exceptions
SqlErrorIf the next token is not a word.

Definition at line 259 of file Parser.cpp.

◆ fail()

void tinysql::anonymous_namespace{Parser.cpp}::Parser::fail ( std::string_view  expected) const
inlineprivate

Reports a syntax error at the current token.

Parameters
expectedWhat should have come, e.g. "a table name".
Exceptions
SqlErrorAlways: "expected ... but found ...".

Definition at line 206 of file Parser.cpp.

◆ next()

Token tinysql::anonymous_namespace{Parser.cpp}::Parser::next ( )
inlineprivate

Consumes the next token (the End token is never consumed).

Returns
The token.

Definition at line 201 of file Parser.cpp.

◆ parse()

Statement tinysql::anonymous_namespace{Parser.cpp}::Parser::parse ( )
inline

Parses the whole statement, choosing the method from its first word.

Returns
The parsed statement.
Exceptions
SqlErrorOn an unknown command or a syntax error.

Definition at line 174 of file Parser.cpp.

◆ parseColumn()

ColumnFormatStmt tinysql::anonymous_namespace{Parser.cpp}::Parser::parseColumn ( )
inlineprivate

Parses COLUMN <col> FORMAT A<width>.

Returns
The ColumnFormatStmt.
Exceptions
SqlErrorOn a syntax error.

Definition at line 463 of file Parser.cpp.

◆ parseCondition()

Condition tinysql::anonymous_namespace{Parser.cpp}::Parser::parseCondition ( )
inlineprivate

Parses <column> <op> <value>, after the WHERE keyword; <op> is a comparison or LIKE.

Returns
The condition.
Exceptions
SqlErrorOn a syntax error.

Definition at line 292 of file Parser.cpp.

◆ parseCreateTable()

CreateTableStmt tinysql::anonymous_namespace{Parser.cpp}::Parser::parseCreateTable ( )
inlineprivate

Parses CREATE TABLE <table> (<col> <type>(<width>), ...), where <type> is CHAR or NUMBER.

Returns
The CreateTableStmt.
Exceptions
SqlErrorOn a syntax error, an unknown type, or a width that is not a positive integer.

Definition at line 425 of file Parser.cpp.

◆ parseDelete()

DeleteStmt tinysql::anonymous_namespace{Parser.cpp}::Parser::parseDelete ( )
inlineprivate

Parses DELETE FROM <table> WHERE ....

Returns
The DeleteStmt.
Exceptions
SqlErrorOn a syntax error.

Definition at line 408 of file Parser.cpp.

◆ parseDescribe()

DescribeStmt tinysql::anonymous_namespace{Parser.cpp}::Parser::parseDescribe ( )
inlineprivate

Parses DESCRIBE [<table>].

Returns
The DescribeStmt.
Exceptions
SqlErrorOn a syntax error.

Definition at line 482 of file Parser.cpp.

◆ parseInsert()

InsertStmt tinysql::anonymous_namespace{Parser.cpp}::Parser::parseInsert ( )
inlineprivate

Parses INSERT INTO <table> VALUES (...). A value is a quoted string, or one or more bare words joined by spaces.

Returns
The InsertStmt.
Exceptions
SqlErrorOn a syntax error.

Definition at line 349 of file Parser.cpp.

◆ parseSelect()

SelectStmt tinysql::anonymous_namespace{Parser.cpp}::Parser::parseSelect ( )
inlineprivate

Parses SELECT <items> FROM <table> [WHERE ...].

Returns
The SelectStmt.
Exceptions
SqlErrorOn a syntax error.

Definition at line 321 of file Parser.cpp.

◆ parseUpdate()

UpdateStmt tinysql::anonymous_namespace{Parser.cpp}::Parser::parseUpdate ( )
inlineprivate

Parses UPDATE <table> SET <col> = <value>, ... WHERE ....

Returns
The UpdateStmt.
Exceptions
SqlErrorOn a syntax error.

Definition at line 380 of file Parser.cpp.

◆ peek()

const Token & tinysql::anonymous_namespace{Parser.cpp}::Parser::peek ( ) const
inlineprivate

Looks at the next token without consuming it.

Returns
The next token.

Definition at line 198 of file Parser.cpp.

Member Data Documentation

◆ pos_

std::size_t tinysql::anonymous_namespace{Parser.cpp}::Parser::pos_ = 0
private

The position of the next token to read.

Definition at line 194 of file Parser.cpp.

◆ tokens_

std::vector<Token> tinysql::anonymous_namespace{Parser.cpp}::Parser::tokens_
private

The tokens, ending with TokenKind::End.

Definition at line 192 of file Parser.cpp.


The documentation for this class was generated from the following file: