71 lines
1.6 KiB
C
71 lines
1.6 KiB
C
|
#pragma once
|
||
|
#ifndef PARSODUS_PARSER_{{name}}_H
|
||
|
#define PARSODUS_PARSER_{{name}}_H
|
||
|
|
||
|
#include <deque>
|
||
|
|
||
|
template <typename Value>
|
||
|
class {{name}} {
|
||
|
public:
|
||
|
{{name}}() {}
|
||
|
virtual ~{{name}}() {}
|
||
|
|
||
|
/**
|
||
|
* Parse it
|
||
|
*/
|
||
|
Value parse();
|
||
|
|
||
|
|
||
|
protected:
|
||
|
/**
|
||
|
* Represents the type of the symbol (both terminals and nonterminals)
|
||
|
*/
|
||
|
enum Symbol {
|
||
|
{{#symbols}}
|
||
|
{{symbol}},
|
||
|
{{/symbols}}
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* A token, consisting of a Symbol type (should be a terminal) and a Value
|
||
|
*/
|
||
|
struct Token {
|
||
|
Symbol symbol,
|
||
|
Value value
|
||
|
};
|
||
|
|
||
|
|
||
|
/******************************************
|
||
|
* Functions to be supplied by the user *
|
||
|
******************************************/
|
||
|
|
||
|
/**
|
||
|
* Get the next token from the lexer
|
||
|
*/
|
||
|
virtual Token lex() = 0;
|
||
|
|
||
|
/**
|
||
|
* Apply a reduction (a grammar rule in reverse)
|
||
|
*/
|
||
|
{{#rules}}
|
||
|
{{#name}}virtual Value reduce_{{name}}(const std::deque<Token>& subparts) = 0;{{/name}}
|
||
|
{{^name}}virtual Value reduce_{{index}}(const std::deque<Token>& subparts) = 0;{{/name}}
|
||
|
{{/rules}}
|
||
|
|
||
|
private:
|
||
|
};
|
||
|
|
||
|
// Not a static member because the table should not be replicated for different instantiations of the parser
|
||
|
extern const std::uint64_t {{name}}___Table[%(num_states)][%(num_symbols)];
|
||
|
|
||
|
|
||
|
/***************************
|
||
|
* Parser implementation *
|
||
|
***************************/
|
||
|
template <typename Value>
|
||
|
Value {{name}}::parse() {
|
||
|
//TODO
|
||
|
}
|
||
|
|
||
|
#endif /* PARSODUS_PARSER_{{name}}_H */
|