Remove unused ‘unexpected token’ handling
This commit is contained in:
parent
d3d25f2683
commit
8b1aeee0e3
5 changed files with 14 additions and 68 deletions
|
|
@ -6,14 +6,7 @@ extern "C" {
|
|||
|
||||
#include "tree.h"
|
||||
|
||||
typedef enum {
|
||||
TSParseErrorTypeNone,
|
||||
TSParseErrorTypeLexical,
|
||||
TSParseErrorTypeSyntactic,
|
||||
} TSParseErrorType;
|
||||
|
||||
typedef struct {
|
||||
TSParseErrorType type;
|
||||
const char **expected_inputs;
|
||||
size_t expected_input_count;
|
||||
size_t position;
|
||||
|
|
|
|||
|
|
@ -56,7 +56,6 @@ static TSParser TSParserMake(const char *input) {
|
|||
.result = {
|
||||
.tree = NULL,
|
||||
.error = {
|
||||
.type = TSParseErrorTypeNone,
|
||||
.expected_inputs = NULL,
|
||||
.expected_input_count = 0
|
||||
},
|
||||
|
|
@ -122,16 +121,6 @@ static void TSParserReduce(TSParser *parser, TSSymbol symbol, int immediate_chil
|
|||
|
||||
static void TSParserError(TSParser *parser, size_t count, const char **expected_inputs) {
|
||||
TSParseError *error = &parser->result.error;
|
||||
error->type = TSParseErrorTypeSyntactic;
|
||||
error->position = parser->position;
|
||||
error->expected_input_count = count;
|
||||
error->expected_inputs = expected_inputs;
|
||||
error->lookahead_sym = TSParserLookaheadSym(parser);
|
||||
}
|
||||
|
||||
static void TSParserLexError(TSParser *parser, size_t count, const char **expected_inputs) {
|
||||
TSParseError *error = &parser->result.error;
|
||||
error->type = TSParseErrorTypeLexical;
|
||||
error->position = parser->position;
|
||||
error->expected_input_count = count;
|
||||
error->expected_inputs = expected_inputs;
|
||||
|
|
@ -139,7 +128,7 @@ static void TSParserLexError(TSParser *parser, size_t count, const char **expect
|
|||
}
|
||||
|
||||
static int TSParserHasError(const TSParser *parser) {
|
||||
return (parser->result.error.type != TSParseErrorTypeNone);
|
||||
return (parser->result.error.expected_inputs != NULL);
|
||||
}
|
||||
|
||||
static void TSParserAdvance(TSParser *parser, TSState lex_state) {
|
||||
|
|
@ -205,17 +194,10 @@ goto next_state; \
|
|||
#define ACCEPT_TOKEN(symbol) \
|
||||
{ TSParserSetLookaheadSym(parser, symbol); goto done; }
|
||||
|
||||
#define PARSE_ERROR(count, inputs) \
|
||||
{ \
|
||||
static const char *expected_inputs[] = inputs; \
|
||||
TSParserError(parser, count, expected_inputs); \
|
||||
goto done; \
|
||||
}
|
||||
|
||||
#define LEX_ERROR(count, inputs) \
|
||||
{ \
|
||||
static const char *expected_inputs[] = inputs; \
|
||||
TSParserLexError(parser, count, expected_inputs); \
|
||||
TSParserError(parser, count, expected_inputs); \
|
||||
goto done; \
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -142,34 +142,18 @@ namespace tree_sitter {
|
|||
|
||||
string code_for_parse_actions(const unordered_set<ParseAction> &actions, const unordered_set<rules::Symbol> &expected_inputs) {
|
||||
auto action = actions.begin();
|
||||
if (action == actions.end()) {
|
||||
return parse_error_call(expected_inputs);
|
||||
} else {
|
||||
switch (action->type) {
|
||||
case ParseActionTypeAccept:
|
||||
return "ACCEPT_INPUT();";
|
||||
case ParseActionTypeShift:
|
||||
return "SHIFT(" + to_string(action->state_index) + ");";
|
||||
case ParseActionTypeReduce:
|
||||
return "REDUCE(" + symbol_id(action->symbol) + ", " + to_string(action->child_flags.size()) + ", COLLAPSE({" + collapse_flags(action->child_flags) + "}));";
|
||||
default:
|
||||
return "";
|
||||
}
|
||||
switch (action->type) {
|
||||
case ParseActionTypeAccept:
|
||||
return "ACCEPT_INPUT();";
|
||||
case ParseActionTypeShift:
|
||||
return "SHIFT(" + to_string(action->state_index) + ");";
|
||||
case ParseActionTypeReduce:
|
||||
return "REDUCE(" + symbol_id(action->symbol) + ", " + to_string(action->child_flags.size()) + ", COLLAPSE({" + collapse_flags(action->child_flags) + "}));";
|
||||
default:
|
||||
return "";
|
||||
}
|
||||
}
|
||||
|
||||
string parse_error_call(const unordered_set<rules::Symbol> &expected_inputs) {
|
||||
string result = "PARSE_ERROR(" + to_string(expected_inputs.size()) + ", EXPECT({";
|
||||
bool started = false;
|
||||
for (auto symbol : expected_inputs) {
|
||||
if (started) result += ", ";
|
||||
started = true;
|
||||
result += "\"" + symbol.name + "\"";
|
||||
}
|
||||
result += "}));";
|
||||
return result;
|
||||
}
|
||||
|
||||
string escape_string(string input) {
|
||||
str_replace(input, "\"", "\\\"");
|
||||
return input;
|
||||
|
|
@ -211,7 +195,7 @@ namespace tree_sitter {
|
|||
string body = "";
|
||||
for (auto pair : parse_state.actions)
|
||||
body += _case(symbol_id(pair.first), code_for_parse_actions(pair.second, parse_state.expected_inputs()));
|
||||
body += _default(parse_error_call(parse_state.expected_inputs()));
|
||||
body += _default("PARSE_PANIC();");
|
||||
return
|
||||
string("SET_LEX_STATE(") + to_string(parse_state.lex_state_index) + ");\n" +
|
||||
_switch("LOOKAHEAD_SYM()", body);
|
||||
|
|
|
|||
|
|
@ -30,7 +30,7 @@ TSTree * TSDocumentTree(const TSDocument *document) {
|
|||
}
|
||||
|
||||
const char * TSDocumentToString(const TSDocument *document) {
|
||||
if (document->error.type != TSParseErrorTypeNone) {
|
||||
if (document->error.expected_inputs != NULL) {
|
||||
return TSParseErrorToString(&document->error, document->text, document->symbol_names);
|
||||
} else {
|
||||
return TSTreeToString(document->tree, document->symbol_names);
|
||||
|
|
|
|||
|
|
@ -2,22 +2,9 @@
|
|||
#include <string>
|
||||
|
||||
using std::string;
|
||||
static const char * EMPTY = "";
|
||||
|
||||
const char * TSParseErrorToString(const TSParseError *error, const char *input_string, const char **symbol_names) {
|
||||
string result;
|
||||
switch (error->type) {
|
||||
case TSParseErrorTypeSyntactic:
|
||||
result = string("Unexpected token ") + symbol_names[error->lookahead_sym] + ". ";
|
||||
break;
|
||||
case TSParseErrorTypeLexical:
|
||||
result = string("Unexpected character '") + input_string[error->position] + "'. ";
|
||||
break;
|
||||
default:
|
||||
return EMPTY;
|
||||
}
|
||||
|
||||
result += "Expected: ";
|
||||
string result = string("Unexpected character '") + input_string[error->position] + "'. Expected: ";
|
||||
for (int i = 0; i < error->expected_input_count; i++)
|
||||
result += string(error->expected_inputs[i]) + " ";
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue