coan
6.0.1
A C/C++ Configuration Analyzer
|
Encapsulates parsing of preprocessor expressions. More...
#include <expression_parser.h>
Public Types | |
enum | deletion_code { not_deleted, non_paren = 1, paren } |
Codes for logical deletion status. More... | |
enum | precedence { max = 10, ternary_either_or = 10, ternary_if = 10, comma = 9, boolean_or = 8, boolean_and = 7, bit_or = 6, bit_xor = 6, bit_and = 6, eq = 5, neq = 5, le = 4, ge = 4, lt = 4, gt = 4, lshift = 3, rshift = 3, add = 2, subtract = 2, mult = 1, divide = 1, mod = 1 } |
Codes for operator precedences. | |
using | sequence_type = CharSeq |
Type of character-sequence containing the parsed expression. | |
Public Member Functions | |
expression_parser (chewer< sequence_type > &chew, reference const *ref=nullptr) | |
Construct given a chewer<sequence_type> More... | |
expression_parser (sequence_type &seq, reference const *ref=nullptr, size_t start=0) | |
Construct to evaluate a sequence_type More... | |
evaluation | result () const |
Get the result of parsing the expression. More... | |
operator evaluation () const | |
Cast the object to an evaluation . | |
std::string | simplified () const |
Get the most simplified form of the expression. More... | |
bool | is_simplified () const |
Say whether the expression has been simplified. | |
Private Types | |
using | infix_operation = evaluation(expression_parser::*)(evaluation const &, evaluation const &) |
Type of an infix operation. | |
Private Member Functions | |
evaluation | op_lt (evaluation const &lhs, evaluation const &rhs) |
Less-than operator. | |
evaluation | op_gt (evaluation const &lhs, evaluation const &rhs) |
Greater-than operator. | |
evaluation | op_le (evaluation const &lhs, evaluation const &rhs) |
Less-than-or-equal operator. | |
evaluation | op_ge (evaluation const &lhs, evaluation const &rhs) |
Greater-than-or-equal operator. | |
evaluation | op_eq (evaluation const &lhs, evaluation const &rhs) |
Equality operator. | |
evaluation | op_ne (evaluation const &lhs, evaluation const &rhs) |
Inequality operator. | |
evaluation | op_or (evaluation const &lhs, evaluation const &rhs) |
Inclusive-or operator. | |
evaluation | op_and (evaluation const &lhs, evaluation const &rhs) |
And operator. | |
evaluation | op_ternary_if (evaluation const &lhs, evaluation const &rhs) |
The '?' "operator". Stacks its left operand; returns its right. | |
evaluation | op_ternary_either_or (evaluation const &lhs, evaluation const &rhs) |
The ':' "operator". Pops the evaluation of the preceding `cond ?' from the stack. If cond is true, returns lhs ; if cond is false returns rhs , else returns evaluation() | |
evaluation | op_bit_and (evaluation const &lhs, evaluation const &rhs) |
Bitwise AND operator. | |
evaluation | op_bit_or (evaluation const &lhs, evaluation const &rhs) |
Bitwise OR operator. | |
evaluation | op_bit_xor (evaluation const &lhs, evaluation const &rhs) |
Bitwise XOR operator. | |
evaluation | op_lshift (evaluation const &lhs, evaluation const &rhs) |
Left-shift operator. | |
evaluation | op_rshift (evaluation const &lhs, evaluation const &rhs) |
Right-shift operator. | |
evaluation | op_add (evaluation const &lhs, evaluation const &rhs) |
Addition operator. | |
evaluation | op_subtract (evaluation const &lhs, evaluation const &rhs) |
Subtraction operator. | |
evaluation | op_comma (evaluation const &lhs, evaluation const &rhs) |
Comma operator. | |
evaluation | op_mult (evaluation const &lhs, evaluation const &rhs) |
Multiplication operator. | |
evaluation | op_divide (evaluation const &lhs, evaluation const &rhs) |
Division operator. | |
evaluation | op_mod (evaluation const &lhs, evaluation const &rhs) |
Modulus operator. | |
void | parse (chewer< sequence_type > &chew) |
Evaluate text from a chewer<sequence_type> More... | |
evaluation | unary_op (chewer< sequence_type > &chew, size_t end) |
Evaluator for innermost subexpressions. More... | |
template<unsigned Precedence> | |
evaluation | infix_op (chewer< sequence_type > &chew, size_t end) |
Evaluator for infix operations. More... | |
evaluation | apply (infix_operation op, evaluation &lhs, evaluation &rhs) |
Apply a infix operation to arguments. More... | |
template<unsigned Precedence> | |
std::pair < expression_parser::infix_operation, bool > | seek_rightmost_infix (chewer< sequence_type > &chew, size_t end, size_t &op_start, size_t &op_end) |
Search for the rightmost binary operator at a given level of precedence within delimited text. More... | |
void | delete_paren (size_t loff, size_t roff) |
Logically delete redundant parentheses. | |
void | restore_paren (size_t loff, size_t roff) |
Restore a prematurely deleted pair of parentheses. | |
void | restore_paren () |
Restore all logically deleted parentheses. | |
void | cut (size_t start, size_t end) |
Logically delete part of the evaluated text. More... | |
void | defer_diagnostic (diagnostic_base &gripe) |
Defer a diagnostic, with the current line context appended if a source line is being parsed. | |
template<unsigned Precedence> | |
std::enable_if<(Precedence==0), evaluation >::type | next_evaluator (chewer< sequence_type > &chew, size_t end) |
Select the member function to recursively evaluate expressions in which the major operator has a given precedence. | |
template<unsigned Precedence> | |
std::enable_if<(Precedence > 0), evaluation >::type | next_evaluator (chewer< sequence_type > &chew, size_t end) |
Private Attributes | |
evaluation | _eval |
The evaluation that results from parsing the expression. | |
std::vector< evaluation > | _ternary_cond_stack |
Stack of ternary conditional results;. | |
sequence_type & | _seq |
A reference to the character sequence being parsed. | |
std::vector< deletion_code > | _deletions |
Vector of logical deletion codes;. | |
size_t | _start |
Text offset at which evaluation starts. | |
size_t | _cuts |
Number of logically deleted operands. | |
ptrdiff_t | _last_deletion |
Index of rightmost deletion, -1 if none. | |
reference const * | _ref |
A pointer to the reference if any, that calls for expression parsing. | |
Encapsulates parsing of preprocessor expressions.
template struct expression_parser
encapsulates the evaluation of all contexts that require resolution with respect to the defined and undefined symbols.
Definition at line 60 of file expression_parser.h.
enum expression_parser::deletion_code |
Codes for logical deletion status.
Enumerator | |
---|---|
not_deleted |
Not logically deleted. |
non_paren |
A logically deleted non-parenthesis. |
paren |
A logically deleted parenthesis. |
Definition at line 67 of file expression_parser.h.
|
inline |
Construct given a chewer<sequence_type>
chew | On entry, a chewer<sequence_type> positioned to the offset in the associated sequence_type to scan. On return chew is positioned to the first offset not consumed. |
ref | A pointer to the reference if any, that calls for expression parsing. |
Definition at line 111 of file expression_parser.h.
expression_parser< CharSeq >::expression_parser | ( | sequence_type & | seq, |
reference const * | ref = nullptr , |
||
size_t | start = 0 |
||
) |
Construct to evaluate a sequence_type
seq | A sequence_type to be evaluated. |
ref | A pointer to the reference if any, that calls for expression parsing. |
start | The offset at which to start evaluation. |
The constructed object will have parsed and seq
offset `start.
|
private |
Apply a infix operation to arguments.
op | Pointer to the operation to apply |
lhs | The left-hand argument |
rhs | The right-hand argument |
op
to lhs
and rhs
|
inlineprivate |
Logically delete part of the evaluated text.
start | Text offset at which to cut. |
end | Text offset just past cut. |
The chunk to be deleted is only logically deleted by this function. This avoids shuffling down terminal segments of the text in the course of evaluation, when the effect of such editing may be obliterated by subsequent deletions. Physical deletion is performed only once at the end of evaluation, by removing all logically deleted portions.
The member function is only called to delete a truth-functionally redundant operator and one of its operands.
Definition at line 412 of file expression_parser.h.
|
private |
Evaluator for infix operations.
The function evaluates subexpressions that are infix operations.
The evaluator does shortcircuit evaluation for && and ||. It also simplifies binary subexpressions that can be solved only on one side.
Precedence | The precedence of the operators that may be considered for evaluation. |
chew | On entry, a chewer<sequence_type> positioned to the offset in the associated sequence_type from which to scan. On return chew is positioned to the first offset not consumed. |
end | Offset beyond which not to evaluate |
evaluation
representing the result of evaluation.
|
private |
Evaluate text from a chewer<sequence_type>
chew | On entry, a chewer<sequence_type> positioned to the offset in the associated sequence_type from which to scan. On return chew is positioned to the first offset not consumed. |
|
inline |
Get the result of parsing the expression.
evaluation
that results from parsing. Definition at line 135 of file expression_parser.h.
|
private |
Search for the rightmost binary operator at a given level of precedence within delimited text.
Precedence | The precedence of the operators to be sought. |
chew | A chewer<sequence_type> positioned to the offset in the associated sequence_type from which to search. On return chew is at the same position. |
end | Offset beyond which not to seach. |
op_start | Insignificant on entry. On return, receives the offset of the rightmost operator found, if any. |
op_end | Insignificant on entry. On return, receives the offset just past the rightmost operator found,if any. |
operation
whose operator was found, if any, else nullptr, and whose second boolean element is true if the operation found is shortcuitable, else false. std::string expression_parser< CharSeq >::simplified | ( | ) | const |
Get the most simplified form of the expression.
|
private |
Evaluator for innermost subexpressions.
chew | On entry, a chewer<sequence_type> positioned to the offset in the associated sequence_type from which to scan. On return chew is positioned to the first offset not consumed. |
end | Offset beyond which not to evaluate. |
evaluation
representing the result of evaluation.The member function evaluates symbols, numbers and unary operations, including the parenthesis operation.