myTinySQL v0.2.0-2-g5000098
A tiny SQL interpreter over fixed-width text files
Loading...
Searching...
No Matches
Classes | Enumerations | Functions | Variables
Parser.cpp File Reference

The tokenizer and recursive-descent parser for the SQL dialect. More...

#include <format>
#include <string_view>
#include <utility>
#include "Statement.hpp"
#include "Util.hpp"

Go to the source code of this file.

Classes

struct  tinysql::anonymous_namespace{Parser.cpp}::Token
 One token of a statement. More...
 
class  tinysql::anonymous_namespace{Parser.cpp}::Parser
 A recursive-descent parser: one method per statement type, reading the tokens left to right. More...
 

Enumerations

enum class  tinysql::anonymous_namespace{Parser.cpp}::TokenKind { tinysql::anonymous_namespace{Parser.cpp}::Word , tinysql::anonymous_namespace{Parser.cpp}::String , tinysql::anonymous_namespace{Parser.cpp}::Symbol , tinysql::anonymous_namespace{Parser.cpp}::End }
 The kinds of token the tokenizer produces. More...
 

Functions

bool tinysql::anonymous_namespace{Parser.cpp}::isWordChar (char c)
 Tells whether a character can stand next to an apostrophe inside a word.
 
bool tinysql::anonymous_namespace{Parser.cpp}::isApostrophe (std::string_view text, std::size_t i)
 Tells whether the ' at a position is an apostrophe inside a word, as in d'Asti, L'Aquila or dell'800.
 
std::vector< Token > tinysql::anonymous_namespace{Parser.cpp}::tokenize (std::string_view sql)
 Splits a statement into tokens.
 
std::string tinysql::anonymous_namespace{Parser.cpp}::describe (const Token &token)
 Describes a token for an error message.
 
bool tinysql::anonymous_namespace{Parser.cpp}::isComparison (std::string_view op)
 Tells whether a symbol is a comparison operator.
 
bool tinysql::anonymous_namespace{Parser.cpp}::isComment (std::string_view sql)
 Tells whether a line is a comment or empty.
 
Statement tinysql::parseStatement (std::string_view sql)
 Parses one statement.
 
std::vector< std::string > tinysql::splitStatements (std::string_view script)
 Splits a script into statements.
 

Variables

constexpr std::string_view tinysql::anonymous_namespace{Parser.cpp}::kDelimiters = " \t\r\n(),*;@=<>!'"
 The characters that end a word (a ' only when it is not an apostrophe, see isApostrophe()).
 

Detailed Description

The tokenizer and recursive-descent parser for the SQL dialect.

The grammar:

SELECT <item>[,<item>...] FROM <table> [WHERE <col> <op> <value>]
INSERT INTO <table> VALUES (<value>[,<value>...])
UPDATE <table> SET <col> = <value>[,<col> = <value>...] WHERE <col> <op> <value>
DELETE FROM <table> WHERE <col> <op> <value>
CREATE TABLE <table> (<col> CHAR(<width>)|NUMBER(<width>)[,...])
COLUMN <col> FORMAT A<width>
DESCRIBE [<table>]
REM <comment>    -- <comment>

<op> is one of = != <> < <= > >= or LIKE (with % and _ wildcards). Keywords are case-insensitive. A trailing ; or @ is ignored.

A ' between two letters or digits is an apostrophe, part of the word, as in d'Asti or L'Aquila: it never starts or ends a quoted string. '' inside quotes still stands for one quote.

Definition in file Parser.cpp.

Function Documentation

◆ parseStatement()

Statement tinysql::parseStatement ( std::string_view  sql)

Parses one statement.

Parameters
sqlThe text of one statement; a trailing ; is allowed.
Returns
The parsed statement.
Exceptions
SqlErrorOn a syntax error; the message says what was expected and what was found.

Definition at line 509 of file Parser.cpp.

◆ splitStatements()

std::vector< std::string > tinysql::splitStatements ( std::string_view  script)

Splits a script into statements.

Parameters
scriptAny number of statements: one per line, and ; outside quotes also ends one.
Returns
The statements in order, trimmed; comment lines and empty pieces are left out.

Definition at line 517 of file Parser.cpp.