Parsodus/templates/c++/lr.h

71 lines
1.6 KiB
C
Raw Normal View History

2016-12-31 16:16:26 +01:00
#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 */