2016-05-02 22:48:57 +00:00
|
|
|
// Programmatic representation of fish code.
|
2013-07-23 01:26:15 +00:00
|
|
|
#ifndef FISH_PARSE_TREE_CONSTRUCTION_H
|
|
|
|
#define FISH_PARSE_TREE_CONSTRUCTION_H
|
|
|
|
|
2016-05-02 22:48:57 +00:00
|
|
|
#include <sys/types.h>
|
2016-04-21 06:00:54 +00:00
|
|
|
|
|
|
|
#include "parse_constants.h"
|
2015-07-25 15:14:25 +00:00
|
|
|
|
|
|
|
struct parse_token_t;
|
2013-07-23 01:26:15 +00:00
|
|
|
|
2016-05-02 22:48:57 +00:00
|
|
|
namespace parse_productions {
|
2013-07-25 22:24:22 +00:00
|
|
|
|
2016-05-02 22:48:57 +00:00
|
|
|
// A production is an array of unsigned char. Symbols are encoded directly as their symbol value.
|
|
|
|
// Keywords are encoded with an offset of LAST_TOKEN_OR_SYMBOL + 1. So essentially we glom together
|
|
|
|
// keywords and symbols.
|
2013-07-28 22:19:38 +00:00
|
|
|
typedef uint8_t production_element_t;
|
2013-07-28 22:44:09 +00:00
|
|
|
|
2016-05-02 22:48:57 +00:00
|
|
|
/// Resolve the type from a production element.
|
|
|
|
inline parse_token_type_t production_element_type(production_element_t elem) {
|
|
|
|
if (elem > LAST_TOKEN_OR_SYMBOL) {
|
2013-07-28 22:19:38 +00:00
|
|
|
return parse_token_type_string;
|
2016-05-02 22:48:57 +00:00
|
|
|
} else {
|
2013-07-28 22:19:38 +00:00
|
|
|
return static_cast<parse_token_type_t>(elem);
|
2013-07-23 01:26:15 +00:00
|
|
|
}
|
2013-07-28 22:19:38 +00:00
|
|
|
}
|
2013-07-23 01:26:15 +00:00
|
|
|
|
2016-05-02 22:48:57 +00:00
|
|
|
/// Resolve the keyword from a production element.
|
|
|
|
inline parse_keyword_t production_element_keyword(production_element_t elem) {
|
|
|
|
if (elem > LAST_TOKEN_OR_SYMBOL) {
|
|
|
|
// First keyword is LAST_TOKEN_OR_SYMBOL + 1.
|
2013-07-28 22:19:38 +00:00
|
|
|
return static_cast<parse_keyword_t>(elem - LAST_TOKEN_OR_SYMBOL - 1);
|
2016-05-02 22:48:57 +00:00
|
|
|
} else {
|
2013-07-28 22:19:38 +00:00
|
|
|
return parse_keyword_none;
|
2013-07-23 01:26:15 +00:00
|
|
|
}
|
2013-07-28 22:19:38 +00:00
|
|
|
}
|
2013-07-23 01:26:15 +00:00
|
|
|
|
2016-05-02 22:48:57 +00:00
|
|
|
/// Check if an element is valid.
|
|
|
|
inline bool production_element_is_valid(production_element_t elem) {
|
2013-07-28 22:19:38 +00:00
|
|
|
return elem != token_type_invalid;
|
|
|
|
}
|
2013-07-23 01:26:15 +00:00
|
|
|
|
2016-05-02 22:48:57 +00:00
|
|
|
/// Fetch a production. We are passed two input tokens. The first input token is guaranteed to not
|
|
|
|
/// be invalid; the second token may be invalid if there's no more tokens. We may also set flags.
|
2016-11-10 05:37:49 +00:00
|
|
|
const production_element_t *production_for_token(parse_token_type_t node_type,
|
|
|
|
const parse_token_t &input1,
|
|
|
|
const parse_token_t &input2, uint8_t *out_tag);
|
2013-07-23 01:26:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|