Allow multiple top-level nodes

Now, the root node of a document is always a document node.
It will often have only one child node which corresponds to the grammar's
start symbol, but not always. Currently, it may have more than one child
if there are ubiquitous tokens such as comments at the beginning of the
document. In the future, it will also be possible be possible to have multiple
for the document to have multiple children if the document is partially parsed.
This commit is contained in:
Max Brunsfeld 2014-08-08 23:58:59 -07:00
parent 9302080aa6
commit 1e79ed794b
17 changed files with 78 additions and 39 deletions

View file

@ -143,6 +143,7 @@ class ParseTableBuilder {
item_set_closure(start_item, { rules::END_OF_INPUT() }, grammar));
parse_table.symbols.insert(rules::ERROR());
parse_table.symbols.insert(rules::DOCUMENT());
while (!item_sets_to_process.empty()) {
auto pair = item_sets_to_process.back();

View file

@ -77,7 +77,7 @@ class CCodeGenerator {
for (auto symbol : parse_table.symbols)
if (!symbol.is_built_in()) {
if (at_start)
line(symbol_id(symbol) + " = ts_start_sym,");
line(symbol_id(symbol) + " = ts_builtin_sym_start,");
else
line(symbol_id(symbol) + ",");
at_start = false;
@ -88,10 +88,6 @@ class CCodeGenerator {
}
void symbol_names_list() {
set<rules::Symbol> symbols(parse_table.symbols);
symbols.insert(rules::END_OF_INPUT());
symbols.insert(rules::ERROR());
line("SYMBOL_NAMES = {");
indent([&]() {
for (auto symbol : parse_table.symbols)
@ -174,8 +170,12 @@ class CCodeGenerator {
string symbol_id(const rules::Symbol &symbol) {
if (symbol.is_built_in()) {
return (symbol == rules::ERROR()) ? "ts_builtin_sym_error"
: "ts_builtin_sym_end";
if (symbol == rules::ERROR())
return "ts_builtin_sym_error";
else if (symbol == rules::END_OF_INPUT())
return "ts_builtin_sym_end";
else
return "ts_builtin_sym_document";
} else {
string name = sanitize_name(rule_name(symbol));
if (symbol.is_auxiliary())
@ -221,7 +221,12 @@ class CCodeGenerator {
string symbol_name(const rules::Symbol &symbol) {
if (symbol.is_built_in()) {
return (symbol == rules::ERROR()) ? "error" : "end";
if (symbol == rules::ERROR())
return "error";
else if (symbol == rules::END_OF_INPUT())
return "end";
else
return "DOCUMENT";
} else if (symbol.is_token() && symbol.is_auxiliary()) {
return rule_name(symbol);
} else {

View file

@ -6,6 +6,7 @@ namespace rules {
Symbol END_OF_INPUT() { return Symbol(-1, SymbolOptionToken); }
Symbol ERROR() { return Symbol(-2, SymbolOptionToken); }
Symbol START() { return Symbol(-3); }
Symbol DOCUMENT() { return Symbol(-4); }
} // namespace rules
} // namespace tree_sitter

View file

@ -9,6 +9,7 @@ namespace rules {
Symbol ERROR();
Symbol START();
Symbol END_OF_INPUT();
Symbol DOCUMENT();
} // namespace rules
} // namespace tree_sitter