Implementation of the default parser of module ALib Expressions.
This internal class is not too well documented. Nevertheless, it is designed to be able to tweak its behavior slightly and in case of need, even derive and use a custom parser class. For doing so, please consult the source code of this class. A custom parser might be set to protected field Compiler::parserby a derived compiler type before compiling a first expression.
It is possible to define scannable custom unary and binary operators. Definitions of binary operators include a "precedence value" that allows aligning them with the built-in types. Also, built-in operators can be removed if wanted.
Definition at line 40 of file parser_impl.hpp.
#include <parser_impl.hpp>
Public Method Index: | |
ParserImpl (Compiler &compiler, MonoAllocator &allocator) | |
virtual | ~ParserImpl () override |
Virtual destructor. | |
virtual ALIB_API detail::AST * | Parse (const String &exprString, NumberFormat *nf) override |
Public Method Index: inherited from Parser | |
virtual | ~Parser () |
Virtual destructor. | |
Protected Type Index: | |
enum class | Tokens : char { EOT = '\0' , SymbolicOp = 'O' , AlphaUnOp = 'U' , AlphaBinOp = 'B' , LitString = 'S' , LitInteger = 'I' , LitFloat = 'F' , Identifier = 'A' , BraceOpen = '(' , BraceClose = ')' , Comma = ',' , SubscriptOpen = '[' , SubscriptClose = ']' } |
Types of tokens. More... | |
Protected Field Index: | |
StdVectorMono< AST * > * | ASTs |
HashSet< MonoAllocator, String, alib::hash_string_ignore_case< character >, alib::equal_to_string_ignore_case< character > > | binaryOperators |
Compiler & | compiler |
The compiler that this parser works for. | |
MonoAllocator & | compileTimeAllocator |
String | expression |
The given expression to parse. | |
NumberFormat * | numberFormat |
Used for scanning literals. Provided to this class with each parse request. | |
std::bitset< 256 > | operatorChars |
Substring | scanner |
The rest of expression. | |
std::bitset< 256 > | syntaxTokens |
Tokens | token |
The actual token type. | |
double | tokFloat |
Float value of token (if applicable). | |
integer | tokInteger |
Integer value of token (if applicable). | |
ASTLiteral::NFHint | tokLiteralHint |
The actual token type. | |
integer | tokPosition |
The position of the token in expression. | |
String | tokString |
String value of token (if applicable). | |
HashSet< MonoAllocator, String, alib::hash_string_ignore_case< character >, alib::equal_to_string_ignore_case< character > > | unaryOperators |
Protected Method Index: | |
ALIB_API String | getBinaryOp () |
ALIB_API String | getUnaryOp () |
void | NextToken () |
This is the "scanner" or "lexer" method. | |
AST * | parseBinary () |
AST * | parseConditional () |
AST * | parseSimple () |
AST * | parseSubscript (AST *function) |
AST * | pop () |
AST * | push (AST *ast) |
AST * | replace (AST *ast) |
AST * | top () |
Additional Inherited Members | |
Public Static Method Index: inherited from Parser | |
static ALIB_API Parser * | Create (Compiler &compiler) |
|
strongprotected |
Types of tokens.
Definition at line 44 of file parser_impl.hpp.
|
protected |
List of ASTs currently created in recursion.
Definition at line 138 of file parser_impl.hpp.
|
protected |
Hash set of binary operators. The key of the table is the operator string, which usually consists of one to three characters, like '+'
or '<<='
.
This table is filled in the constructor of the class with the values stored in Compiler::BinaryOperators and used for testing of existence.
Definition at line 132 of file parser_impl.hpp.
|
protected |
The compiler that this parser works for.
Definition at line 88 of file parser_impl.hpp.
|
protected |
Memory for temporary allocations, like AST objects or literal strings with converted escape sequences. Provided by the compiler with method Parse.
Definition at line 67 of file parser_impl.hpp.
|
protected |
The given expression to parse.
Definition at line 95 of file parser_impl.hpp.
|
protected |
Used for scanning literals. Provided to this class with each parse request.
Definition at line 91 of file parser_impl.hpp.
|
protected |
Lists single characters that got found in operator strings which have been registered with Compiler::AddUnaryOperator and Compiler::AddBinaryOperator.
Used by the internal token scanner (lexer) and by default will become something like "=+-*%/?:~!|&^!<>/%"
. when found in the expression string.
Definition at line 112 of file parser_impl.hpp.
|
protected |
The rest of expression.
Definition at line 98 of file parser_impl.hpp.
|
protected |
Lists single characters that get directly converted into tokens of corresponding type when found in the expression string. Tokens are "()[],"
.
Definition at line 102 of file parser_impl.hpp.
|
protected |
The actual token type.
Definition at line 70 of file parser_impl.hpp.
|
protected |
Float value of token (if applicable).
Definition at line 79 of file parser_impl.hpp.
|
protected |
Integer value of token (if applicable).
Definition at line 76 of file parser_impl.hpp.
|
protected |
The actual token type.
Definition at line 73 of file parser_impl.hpp.
|
protected |
The position of the token in expression.
Definition at line 85 of file parser_impl.hpp.
|
protected |
String value of token (if applicable).
Definition at line 82 of file parser_impl.hpp.
|
protected |
Hash set of unary operators. The key of the table is the operator string, which usually consists of one character, like '-'
or '!'
.
This table is filled in the constructor of the class with the values stored in Compiler::UnaryOperators and used for testing of existence.
Definition at line 122 of file parser_impl.hpp.
ParserImpl | ( | Compiler & | compiler, |
MonoAllocator & | allocator ) |
Constructor.
compiler | The compiler that this parser works for. |
allocator | A monotonic allocator for permanent allocations. |
Definition at line 23 of file parser_impl.cpp.
|
inlineoverridevirtual |
Virtual destructor.
Definition at line 150 of file parser_impl.hpp.
|
protected |
Tests if the actual token represents a known binary operator.
Definition at line 606 of file parser_impl.cpp.
|
protected |
Tests if the actual token represents a known unary operator.
Definition at line 568 of file parser_impl.cpp.
|
protected |
This is the "scanner" or "lexer" method.
Definition at line 98 of file parser_impl.cpp.
|
overridevirtual |
Parses the given expression string.
void*
. This is to allow avoid flooding of boost
header includes files to the code entities using module ALib Expressions.exprString | The string to parse. |
nf | Used to scan number literals. |
Implements Parser.
Definition at line 308 of file parser_impl.cpp.
|
protected |
Internal method that optionally parses a binary operator and levels (recursively) trees of such according to operator precedence and brackets given.
Definition at line 373 of file parser_impl.cpp.
|
protected |
Internal method that optionally parses a conditional operator (Q ? T : F
)
Definition at line 339 of file parser_impl.cpp.
|
protected |
Parses unary ops, literals, identifiers, functions and expressions surrounded by brackets.
Definition at line 429 of file parser_impl.cpp.
Invoked after an identifier or function was parsed. Tests for subscript operator, otherwise returns the given ast as is.
function | The identifier or function parsed. |
lhs
set to function, rhs
to the parsed subscript arg and and operator set to '[]'
. Definition at line 538 of file parser_impl.cpp.
|
inlineprotected |
Simple shortcut popping and returning last ast from the current list.
Definition at line 214 of file parser_impl.hpp.
Simple shortcut pushing an ast to current list and returning it.
ast | The AST node to push. |
Definition at line 224 of file parser_impl.hpp.
Simple shortcut replacing the topmost ast.
ast | The new AST node to replace the existing one with. |
Definition at line 240 of file parser_impl.hpp.
|
inlineprotected |
Simple shortcut to the topmost AST.
Definition at line 232 of file parser_impl.hpp.