From ef984491f7d650f910f65605bd07ad3bf34484b9 Mon Sep 17 00:00:00 2001 From: Romain Paquet Date: Mon, 16 Oct 2023 22:02:26 +0200 Subject: [PATCH] init --- .gitignore | 3 + Cargo.toml | 26 + binding.gyp | 19 + bindings/node/binding.cc | 28 + bindings/node/index.js | 19 + bindings/rust/build.rs | 40 + bindings/rust/lib.rs | 52 + grammar.js | 120 ++ package.json | 27 + queries/highlights.scm | 14 + src/grammar.json | 538 +++++++++ src/node-types.json | 524 ++++++++ src/parser.c | 1747 +++++++++++++++++++++++++++ src/tree_sitter/parser.h | 224 ++++ test/corpus/booleans.txt | 75 ++ test/corpus/function_parameters.txt | 114 ++ test/corpus/if.txt | 95 ++ test/corpus/return.txt | 56 + test/corpus/return_types.txt | 30 + ts_verify_ts.conf | 1 + 20 files changed, 3752 insertions(+) create mode 100644 .gitignore create mode 100644 Cargo.toml create mode 100644 binding.gyp create mode 100644 bindings/node/binding.cc create mode 100644 bindings/node/index.js create mode 100644 bindings/rust/build.rs create mode 100644 bindings/rust/lib.rs create mode 100644 grammar.js create mode 100644 package.json create mode 100644 queries/highlights.scm create mode 100644 src/grammar.json create mode 100644 src/node-types.json create mode 100644 src/parser.c create mode 100644 src/tree_sitter/parser.h create mode 100644 test/corpus/booleans.txt create mode 100644 test/corpus/function_parameters.txt create mode 100644 test/corpus/if.txt create mode 100644 test/corpus/return.txt create mode 100644 test/corpus/return_types.txt create mode 100644 ts_verify_ts.conf diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..5b9e2ce --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +package-lock.json +Cargo.lock +target diff --git a/Cargo.toml b/Cargo.toml new file mode 100644 index 0000000..f8ba1cf --- /dev/null +++ b/Cargo.toml @@ -0,0 +1,26 @@ +[package] +name = "tree-sitter-lila" +description = "Lila grammar for the tree-sitter parsing library" +version = "0.0.1" +keywords = ["incremental", "parsing", "lila"] +categories = ["parsing", "text-editors"] +repository = "https://git.sr.ht/~rpqt/tree-sitter-lila" +edition = "2018" +license = "MIT" + +build = "bindings/rust/build.rs" +include = [ + "bindings/rust/*", + "grammar.js", + "queries/*", + "src/*", +] + +[lib] +path = "bindings/rust/lib.rs" + +[dependencies] +tree-sitter = "~0.20.3" + +[build-dependencies] +cc = "1.0" diff --git a/binding.gyp b/binding.gyp new file mode 100644 index 0000000..6872ca2 --- /dev/null +++ b/binding.gyp @@ -0,0 +1,19 @@ +{ + "targets": [ + { + "target_name": "tree_sitter_lila_binding", + "include_dirs": [ + " +#include "nan.h" + +using namespace v8; + +extern "C" TSLanguage * tree_sitter_lila(); + +namespace { + +NAN_METHOD(New) {} + +void Init(Local exports, Local module) { + Local tpl = Nan::New(New); + tpl->SetClassName(Nan::New("Language").ToLocalChecked()); + tpl->InstanceTemplate()->SetInternalFieldCount(1); + + Local constructor = Nan::GetFunction(tpl).ToLocalChecked(); + Local instance = constructor->NewInstance(Nan::GetCurrentContext()).ToLocalChecked(); + Nan::SetInternalFieldPointer(instance, 0, tree_sitter_lila()); + + Nan::Set(instance, Nan::New("name").ToLocalChecked(), Nan::New("lila").ToLocalChecked()); + Nan::Set(module, Nan::New("exports").ToLocalChecked(), instance); +} + +NODE_MODULE(tree_sitter_lila_binding, Init) + +} // namespace diff --git a/bindings/node/index.js b/bindings/node/index.js new file mode 100644 index 0000000..9597c13 --- /dev/null +++ b/bindings/node/index.js @@ -0,0 +1,19 @@ +try { + module.exports = require("../../build/Release/tree_sitter_lila_binding"); +} catch (error1) { + if (error1.code !== 'MODULE_NOT_FOUND') { + throw error1; + } + try { + module.exports = require("../../build/Debug/tree_sitter_lila_binding"); + } catch (error2) { + if (error2.code !== 'MODULE_NOT_FOUND') { + throw error2; + } + throw error1 + } +} + +try { + module.exports.nodeTypeInfo = require("../../src/node-types.json"); +} catch (_) {} diff --git a/bindings/rust/build.rs b/bindings/rust/build.rs new file mode 100644 index 0000000..c6061f0 --- /dev/null +++ b/bindings/rust/build.rs @@ -0,0 +1,40 @@ +fn main() { + let src_dir = std::path::Path::new("src"); + + let mut c_config = cc::Build::new(); + c_config.include(&src_dir); + c_config + .flag_if_supported("-Wno-unused-parameter") + .flag_if_supported("-Wno-unused-but-set-variable") + .flag_if_supported("-Wno-trigraphs"); + let parser_path = src_dir.join("parser.c"); + c_config.file(&parser_path); + + // If your language uses an external scanner written in C, + // then include this block of code: + + /* + let scanner_path = src_dir.join("scanner.c"); + c_config.file(&scanner_path); + println!("cargo:rerun-if-changed={}", scanner_path.to_str().unwrap()); + */ + + c_config.compile("parser"); + println!("cargo:rerun-if-changed={}", parser_path.to_str().unwrap()); + + // If your language uses an external scanner written in C++, + // then include this block of code: + + /* + let mut cpp_config = cc::Build::new(); + cpp_config.cpp(true); + cpp_config.include(&src_dir); + cpp_config + .flag_if_supported("-Wno-unused-parameter") + .flag_if_supported("-Wno-unused-but-set-variable"); + let scanner_path = src_dir.join("scanner.cc"); + cpp_config.file(&scanner_path); + cpp_config.compile("scanner"); + println!("cargo:rerun-if-changed={}", scanner_path.to_str().unwrap()); + */ +} diff --git a/bindings/rust/lib.rs b/bindings/rust/lib.rs new file mode 100644 index 0000000..0430ec4 --- /dev/null +++ b/bindings/rust/lib.rs @@ -0,0 +1,52 @@ +//! This crate provides lila language support for the [tree-sitter][] parsing library. +//! +//! Typically, you will use the [language][language func] function to add this language to a +//! tree-sitter [Parser][], and then use the parser to parse some code: +//! +//! ``` +//! let code = ""; +//! let mut parser = tree_sitter::Parser::new(); +//! parser.set_language(tree_sitter_lila::language()).expect("Error loading lila grammar"); +//! let tree = parser.parse(code, None).unwrap(); +//! ``` +//! +//! [Language]: https://docs.rs/tree-sitter/*/tree_sitter/struct.Language.html +//! [language func]: fn.language.html +//! [Parser]: https://docs.rs/tree-sitter/*/tree_sitter/struct.Parser.html +//! [tree-sitter]: https://tree-sitter.github.io/ + +use tree_sitter::Language; + +extern "C" { + fn tree_sitter_lila() -> Language; +} + +/// Get the tree-sitter [Language][] for this grammar. +/// +/// [Language]: https://docs.rs/tree-sitter/*/tree_sitter/struct.Language.html +pub fn language() -> Language { + unsafe { tree_sitter_lila() } +} + +/// The content of the [`node-types.json`][] file for this grammar. +/// +/// [`node-types.json`]: https://tree-sitter.github.io/tree-sitter/using-parsers#static-node-types +pub const NODE_TYPES: &'static str = include_str!("../../src/node-types.json"); + +// Uncomment these to include any queries that this grammar contains + +// pub const HIGHLIGHTS_QUERY: &'static str = include_str!("../../queries/highlights.scm"); +// pub const INJECTIONS_QUERY: &'static str = include_str!("../../queries/injections.scm"); +// pub const LOCALS_QUERY: &'static str = include_str!("../../queries/locals.scm"); +// pub const TAGS_QUERY: &'static str = include_str!("../../queries/tags.scm"); + +#[cfg(test)] +mod tests { + #[test] + fn test_can_load_grammar() { + let mut parser = tree_sitter::Parser::new(); + parser + .set_language(super::language()) + .expect("Error loading lila language"); + } +} diff --git a/grammar.js b/grammar.js new file mode 100644 index 0000000..f04141c --- /dev/null +++ b/grammar.js @@ -0,0 +1,120 @@ +module.exports = grammar({ + name: 'lila', + + word: $ => $.identifier, + + rules: { + source_file: $ => repeat($._definition), + + _definition: $ => choice( + $.function_definition + ), + + function_definition: $ => seq( + 'func', + field('name', $.identifier), + field('parameters', $.parameters), + field('return_type', optional($._type)), + field('body', $.block), + ), + + parameters: $ => seq( + '(', + optional(seq( + repeat(seq($.parameter, ',')), + seq($.parameter, optional(',')), + )), + ')', + ), + + parameter: $ => seq( + $.identifier, + ' ', + $._type, + ), + + block: $ => seq( + '{', + repeat($._statement), + optional($._expression), + '}', + ), + + _statement: $ => choice( + $.return_statement, + $.assign_statement, + $.if_statement, + ), + + if_statement: $ => seq( + 'if', + field('condition', $._expression), + field('body', $.block), + ';' + ), + + if_expression: $ => seq( + 'if', + field('condition', $._expression), + field('body', $.block), + field('else', + seq('else', choice( + $._expression, + $.block, + )) + ), + ), + + return_statement: $ => seq( + 'return', + optional($._expression), + ';', + ), + + assign_statement: $ => seq( + $.identifier, + '=', + $._expression, + ';', + ), + + _expression: $ => choice( + $.unary_expression, + $.binary_expression, + $.if_expression, + $.boolean_literal, + $.integer_literal, + $.identifier, + ), + + unary_expression: $ => prec(10, choice( + seq('-', $._expression), + seq('!', $._expression), + )), + + binary_expression: $ => choice( + prec.left(4, seq($._expression, '*', $._expression)), + prec.left(3, seq($._expression, '+', $._expression)), + prec.left(2, seq($._expression, '||', $._expression)), + prec.left(1, seq($._expression, '&&', $._expression)), + ), + + identifier: $ => /[a-z_]+[a-zA-Z0-9_]*/, + + integer_literal: $ => /\d+/, + + boolean_literal: $ => choice('true', 'false'), + + //string_literal: $ => /"[]"/, + + _type: $ => choice( + $.primitive_type, + ), + + primitive_type: $ => choice( + 'bool', + 'int', + 'uint', + ), + } +}); diff --git a/package.json b/package.json new file mode 100644 index 0000000..1528d54 --- /dev/null +++ b/package.json @@ -0,0 +1,27 @@ +{ + "name": "tree-sitter-lila", + "version": "0.0.1", + "description": "Lila grammar for tree-sitter", + "main": "bindings/node", + "keywords": [ + "parsing", + "incremental" + ], + "dependencies": { + "nan": "^2.12.1" + }, + "devDependencies": { + "tree-sitter-cli": "^0.20.8" + }, + "scripts": { + "test": "tree-sitter test" + }, + "tree-sitter": [ + { + "scope": "source.lila", + "file-types": [ + "lila" + ] + } + ] +} diff --git a/queries/highlights.scm b/queries/highlights.scm new file mode 100644 index 0000000..36dd0da --- /dev/null +++ b/queries/highlights.scm @@ -0,0 +1,14 @@ + + +"func" @keyword +"return" @keyword +"if" @keyword +"else" @keyword +;"while" @keyword + +"uint" @type +"int" @type + +(integer_literal) @number + +(function_definition name: (identifier) @function) diff --git a/src/grammar.json b/src/grammar.json new file mode 100644 index 0000000..4fff430 --- /dev/null +++ b/src/grammar.json @@ -0,0 +1,538 @@ +{ + "name": "lila", + "word": "identifier", + "rules": { + "source_file": { + "type": "REPEAT", + "content": { + "type": "SYMBOL", + "name": "_definition" + } + }, + "_definition": { + "type": "CHOICE", + "members": [ + { + "type": "SYMBOL", + "name": "function_definition" + } + ] + }, + "function_definition": { + "type": "SEQ", + "members": [ + { + "type": "STRING", + "value": "func" + }, + { + "type": "FIELD", + "name": "name", + "content": { + "type": "SYMBOL", + "name": "identifier" + } + }, + { + "type": "FIELD", + "name": "parameters", + "content": { + "type": "SYMBOL", + "name": "parameters" + } + }, + { + "type": "FIELD", + "name": "return_type", + "content": { + "type": "CHOICE", + "members": [ + { + "type": "SYMBOL", + "name": "_type" + }, + { + "type": "BLANK" + } + ] + } + }, + { + "type": "FIELD", + "name": "body", + "content": { + "type": "SYMBOL", + "name": "block" + } + } + ] + }, + "parameters": { + "type": "SEQ", + "members": [ + { + "type": "STRING", + "value": "(" + }, + { + "type": "CHOICE", + "members": [ + { + "type": "SEQ", + "members": [ + { + "type": "REPEAT", + "content": { + "type": "SEQ", + "members": [ + { + "type": "SYMBOL", + "name": "parameter" + }, + { + "type": "STRING", + "value": "," + } + ] + } + }, + { + "type": "SEQ", + "members": [ + { + "type": "SYMBOL", + "name": "parameter" + }, + { + "type": "CHOICE", + "members": [ + { + "type": "STRING", + "value": "," + }, + { + "type": "BLANK" + } + ] + } + ] + } + ] + }, + { + "type": "BLANK" + } + ] + }, + { + "type": "STRING", + "value": ")" + } + ] + }, + "parameter": { + "type": "SEQ", + "members": [ + { + "type": "SYMBOL", + "name": "identifier" + }, + { + "type": "STRING", + "value": " " + }, + { + "type": "SYMBOL", + "name": "_type" + } + ] + }, + "block": { + "type": "SEQ", + "members": [ + { + "type": "STRING", + "value": "{" + }, + { + "type": "REPEAT", + "content": { + "type": "SYMBOL", + "name": "_statement" + } + }, + { + "type": "CHOICE", + "members": [ + { + "type": "SYMBOL", + "name": "_expression" + }, + { + "type": "BLANK" + } + ] + }, + { + "type": "STRING", + "value": "}" + } + ] + }, + "_statement": { + "type": "CHOICE", + "members": [ + { + "type": "SYMBOL", + "name": "return_statement" + }, + { + "type": "SYMBOL", + "name": "assign_statement" + }, + { + "type": "SYMBOL", + "name": "if_statement" + } + ] + }, + "if_statement": { + "type": "SEQ", + "members": [ + { + "type": "STRING", + "value": "if" + }, + { + "type": "FIELD", + "name": "condition", + "content": { + "type": "SYMBOL", + "name": "_expression" + } + }, + { + "type": "FIELD", + "name": "body", + "content": { + "type": "SYMBOL", + "name": "block" + } + }, + { + "type": "STRING", + "value": ";" + } + ] + }, + "if_expression": { + "type": "SEQ", + "members": [ + { + "type": "STRING", + "value": "if" + }, + { + "type": "FIELD", + "name": "condition", + "content": { + "type": "SYMBOL", + "name": "_expression" + } + }, + { + "type": "FIELD", + "name": "body", + "content": { + "type": "SYMBOL", + "name": "block" + } + }, + { + "type": "FIELD", + "name": "else", + "content": { + "type": "SEQ", + "members": [ + { + "type": "STRING", + "value": "else" + }, + { + "type": "CHOICE", + "members": [ + { + "type": "SYMBOL", + "name": "_expression" + }, + { + "type": "SYMBOL", + "name": "block" + } + ] + } + ] + } + } + ] + }, + "return_statement": { + "type": "SEQ", + "members": [ + { + "type": "STRING", + "value": "return" + }, + { + "type": "CHOICE", + "members": [ + { + "type": "SYMBOL", + "name": "_expression" + }, + { + "type": "BLANK" + } + ] + }, + { + "type": "STRING", + "value": ";" + } + ] + }, + "assign_statement": { + "type": "SEQ", + "members": [ + { + "type": "SYMBOL", + "name": "identifier" + }, + { + "type": "STRING", + "value": "=" + }, + { + "type": "SYMBOL", + "name": "_expression" + }, + { + "type": "STRING", + "value": ";" + } + ] + }, + "_expression": { + "type": "CHOICE", + "members": [ + { + "type": "SYMBOL", + "name": "unary_expression" + }, + { + "type": "SYMBOL", + "name": "binary_expression" + }, + { + "type": "SYMBOL", + "name": "if_expression" + }, + { + "type": "SYMBOL", + "name": "boolean_literal" + }, + { + "type": "SYMBOL", + "name": "integer_literal" + }, + { + "type": "SYMBOL", + "name": "identifier" + } + ] + }, + "unary_expression": { + "type": "PREC", + "value": 10, + "content": { + "type": "CHOICE", + "members": [ + { + "type": "SEQ", + "members": [ + { + "type": "STRING", + "value": "-" + }, + { + "type": "SYMBOL", + "name": "_expression" + } + ] + }, + { + "type": "SEQ", + "members": [ + { + "type": "STRING", + "value": "!" + }, + { + "type": "SYMBOL", + "name": "_expression" + } + ] + } + ] + } + }, + "binary_expression": { + "type": "CHOICE", + "members": [ + { + "type": "PREC_LEFT", + "value": 4, + "content": { + "type": "SEQ", + "members": [ + { + "type": "SYMBOL", + "name": "_expression" + }, + { + "type": "STRING", + "value": "*" + }, + { + "type": "SYMBOL", + "name": "_expression" + } + ] + } + }, + { + "type": "PREC_LEFT", + "value": 3, + "content": { + "type": "SEQ", + "members": [ + { + "type": "SYMBOL", + "name": "_expression" + }, + { + "type": "STRING", + "value": "+" + }, + { + "type": "SYMBOL", + "name": "_expression" + } + ] + } + }, + { + "type": "PREC_LEFT", + "value": 2, + "content": { + "type": "SEQ", + "members": [ + { + "type": "SYMBOL", + "name": "_expression" + }, + { + "type": "STRING", + "value": "||" + }, + { + "type": "SYMBOL", + "name": "_expression" + } + ] + } + }, + { + "type": "PREC_LEFT", + "value": 1, + "content": { + "type": "SEQ", + "members": [ + { + "type": "SYMBOL", + "name": "_expression" + }, + { + "type": "STRING", + "value": "&&" + }, + { + "type": "SYMBOL", + "name": "_expression" + } + ] + } + } + ] + }, + "identifier": { + "type": "PATTERN", + "value": "[a-z_]+[a-zA-Z0-9_]*" + }, + "integer_literal": { + "type": "PATTERN", + "value": "\\d+" + }, + "boolean_literal": { + "type": "CHOICE", + "members": [ + { + "type": "STRING", + "value": "true" + }, + { + "type": "STRING", + "value": "false" + } + ] + }, + "_type": { + "type": "CHOICE", + "members": [ + { + "type": "SYMBOL", + "name": "primitive_type" + } + ] + }, + "primitive_type": { + "type": "CHOICE", + "members": [ + { + "type": "STRING", + "value": "bool" + }, + { + "type": "STRING", + "value": "int" + }, + { + "type": "STRING", + "value": "uint" + } + ] + } + }, + "extras": [ + { + "type": "PATTERN", + "value": "\\s" + } + ], + "conflicts": [], + "precedences": [], + "externals": [], + "inline": [], + "supertypes": [] +} + diff --git a/src/node-types.json b/src/node-types.json new file mode 100644 index 0000000..586cd42 --- /dev/null +++ b/src/node-types.json @@ -0,0 +1,524 @@ +[ + { + "type": "assign_statement", + "named": true, + "fields": {}, + "children": { + "multiple": true, + "required": true, + "types": [ + { + "type": "binary_expression", + "named": true + }, + { + "type": "boolean_literal", + "named": true + }, + { + "type": "identifier", + "named": true + }, + { + "type": "if_expression", + "named": true + }, + { + "type": "integer_literal", + "named": true + }, + { + "type": "unary_expression", + "named": true + } + ] + } + }, + { + "type": "binary_expression", + "named": true, + "fields": {}, + "children": { + "multiple": true, + "required": true, + "types": [ + { + "type": "binary_expression", + "named": true + }, + { + "type": "boolean_literal", + "named": true + }, + { + "type": "identifier", + "named": true + }, + { + "type": "if_expression", + "named": true + }, + { + "type": "integer_literal", + "named": true + }, + { + "type": "unary_expression", + "named": true + } + ] + } + }, + { + "type": "block", + "named": true, + "fields": {}, + "children": { + "multiple": true, + "required": false, + "types": [ + { + "type": "assign_statement", + "named": true + }, + { + "type": "binary_expression", + "named": true + }, + { + "type": "boolean_literal", + "named": true + }, + { + "type": "identifier", + "named": true + }, + { + "type": "if_expression", + "named": true + }, + { + "type": "if_statement", + "named": true + }, + { + "type": "integer_literal", + "named": true + }, + { + "type": "return_statement", + "named": true + }, + { + "type": "unary_expression", + "named": true + } + ] + } + }, + { + "type": "boolean_literal", + "named": true, + "fields": {} + }, + { + "type": "function_definition", + "named": true, + "fields": { + "body": { + "multiple": false, + "required": true, + "types": [ + { + "type": "block", + "named": true + } + ] + }, + "name": { + "multiple": false, + "required": true, + "types": [ + { + "type": "identifier", + "named": true + } + ] + }, + "parameters": { + "multiple": false, + "required": true, + "types": [ + { + "type": "parameters", + "named": true + } + ] + }, + "return_type": { + "multiple": false, + "required": false, + "types": [ + { + "type": "primitive_type", + "named": true + } + ] + } + } + }, + { + "type": "if_expression", + "named": true, + "fields": { + "body": { + "multiple": false, + "required": true, + "types": [ + { + "type": "block", + "named": true + } + ] + }, + "condition": { + "multiple": false, + "required": true, + "types": [ + { + "type": "binary_expression", + "named": true + }, + { + "type": "boolean_literal", + "named": true + }, + { + "type": "identifier", + "named": true + }, + { + "type": "if_expression", + "named": true + }, + { + "type": "integer_literal", + "named": true + }, + { + "type": "unary_expression", + "named": true + } + ] + }, + "else": { + "multiple": true, + "required": true, + "types": [ + { + "type": "binary_expression", + "named": true + }, + { + "type": "block", + "named": true + }, + { + "type": "boolean_literal", + "named": true + }, + { + "type": "else", + "named": false + }, + { + "type": "identifier", + "named": true + }, + { + "type": "if_expression", + "named": true + }, + { + "type": "integer_literal", + "named": true + }, + { + "type": "unary_expression", + "named": true + } + ] + } + } + }, + { + "type": "if_statement", + "named": true, + "fields": { + "body": { + "multiple": false, + "required": true, + "types": [ + { + "type": "block", + "named": true + } + ] + }, + "condition": { + "multiple": false, + "required": true, + "types": [ + { + "type": "binary_expression", + "named": true + }, + { + "type": "boolean_literal", + "named": true + }, + { + "type": "identifier", + "named": true + }, + { + "type": "if_expression", + "named": true + }, + { + "type": "integer_literal", + "named": true + }, + { + "type": "unary_expression", + "named": true + } + ] + } + } + }, + { + "type": "parameter", + "named": true, + "fields": {}, + "children": { + "multiple": true, + "required": true, + "types": [ + { + "type": "identifier", + "named": true + }, + { + "type": "primitive_type", + "named": true + } + ] + } + }, + { + "type": "parameters", + "named": true, + "fields": {}, + "children": { + "multiple": true, + "required": false, + "types": [ + { + "type": "parameter", + "named": true + } + ] + } + }, + { + "type": "primitive_type", + "named": true, + "fields": {} + }, + { + "type": "return_statement", + "named": true, + "fields": {}, + "children": { + "multiple": false, + "required": false, + "types": [ + { + "type": "binary_expression", + "named": true + }, + { + "type": "boolean_literal", + "named": true + }, + { + "type": "identifier", + "named": true + }, + { + "type": "if_expression", + "named": true + }, + { + "type": "integer_literal", + "named": true + }, + { + "type": "unary_expression", + "named": true + } + ] + } + }, + { + "type": "source_file", + "named": true, + "fields": {}, + "children": { + "multiple": true, + "required": false, + "types": [ + { + "type": "function_definition", + "named": true + } + ] + } + }, + { + "type": "unary_expression", + "named": true, + "fields": {}, + "children": { + "multiple": false, + "required": true, + "types": [ + { + "type": "binary_expression", + "named": true + }, + { + "type": "boolean_literal", + "named": true + }, + { + "type": "identifier", + "named": true + }, + { + "type": "if_expression", + "named": true + }, + { + "type": "integer_literal", + "named": true + }, + { + "type": "unary_expression", + "named": true + } + ] + } + }, + { + "type": " ", + "named": false + }, + { + "type": "!", + "named": false + }, + { + "type": "&&", + "named": false + }, + { + "type": "(", + "named": false + }, + { + "type": ")", + "named": false + }, + { + "type": "*", + "named": false + }, + { + "type": "+", + "named": false + }, + { + "type": ",", + "named": false + }, + { + "type": "-", + "named": false + }, + { + "type": ";", + "named": false + }, + { + "type": "=", + "named": false + }, + { + "type": "bool", + "named": false + }, + { + "type": "else", + "named": false + }, + { + "type": "false", + "named": false + }, + { + "type": "func", + "named": false + }, + { + "type": "identifier", + "named": true + }, + { + "type": "if", + "named": false + }, + { + "type": "int", + "named": false + }, + { + "type": "integer_literal", + "named": true + }, + { + "type": "return", + "named": false + }, + { + "type": "true", + "named": false + }, + { + "type": "uint", + "named": false + }, + { + "type": "{", + "named": false + }, + { + "type": "||", + "named": false + }, + { + "type": "}", + "named": false + } +] \ No newline at end of file diff --git a/src/parser.c b/src/parser.c new file mode 100644 index 0000000..b31352a --- /dev/null +++ b/src/parser.c @@ -0,0 +1,1747 @@ +#include + +#if defined(__GNUC__) || defined(__clang__) +#pragma GCC diagnostic push +#pragma GCC diagnostic ignored "-Wmissing-field-initializers" +#endif + +#define LANGUAGE_VERSION 14 +#define STATE_COUNT 69 +#define LARGE_STATE_COUNT 2 +#define SYMBOL_COUNT 46 +#define ALIAS_COUNT 0 +#define TOKEN_COUNT 26 +#define EXTERNAL_TOKEN_COUNT 0 +#define FIELD_COUNT 6 +#define MAX_ALIAS_SEQUENCE_LENGTH 5 +#define PRODUCTION_ID_COUNT 5 + +enum { + sym_identifier = 1, + anon_sym_func = 2, + anon_sym_LPAREN = 3, + anon_sym_COMMA = 4, + anon_sym_RPAREN = 5, + anon_sym_ = 6, + anon_sym_LBRACE = 7, + anon_sym_RBRACE = 8, + anon_sym_if = 9, + anon_sym_SEMI = 10, + anon_sym_else = 11, + anon_sym_return = 12, + anon_sym_EQ = 13, + anon_sym_DASH = 14, + anon_sym_BANG = 15, + anon_sym_STAR = 16, + anon_sym_PLUS = 17, + anon_sym_PIPE_PIPE = 18, + anon_sym_AMP_AMP = 19, + sym_integer_literal = 20, + anon_sym_true = 21, + anon_sym_false = 22, + anon_sym_bool = 23, + anon_sym_int = 24, + anon_sym_uint = 25, + sym_source_file = 26, + sym__definition = 27, + sym_function_definition = 28, + sym_parameters = 29, + sym_parameter = 30, + sym_block = 31, + sym__statement = 32, + sym_if_statement = 33, + sym_if_expression = 34, + sym_return_statement = 35, + sym_assign_statement = 36, + sym__expression = 37, + sym_unary_expression = 38, + sym_binary_expression = 39, + sym_boolean_literal = 40, + sym__type = 41, + sym_primitive_type = 42, + aux_sym_source_file_repeat1 = 43, + aux_sym_parameters_repeat1 = 44, + aux_sym_block_repeat1 = 45, +}; + +static const char * const ts_symbol_names[] = { + [ts_builtin_sym_end] = "end", + [sym_identifier] = "identifier", + [anon_sym_func] = "func", + [anon_sym_LPAREN] = "(", + [anon_sym_COMMA] = ",", + [anon_sym_RPAREN] = ")", + [anon_sym_] = " ", + [anon_sym_LBRACE] = "{", + [anon_sym_RBRACE] = "}", + [anon_sym_if] = "if", + [anon_sym_SEMI] = ";", + [anon_sym_else] = "else", + [anon_sym_return] = "return", + [anon_sym_EQ] = "=", + [anon_sym_DASH] = "-", + [anon_sym_BANG] = "!", + [anon_sym_STAR] = "*", + [anon_sym_PLUS] = "+", + [anon_sym_PIPE_PIPE] = "||", + [anon_sym_AMP_AMP] = "&&", + [sym_integer_literal] = "integer_literal", + [anon_sym_true] = "true", + [anon_sym_false] = "false", + [anon_sym_bool] = "bool", + [anon_sym_int] = "int", + [anon_sym_uint] = "uint", + [sym_source_file] = "source_file", + [sym__definition] = "_definition", + [sym_function_definition] = "function_definition", + [sym_parameters] = "parameters", + [sym_parameter] = "parameter", + [sym_block] = "block", + [sym__statement] = "_statement", + [sym_if_statement] = "if_statement", + [sym_if_expression] = "if_expression", + [sym_return_statement] = "return_statement", + [sym_assign_statement] = "assign_statement", + [sym__expression] = "_expression", + [sym_unary_expression] = "unary_expression", + [sym_binary_expression] = "binary_expression", + [sym_boolean_literal] = "boolean_literal", + [sym__type] = "_type", + [sym_primitive_type] = "primitive_type", + [aux_sym_source_file_repeat1] = "source_file_repeat1", + [aux_sym_parameters_repeat1] = "parameters_repeat1", + [aux_sym_block_repeat1] = "block_repeat1", +}; + +static const TSSymbol ts_symbol_map[] = { + [ts_builtin_sym_end] = ts_builtin_sym_end, + [sym_identifier] = sym_identifier, + [anon_sym_func] = anon_sym_func, + [anon_sym_LPAREN] = anon_sym_LPAREN, + [anon_sym_COMMA] = anon_sym_COMMA, + [anon_sym_RPAREN] = anon_sym_RPAREN, + [anon_sym_] = anon_sym_, + [anon_sym_LBRACE] = anon_sym_LBRACE, + [anon_sym_RBRACE] = anon_sym_RBRACE, + [anon_sym_if] = anon_sym_if, + [anon_sym_SEMI] = anon_sym_SEMI, + [anon_sym_else] = anon_sym_else, + [anon_sym_return] = anon_sym_return, + [anon_sym_EQ] = anon_sym_EQ, + [anon_sym_DASH] = anon_sym_DASH, + [anon_sym_BANG] = anon_sym_BANG, + [anon_sym_STAR] = anon_sym_STAR, + [anon_sym_PLUS] = anon_sym_PLUS, + [anon_sym_PIPE_PIPE] = anon_sym_PIPE_PIPE, + [anon_sym_AMP_AMP] = anon_sym_AMP_AMP, + [sym_integer_literal] = sym_integer_literal, + [anon_sym_true] = anon_sym_true, + [anon_sym_false] = anon_sym_false, + [anon_sym_bool] = anon_sym_bool, + [anon_sym_int] = anon_sym_int, + [anon_sym_uint] = anon_sym_uint, + [sym_source_file] = sym_source_file, + [sym__definition] = sym__definition, + [sym_function_definition] = sym_function_definition, + [sym_parameters] = sym_parameters, + [sym_parameter] = sym_parameter, + [sym_block] = sym_block, + [sym__statement] = sym__statement, + [sym_if_statement] = sym_if_statement, + [sym_if_expression] = sym_if_expression, + [sym_return_statement] = sym_return_statement, + [sym_assign_statement] = sym_assign_statement, + [sym__expression] = sym__expression, + [sym_unary_expression] = sym_unary_expression, + [sym_binary_expression] = sym_binary_expression, + [sym_boolean_literal] = sym_boolean_literal, + [sym__type] = sym__type, + [sym_primitive_type] = sym_primitive_type, + [aux_sym_source_file_repeat1] = aux_sym_source_file_repeat1, + [aux_sym_parameters_repeat1] = aux_sym_parameters_repeat1, + [aux_sym_block_repeat1] = aux_sym_block_repeat1, +}; + +static const TSSymbolMetadata ts_symbol_metadata[] = { + [ts_builtin_sym_end] = { + .visible = false, + .named = true, + }, + [sym_identifier] = { + .visible = true, + .named = true, + }, + [anon_sym_func] = { + .visible = true, + .named = false, + }, + [anon_sym_LPAREN] = { + .visible = true, + .named = false, + }, + [anon_sym_COMMA] = { + .visible = true, + .named = false, + }, + [anon_sym_RPAREN] = { + .visible = true, + .named = false, + }, + [anon_sym_] = { + .visible = true, + .named = false, + }, + [anon_sym_LBRACE] = { + .visible = true, + .named = false, + }, + [anon_sym_RBRACE] = { + .visible = true, + .named = false, + }, + [anon_sym_if] = { + .visible = true, + .named = false, + }, + [anon_sym_SEMI] = { + .visible = true, + .named = false, + }, + [anon_sym_else] = { + .visible = true, + .named = false, + }, + [anon_sym_return] = { + .visible = true, + .named = false, + }, + [anon_sym_EQ] = { + .visible = true, + .named = false, + }, + [anon_sym_DASH] = { + .visible = true, + .named = false, + }, + [anon_sym_BANG] = { + .visible = true, + .named = false, + }, + [anon_sym_STAR] = { + .visible = true, + .named = false, + }, + [anon_sym_PLUS] = { + .visible = true, + .named = false, + }, + [anon_sym_PIPE_PIPE] = { + .visible = true, + .named = false, + }, + [anon_sym_AMP_AMP] = { + .visible = true, + .named = false, + }, + [sym_integer_literal] = { + .visible = true, + .named = true, + }, + [anon_sym_true] = { + .visible = true, + .named = false, + }, + [anon_sym_false] = { + .visible = true, + .named = false, + }, + [anon_sym_bool] = { + .visible = true, + .named = false, + }, + [anon_sym_int] = { + .visible = true, + .named = false, + }, + [anon_sym_uint] = { + .visible = true, + .named = false, + }, + [sym_source_file] = { + .visible = true, + .named = true, + }, + [sym__definition] = { + .visible = false, + .named = true, + }, + [sym_function_definition] = { + .visible = true, + .named = true, + }, + [sym_parameters] = { + .visible = true, + .named = true, + }, + [sym_parameter] = { + .visible = true, + .named = true, + }, + [sym_block] = { + .visible = true, + .named = true, + }, + [sym__statement] = { + .visible = false, + .named = true, + }, + [sym_if_statement] = { + .visible = true, + .named = true, + }, + [sym_if_expression] = { + .visible = true, + .named = true, + }, + [sym_return_statement] = { + .visible = true, + .named = true, + }, + [sym_assign_statement] = { + .visible = true, + .named = true, + }, + [sym__expression] = { + .visible = false, + .named = true, + }, + [sym_unary_expression] = { + .visible = true, + .named = true, + }, + [sym_binary_expression] = { + .visible = true, + .named = true, + }, + [sym_boolean_literal] = { + .visible = true, + .named = true, + }, + [sym__type] = { + .visible = false, + .named = true, + }, + [sym_primitive_type] = { + .visible = true, + .named = true, + }, + [aux_sym_source_file_repeat1] = { + .visible = false, + .named = false, + }, + [aux_sym_parameters_repeat1] = { + .visible = false, + .named = false, + }, + [aux_sym_block_repeat1] = { + .visible = false, + .named = false, + }, +}; + +enum { + field_body = 1, + field_condition = 2, + field_else = 3, + field_name = 4, + field_parameters = 5, + field_return_type = 6, +}; + +static const char * const ts_field_names[] = { + [0] = NULL, + [field_body] = "body", + [field_condition] = "condition", + [field_else] = "else", + [field_name] = "name", + [field_parameters] = "parameters", + [field_return_type] = "return_type", +}; + +static const TSFieldMapSlice ts_field_map_slices[PRODUCTION_ID_COUNT] = { + [1] = {.index = 0, .length = 3}, + [2] = {.index = 3, .length = 4}, + [3] = {.index = 7, .length = 2}, + [4] = {.index = 9, .length = 4}, +}; + +static const TSFieldMapEntry ts_field_map_entries[] = { + [0] = + {field_body, 3}, + {field_name, 1}, + {field_parameters, 2}, + [3] = + {field_body, 4}, + {field_name, 1}, + {field_parameters, 2}, + {field_return_type, 3}, + [7] = + {field_body, 2}, + {field_condition, 1}, + [9] = + {field_body, 2}, + {field_condition, 1}, + {field_else, 3}, + {field_else, 4}, +}; + +static const TSSymbol ts_alias_sequences[PRODUCTION_ID_COUNT][MAX_ALIAS_SEQUENCE_LENGTH] = { + [0] = {0}, +}; + +static const uint16_t ts_non_terminal_alias_map[] = { + 0, +}; + +static const TSStateId ts_primary_state_ids[STATE_COUNT] = { + [0] = 0, + [1] = 1, + [2] = 2, + [3] = 3, + [4] = 4, + [5] = 5, + [6] = 6, + [7] = 7, + [8] = 8, + [9] = 9, + [10] = 10, + [11] = 11, + [12] = 12, + [13] = 13, + [14] = 14, + [15] = 15, + [16] = 16, + [17] = 17, + [18] = 18, + [19] = 19, + [20] = 20, + [21] = 21, + [22] = 22, + [23] = 23, + [24] = 24, + [25] = 25, + [26] = 26, + [27] = 27, + [28] = 28, + [29] = 29, + [30] = 30, + [31] = 31, + [32] = 32, + [33] = 33, + [34] = 34, + [35] = 35, + [36] = 36, + [37] = 37, + [38] = 38, + [39] = 39, + [40] = 40, + [41] = 41, + [42] = 42, + [43] = 43, + [44] = 44, + [45] = 45, + [46] = 46, + [47] = 47, + [48] = 48, + [49] = 49, + [50] = 50, + [51] = 51, + [52] = 52, + [53] = 53, + [54] = 54, + [55] = 55, + [56] = 56, + [57] = 57, + [58] = 58, + [59] = 59, + [60] = 60, + [61] = 61, + [62] = 62, + [63] = 63, + [64] = 64, + [65] = 65, + [66] = 66, + [67] = 67, + [68] = 68, +}; + +static bool ts_lex(TSLexer *lexer, TSStateId state) { + START_LEXER(); + eof = lexer->eof(lexer); + switch (state) { + case 0: + if (eof) ADVANCE(4); + if (lookahead == '!') ADVANCE(14); + if (lookahead == '&') ADVANCE(2); + if (lookahead == '(') ADVANCE(5); + if (lookahead == ')') ADVANCE(7); + if (lookahead == '*') ADVANCE(15); + if (lookahead == '+') ADVANCE(16); + if (lookahead == ',') ADVANCE(6); + if (lookahead == '-') ADVANCE(13); + if (lookahead == ';') ADVANCE(11); + if (lookahead == '=') ADVANCE(12); + if (lookahead == '{') ADVANCE(9); + if (lookahead == '|') ADVANCE(3); + if (lookahead == '}') ADVANCE(10); + if (lookahead == '\t' || + lookahead == '\n' || + lookahead == '\r' || + lookahead == ' ') SKIP(0) + if (('0' <= lookahead && lookahead <= '9')) ADVANCE(21); + if (lookahead == '_' || + ('a' <= lookahead && lookahead <= 'z')) ADVANCE(19); + END_STATE(); + case 1: + if (lookahead == ' ') ADVANCE(8); + if (lookahead == '\t' || + lookahead == '\n' || + lookahead == '\r') SKIP(1) + END_STATE(); + case 2: + if (lookahead == '&') ADVANCE(18); + END_STATE(); + case 3: + if (lookahead == '|') ADVANCE(17); + END_STATE(); + case 4: + ACCEPT_TOKEN(ts_builtin_sym_end); + END_STATE(); + case 5: + ACCEPT_TOKEN(anon_sym_LPAREN); + END_STATE(); + case 6: + ACCEPT_TOKEN(anon_sym_COMMA); + END_STATE(); + case 7: + ACCEPT_TOKEN(anon_sym_RPAREN); + END_STATE(); + case 8: + ACCEPT_TOKEN(anon_sym_); + if (lookahead == ' ') ADVANCE(8); + END_STATE(); + case 9: + ACCEPT_TOKEN(anon_sym_LBRACE); + END_STATE(); + case 10: + ACCEPT_TOKEN(anon_sym_RBRACE); + END_STATE(); + case 11: + ACCEPT_TOKEN(anon_sym_SEMI); + END_STATE(); + case 12: + ACCEPT_TOKEN(anon_sym_EQ); + END_STATE(); + case 13: + ACCEPT_TOKEN(anon_sym_DASH); + END_STATE(); + case 14: + ACCEPT_TOKEN(anon_sym_BANG); + END_STATE(); + case 15: + ACCEPT_TOKEN(anon_sym_STAR); + END_STATE(); + case 16: + ACCEPT_TOKEN(anon_sym_PLUS); + END_STATE(); + case 17: + ACCEPT_TOKEN(anon_sym_PIPE_PIPE); + END_STATE(); + case 18: + ACCEPT_TOKEN(anon_sym_AMP_AMP); + END_STATE(); + case 19: + ACCEPT_TOKEN(sym_identifier); + if (lookahead == '_' || + ('a' <= lookahead && lookahead <= 'z')) ADVANCE(19); + if (('0' <= lookahead && lookahead <= '9') || + ('A' <= lookahead && lookahead <= 'Z')) ADVANCE(20); + END_STATE(); + case 20: + ACCEPT_TOKEN(sym_identifier); + if (('0' <= lookahead && lookahead <= '9') || + ('A' <= lookahead && lookahead <= 'Z') || + lookahead == '_' || + ('a' <= lookahead && lookahead <= 'z')) ADVANCE(20); + END_STATE(); + case 21: + ACCEPT_TOKEN(sym_integer_literal); + if (('0' <= lookahead && lookahead <= '9')) ADVANCE(21); + END_STATE(); + default: + return false; + } +} + +static bool ts_lex_keywords(TSLexer *lexer, TSStateId state) { + START_LEXER(); + eof = lexer->eof(lexer); + switch (state) { + case 0: + if (lookahead == 'b') ADVANCE(1); + if (lookahead == 'e') ADVANCE(2); + if (lookahead == 'f') ADVANCE(3); + if (lookahead == 'i') ADVANCE(4); + if (lookahead == 'r') ADVANCE(5); + if (lookahead == 't') ADVANCE(6); + if (lookahead == 'u') ADVANCE(7); + if (lookahead == '\t' || + lookahead == '\n' || + lookahead == '\r' || + lookahead == ' ') SKIP(0) + END_STATE(); + case 1: + if (lookahead == 'o') ADVANCE(8); + END_STATE(); + case 2: + if (lookahead == 'l') ADVANCE(9); + END_STATE(); + case 3: + if (lookahead == 'a') ADVANCE(10); + if (lookahead == 'u') ADVANCE(11); + END_STATE(); + case 4: + if (lookahead == 'f') ADVANCE(12); + if (lookahead == 'n') ADVANCE(13); + END_STATE(); + case 5: + if (lookahead == 'e') ADVANCE(14); + END_STATE(); + case 6: + if (lookahead == 'r') ADVANCE(15); + END_STATE(); + case 7: + if (lookahead == 'i') ADVANCE(16); + END_STATE(); + case 8: + if (lookahead == 'o') ADVANCE(17); + END_STATE(); + case 9: + if (lookahead == 's') ADVANCE(18); + END_STATE(); + case 10: + if (lookahead == 'l') ADVANCE(19); + END_STATE(); + case 11: + if (lookahead == 'n') ADVANCE(20); + END_STATE(); + case 12: + ACCEPT_TOKEN(anon_sym_if); + END_STATE(); + case 13: + if (lookahead == 't') ADVANCE(21); + END_STATE(); + case 14: + if (lookahead == 't') ADVANCE(22); + END_STATE(); + case 15: + if (lookahead == 'u') ADVANCE(23); + END_STATE(); + case 16: + if (lookahead == 'n') ADVANCE(24); + END_STATE(); + case 17: + if (lookahead == 'l') ADVANCE(25); + END_STATE(); + case 18: + if (lookahead == 'e') ADVANCE(26); + END_STATE(); + case 19: + if (lookahead == 's') ADVANCE(27); + END_STATE(); + case 20: + if (lookahead == 'c') ADVANCE(28); + END_STATE(); + case 21: + ACCEPT_TOKEN(anon_sym_int); + END_STATE(); + case 22: + if (lookahead == 'u') ADVANCE(29); + END_STATE(); + case 23: + if (lookahead == 'e') ADVANCE(30); + END_STATE(); + case 24: + if (lookahead == 't') ADVANCE(31); + END_STATE(); + case 25: + ACCEPT_TOKEN(anon_sym_bool); + END_STATE(); + case 26: + ACCEPT_TOKEN(anon_sym_else); + END_STATE(); + case 27: + if (lookahead == 'e') ADVANCE(32); + END_STATE(); + case 28: + ACCEPT_TOKEN(anon_sym_func); + END_STATE(); + case 29: + if (lookahead == 'r') ADVANCE(33); + END_STATE(); + case 30: + ACCEPT_TOKEN(anon_sym_true); + END_STATE(); + case 31: + ACCEPT_TOKEN(anon_sym_uint); + END_STATE(); + case 32: + ACCEPT_TOKEN(anon_sym_false); + END_STATE(); + case 33: + if (lookahead == 'n') ADVANCE(34); + END_STATE(); + case 34: + ACCEPT_TOKEN(anon_sym_return); + END_STATE(); + default: + return false; + } +} + +static const TSLexMode ts_lex_modes[STATE_COUNT] = { + [0] = {.lex_state = 0}, + [1] = {.lex_state = 0}, + [2] = {.lex_state = 0}, + [3] = {.lex_state = 0}, + [4] = {.lex_state = 0}, + [5] = {.lex_state = 0}, + [6] = {.lex_state = 0}, + [7] = {.lex_state = 0}, + [8] = {.lex_state = 0}, + [9] = {.lex_state = 0}, + [10] = {.lex_state = 0}, + [11] = {.lex_state = 0}, + [12] = {.lex_state = 0}, + [13] = {.lex_state = 0}, + [14] = {.lex_state = 0}, + [15] = {.lex_state = 0}, + [16] = {.lex_state = 0}, + [17] = {.lex_state = 0}, + [18] = {.lex_state = 0}, + [19] = {.lex_state = 0}, + [20] = {.lex_state = 0}, + [21] = {.lex_state = 0}, + [22] = {.lex_state = 0}, + [23] = {.lex_state = 0}, + [24] = {.lex_state = 0}, + [25] = {.lex_state = 0}, + [26] = {.lex_state = 0}, + [27] = {.lex_state = 0}, + [28] = {.lex_state = 0}, + [29] = {.lex_state = 0}, + [30] = {.lex_state = 0}, + [31] = {.lex_state = 0}, + [32] = {.lex_state = 0}, + [33] = {.lex_state = 0}, + [34] = {.lex_state = 0}, + [35] = {.lex_state = 0}, + [36] = {.lex_state = 0}, + [37] = {.lex_state = 0}, + [38] = {.lex_state = 0}, + [39] = {.lex_state = 0}, + [40] = {.lex_state = 0}, + [41] = {.lex_state = 0}, + [42] = {.lex_state = 0}, + [43] = {.lex_state = 0}, + [44] = {.lex_state = 0}, + [45] = {.lex_state = 0}, + [46] = {.lex_state = 0}, + [47] = {.lex_state = 0}, + [48] = {.lex_state = 0}, + [49] = {.lex_state = 0}, + [50] = {.lex_state = 0}, + [51] = {.lex_state = 0}, + [52] = {.lex_state = 0}, + [53] = {.lex_state = 0}, + [54] = {.lex_state = 0}, + [55] = {.lex_state = 0}, + [56] = {.lex_state = 0}, + [57] = {.lex_state = 0}, + [58] = {.lex_state = 0}, + [59] = {.lex_state = 0}, + [60] = {.lex_state = 0}, + [61] = {.lex_state = 0}, + [62] = {.lex_state = 0}, + [63] = {.lex_state = 0}, + [64] = {.lex_state = 0}, + [65] = {.lex_state = 0}, + [66] = {.lex_state = 1}, + [67] = {.lex_state = 0}, + [68] = {.lex_state = 0}, +}; + +static const uint16_t ts_parse_table[LARGE_STATE_COUNT][SYMBOL_COUNT] = { + [0] = { + [ts_builtin_sym_end] = ACTIONS(1), + [sym_identifier] = ACTIONS(1), + [anon_sym_func] = ACTIONS(1), + [anon_sym_LPAREN] = ACTIONS(1), + [anon_sym_COMMA] = ACTIONS(1), + [anon_sym_RPAREN] = ACTIONS(1), + [anon_sym_LBRACE] = ACTIONS(1), + [anon_sym_RBRACE] = ACTIONS(1), + [anon_sym_if] = ACTIONS(1), + [anon_sym_SEMI] = ACTIONS(1), + [anon_sym_else] = ACTIONS(1), + [anon_sym_return] = ACTIONS(1), + [anon_sym_EQ] = ACTIONS(1), + [anon_sym_DASH] = ACTIONS(1), + [anon_sym_BANG] = ACTIONS(1), + [anon_sym_STAR] = ACTIONS(1), + [anon_sym_PLUS] = ACTIONS(1), + [anon_sym_PIPE_PIPE] = ACTIONS(1), + [anon_sym_AMP_AMP] = ACTIONS(1), + [sym_integer_literal] = ACTIONS(1), + [anon_sym_true] = ACTIONS(1), + [anon_sym_false] = ACTIONS(1), + [anon_sym_bool] = ACTIONS(1), + [anon_sym_int] = ACTIONS(1), + [anon_sym_uint] = ACTIONS(1), + }, + [1] = { + [sym_source_file] = STATE(67), + [sym__definition] = STATE(39), + [sym_function_definition] = STATE(39), + [aux_sym_source_file_repeat1] = STATE(39), + [ts_builtin_sym_end] = ACTIONS(3), + [anon_sym_func] = ACTIONS(5), + }, +}; + +static const uint16_t ts_small_parse_table[] = { + [0] = 9, + ACTIONS(7), 1, + sym_identifier, + ACTIONS(9), 1, + anon_sym_RBRACE, + ACTIONS(11), 1, + anon_sym_if, + ACTIONS(13), 1, + anon_sym_return, + ACTIONS(17), 1, + sym_integer_literal, + ACTIONS(15), 2, + anon_sym_DASH, + anon_sym_BANG, + ACTIONS(19), 2, + anon_sym_true, + anon_sym_false, + STATE(5), 5, + sym__statement, + sym_if_statement, + sym_return_statement, + sym_assign_statement, + aux_sym_block_repeat1, + STATE(42), 5, + sym_if_expression, + sym__expression, + sym_unary_expression, + sym_binary_expression, + sym_boolean_literal, + [38] = 9, + ACTIONS(7), 1, + sym_identifier, + ACTIONS(11), 1, + anon_sym_if, + ACTIONS(13), 1, + anon_sym_return, + ACTIONS(21), 1, + anon_sym_RBRACE, + ACTIONS(23), 1, + sym_integer_literal, + ACTIONS(15), 2, + anon_sym_DASH, + anon_sym_BANG, + ACTIONS(19), 2, + anon_sym_true, + anon_sym_false, + STATE(2), 5, + sym__statement, + sym_if_statement, + sym_return_statement, + sym_assign_statement, + aux_sym_block_repeat1, + STATE(36), 5, + sym_if_expression, + sym__expression, + sym_unary_expression, + sym_binary_expression, + sym_boolean_literal, + [76] = 8, + ACTIONS(25), 1, + sym_identifier, + ACTIONS(27), 1, + anon_sym_LBRACE, + ACTIONS(29), 1, + anon_sym_if, + ACTIONS(31), 1, + sym_integer_literal, + STATE(24), 1, + sym_block, + ACTIONS(15), 2, + anon_sym_DASH, + anon_sym_BANG, + ACTIONS(19), 2, + anon_sym_true, + anon_sym_false, + STATE(27), 5, + sym_if_expression, + sym__expression, + sym_unary_expression, + sym_binary_expression, + sym_boolean_literal, + [107] = 6, + ACTIONS(33), 1, + sym_identifier, + ACTIONS(38), 1, + anon_sym_if, + ACTIONS(41), 1, + anon_sym_return, + ACTIONS(44), 2, + anon_sym_true, + anon_sym_false, + ACTIONS(36), 4, + anon_sym_RBRACE, + anon_sym_DASH, + anon_sym_BANG, + sym_integer_literal, + STATE(5), 5, + sym__statement, + sym_if_statement, + sym_return_statement, + sym_assign_statement, + aux_sym_block_repeat1, + [134] = 7, + ACTIONS(29), 1, + anon_sym_if, + ACTIONS(46), 1, + sym_identifier, + ACTIONS(48), 1, + anon_sym_SEMI, + ACTIONS(50), 1, + sym_integer_literal, + ACTIONS(15), 2, + anon_sym_DASH, + anon_sym_BANG, + ACTIONS(19), 2, + anon_sym_true, + anon_sym_false, + STATE(37), 5, + sym_if_expression, + sym__expression, + sym_unary_expression, + sym_binary_expression, + sym_boolean_literal, + [162] = 6, + ACTIONS(29), 1, + anon_sym_if, + ACTIONS(52), 1, + sym_identifier, + ACTIONS(54), 1, + sym_integer_literal, + ACTIONS(15), 2, + anon_sym_DASH, + anon_sym_BANG, + ACTIONS(19), 2, + anon_sym_true, + anon_sym_false, + STATE(35), 5, + sym_if_expression, + sym__expression, + sym_unary_expression, + sym_binary_expression, + sym_boolean_literal, + [187] = 6, + ACTIONS(29), 1, + anon_sym_if, + ACTIONS(56), 1, + sym_identifier, + ACTIONS(58), 1, + sym_integer_literal, + ACTIONS(15), 2, + anon_sym_DASH, + anon_sym_BANG, + ACTIONS(19), 2, + anon_sym_true, + anon_sym_false, + STATE(29), 5, + sym_if_expression, + sym__expression, + sym_unary_expression, + sym_binary_expression, + sym_boolean_literal, + [212] = 6, + ACTIONS(29), 1, + anon_sym_if, + ACTIONS(60), 1, + sym_identifier, + ACTIONS(62), 1, + sym_integer_literal, + ACTIONS(15), 2, + anon_sym_DASH, + anon_sym_BANG, + ACTIONS(19), 2, + anon_sym_true, + anon_sym_false, + STATE(33), 5, + sym_if_expression, + sym__expression, + sym_unary_expression, + sym_binary_expression, + sym_boolean_literal, + [237] = 6, + ACTIONS(29), 1, + anon_sym_if, + ACTIONS(64), 1, + sym_identifier, + ACTIONS(66), 1, + sym_integer_literal, + ACTIONS(15), 2, + anon_sym_DASH, + anon_sym_BANG, + ACTIONS(19), 2, + anon_sym_true, + anon_sym_false, + STATE(26), 5, + sym_if_expression, + sym__expression, + sym_unary_expression, + sym_binary_expression, + sym_boolean_literal, + [262] = 6, + ACTIONS(29), 1, + anon_sym_if, + ACTIONS(68), 1, + sym_identifier, + ACTIONS(70), 1, + sym_integer_literal, + ACTIONS(15), 2, + anon_sym_DASH, + anon_sym_BANG, + ACTIONS(19), 2, + anon_sym_true, + anon_sym_false, + STATE(40), 5, + sym_if_expression, + sym__expression, + sym_unary_expression, + sym_binary_expression, + sym_boolean_literal, + [287] = 6, + ACTIONS(29), 1, + anon_sym_if, + ACTIONS(72), 1, + sym_identifier, + ACTIONS(74), 1, + sym_integer_literal, + ACTIONS(15), 2, + anon_sym_DASH, + anon_sym_BANG, + ACTIONS(19), 2, + anon_sym_true, + anon_sym_false, + STATE(31), 5, + sym_if_expression, + sym__expression, + sym_unary_expression, + sym_binary_expression, + sym_boolean_literal, + [312] = 6, + ACTIONS(29), 1, + anon_sym_if, + ACTIONS(76), 1, + sym_identifier, + ACTIONS(78), 1, + sym_integer_literal, + ACTIONS(15), 2, + anon_sym_DASH, + anon_sym_BANG, + ACTIONS(19), 2, + anon_sym_true, + anon_sym_false, + STATE(30), 5, + sym_if_expression, + sym__expression, + sym_unary_expression, + sym_binary_expression, + sym_boolean_literal, + [337] = 6, + ACTIONS(29), 1, + anon_sym_if, + ACTIONS(80), 1, + sym_identifier, + ACTIONS(82), 1, + sym_integer_literal, + ACTIONS(15), 2, + anon_sym_DASH, + anon_sym_BANG, + ACTIONS(19), 2, + anon_sym_true, + anon_sym_false, + STATE(32), 5, + sym_if_expression, + sym__expression, + sym_unary_expression, + sym_binary_expression, + sym_boolean_literal, + [362] = 6, + ACTIONS(29), 1, + anon_sym_if, + ACTIONS(84), 1, + sym_identifier, + ACTIONS(86), 1, + sym_integer_literal, + ACTIONS(15), 2, + anon_sym_DASH, + anon_sym_BANG, + ACTIONS(19), 2, + anon_sym_true, + anon_sym_false, + STATE(28), 5, + sym_if_expression, + sym__expression, + sym_unary_expression, + sym_binary_expression, + sym_boolean_literal, + [387] = 1, + ACTIONS(88), 10, + ts_builtin_sym_end, + anon_sym_func, + anon_sym_LBRACE, + anon_sym_RBRACE, + anon_sym_SEMI, + anon_sym_else, + anon_sym_STAR, + anon_sym_PLUS, + anon_sym_PIPE_PIPE, + anon_sym_AMP_AMP, + [400] = 1, + ACTIONS(90), 10, + ts_builtin_sym_end, + anon_sym_func, + anon_sym_LBRACE, + anon_sym_RBRACE, + anon_sym_SEMI, + anon_sym_else, + anon_sym_STAR, + anon_sym_PLUS, + anon_sym_PIPE_PIPE, + anon_sym_AMP_AMP, + [413] = 1, + ACTIONS(92), 10, + ts_builtin_sym_end, + anon_sym_func, + anon_sym_LBRACE, + anon_sym_RBRACE, + anon_sym_SEMI, + anon_sym_else, + anon_sym_STAR, + anon_sym_PLUS, + anon_sym_PIPE_PIPE, + anon_sym_AMP_AMP, + [426] = 2, + ACTIONS(96), 4, + anon_sym_RBRACE, + anon_sym_DASH, + anon_sym_BANG, + sym_integer_literal, + ACTIONS(94), 5, + anon_sym_if, + anon_sym_return, + sym_identifier, + anon_sym_true, + anon_sym_false, + [440] = 2, + ACTIONS(100), 4, + anon_sym_RBRACE, + anon_sym_DASH, + anon_sym_BANG, + sym_integer_literal, + ACTIONS(98), 5, + anon_sym_if, + anon_sym_return, + sym_identifier, + anon_sym_true, + anon_sym_false, + [454] = 2, + ACTIONS(104), 4, + anon_sym_RBRACE, + anon_sym_DASH, + anon_sym_BANG, + sym_integer_literal, + ACTIONS(102), 5, + anon_sym_if, + anon_sym_return, + sym_identifier, + anon_sym_true, + anon_sym_false, + [468] = 2, + ACTIONS(108), 4, + anon_sym_RBRACE, + anon_sym_DASH, + anon_sym_BANG, + sym_integer_literal, + ACTIONS(106), 5, + anon_sym_if, + anon_sym_return, + sym_identifier, + anon_sym_true, + anon_sym_false, + [482] = 4, + ACTIONS(27), 1, + anon_sym_LBRACE, + STATE(58), 1, + sym_block, + STATE(51), 2, + sym__type, + sym_primitive_type, + ACTIONS(110), 3, + anon_sym_bool, + anon_sym_int, + anon_sym_uint, + [498] = 1, + ACTIONS(112), 7, + anon_sym_LBRACE, + anon_sym_RBRACE, + anon_sym_SEMI, + anon_sym_STAR, + anon_sym_PLUS, + anon_sym_PIPE_PIPE, + anon_sym_AMP_AMP, + [508] = 1, + ACTIONS(114), 7, + anon_sym_LBRACE, + anon_sym_RBRACE, + anon_sym_SEMI, + anon_sym_STAR, + anon_sym_PLUS, + anon_sym_PIPE_PIPE, + anon_sym_AMP_AMP, + [518] = 1, + ACTIONS(116), 7, + anon_sym_LBRACE, + anon_sym_RBRACE, + anon_sym_SEMI, + anon_sym_STAR, + anon_sym_PLUS, + anon_sym_PIPE_PIPE, + anon_sym_AMP_AMP, + [528] = 5, + ACTIONS(118), 1, + anon_sym_STAR, + ACTIONS(120), 1, + anon_sym_PLUS, + ACTIONS(122), 1, + anon_sym_PIPE_PIPE, + ACTIONS(124), 1, + anon_sym_AMP_AMP, + ACTIONS(112), 3, + anon_sym_LBRACE, + anon_sym_RBRACE, + anon_sym_SEMI, + [546] = 4, + ACTIONS(118), 1, + anon_sym_STAR, + ACTIONS(120), 1, + anon_sym_PLUS, + ACTIONS(122), 1, + anon_sym_PIPE_PIPE, + ACTIONS(126), 4, + anon_sym_LBRACE, + anon_sym_RBRACE, + anon_sym_SEMI, + anon_sym_AMP_AMP, + [562] = 3, + ACTIONS(118), 1, + anon_sym_STAR, + ACTIONS(120), 1, + anon_sym_PLUS, + ACTIONS(126), 5, + anon_sym_LBRACE, + anon_sym_RBRACE, + anon_sym_SEMI, + anon_sym_PIPE_PIPE, + anon_sym_AMP_AMP, + [576] = 2, + ACTIONS(118), 1, + anon_sym_STAR, + ACTIONS(126), 6, + anon_sym_LBRACE, + anon_sym_RBRACE, + anon_sym_SEMI, + anon_sym_PLUS, + anon_sym_PIPE_PIPE, + anon_sym_AMP_AMP, + [588] = 1, + ACTIONS(126), 7, + anon_sym_LBRACE, + anon_sym_RBRACE, + anon_sym_SEMI, + anon_sym_STAR, + anon_sym_PLUS, + anon_sym_PIPE_PIPE, + anon_sym_AMP_AMP, + [598] = 6, + ACTIONS(27), 1, + anon_sym_LBRACE, + ACTIONS(118), 1, + anon_sym_STAR, + ACTIONS(120), 1, + anon_sym_PLUS, + ACTIONS(122), 1, + anon_sym_PIPE_PIPE, + ACTIONS(124), 1, + anon_sym_AMP_AMP, + STATE(68), 1, + sym_block, + [617] = 6, + ACTIONS(27), 1, + anon_sym_LBRACE, + ACTIONS(118), 1, + anon_sym_STAR, + ACTIONS(120), 1, + anon_sym_PLUS, + ACTIONS(122), 1, + anon_sym_PIPE_PIPE, + ACTIONS(124), 1, + anon_sym_AMP_AMP, + STATE(65), 1, + sym_block, + [636] = 2, + ACTIONS(130), 1, + anon_sym_EQ, + ACTIONS(128), 5, + anon_sym_RBRACE, + anon_sym_STAR, + anon_sym_PLUS, + anon_sym_PIPE_PIPE, + anon_sym_AMP_AMP, + [647] = 6, + ACTIONS(27), 1, + anon_sym_LBRACE, + ACTIONS(118), 1, + anon_sym_STAR, + ACTIONS(120), 1, + anon_sym_PLUS, + ACTIONS(122), 1, + anon_sym_PIPE_PIPE, + ACTIONS(124), 1, + anon_sym_AMP_AMP, + STATE(52), 1, + sym_block, + [666] = 5, + ACTIONS(9), 1, + anon_sym_RBRACE, + ACTIONS(118), 1, + anon_sym_STAR, + ACTIONS(120), 1, + anon_sym_PLUS, + ACTIONS(122), 1, + anon_sym_PIPE_PIPE, + ACTIONS(124), 1, + anon_sym_AMP_AMP, + [682] = 5, + ACTIONS(118), 1, + anon_sym_STAR, + ACTIONS(120), 1, + anon_sym_PLUS, + ACTIONS(122), 1, + anon_sym_PIPE_PIPE, + ACTIONS(124), 1, + anon_sym_AMP_AMP, + ACTIONS(132), 1, + anon_sym_SEMI, + [698] = 3, + ACTIONS(134), 1, + ts_builtin_sym_end, + ACTIONS(136), 1, + anon_sym_func, + STATE(38), 3, + sym__definition, + sym_function_definition, + aux_sym_source_file_repeat1, + [710] = 3, + ACTIONS(5), 1, + anon_sym_func, + ACTIONS(139), 1, + ts_builtin_sym_end, + STATE(38), 3, + sym__definition, + sym_function_definition, + aux_sym_source_file_repeat1, + [722] = 5, + ACTIONS(118), 1, + anon_sym_STAR, + ACTIONS(120), 1, + anon_sym_PLUS, + ACTIONS(122), 1, + anon_sym_PIPE_PIPE, + ACTIONS(124), 1, + anon_sym_AMP_AMP, + ACTIONS(141), 1, + anon_sym_SEMI, + [738] = 2, + STATE(54), 2, + sym__type, + sym_primitive_type, + ACTIONS(110), 3, + anon_sym_bool, + anon_sym_int, + anon_sym_uint, + [748] = 5, + ACTIONS(118), 1, + anon_sym_STAR, + ACTIONS(120), 1, + anon_sym_PLUS, + ACTIONS(122), 1, + anon_sym_PIPE_PIPE, + ACTIONS(124), 1, + anon_sym_AMP_AMP, + ACTIONS(143), 1, + anon_sym_RBRACE, + [764] = 1, + ACTIONS(145), 4, + anon_sym_LBRACE, + anon_sym_bool, + anon_sym_int, + anon_sym_uint, + [771] = 1, + ACTIONS(147), 4, + anon_sym_LBRACE, + anon_sym_bool, + anon_sym_int, + anon_sym_uint, + [778] = 1, + ACTIONS(149), 4, + anon_sym_LBRACE, + anon_sym_bool, + anon_sym_int, + anon_sym_uint, + [785] = 1, + ACTIONS(151), 4, + anon_sym_LBRACE, + anon_sym_bool, + anon_sym_int, + anon_sym_uint, + [792] = 4, + ACTIONS(153), 1, + sym_identifier, + ACTIONS(155), 1, + anon_sym_RPAREN, + STATE(50), 1, + aux_sym_parameters_repeat1, + STATE(59), 1, + sym_parameter, + [805] = 3, + ACTIONS(157), 1, + sym_identifier, + STATE(48), 1, + aux_sym_parameters_repeat1, + STATE(63), 1, + sym_parameter, + [815] = 1, + ACTIONS(160), 3, + anon_sym_COMMA, + anon_sym_RPAREN, + anon_sym_LBRACE, + [821] = 3, + ACTIONS(153), 1, + sym_identifier, + STATE(48), 1, + aux_sym_parameters_repeat1, + STATE(56), 1, + sym_parameter, + [831] = 2, + ACTIONS(27), 1, + anon_sym_LBRACE, + STATE(55), 1, + sym_block, + [838] = 2, + ACTIONS(162), 1, + anon_sym_SEMI, + ACTIONS(164), 1, + anon_sym_else, + [845] = 2, + ACTIONS(166), 1, + sym_identifier, + ACTIONS(168), 1, + anon_sym_RPAREN, + [852] = 1, + ACTIONS(170), 2, + anon_sym_COMMA, + anon_sym_RPAREN, + [857] = 1, + ACTIONS(172), 2, + ts_builtin_sym_end, + anon_sym_func, + [862] = 2, + ACTIONS(174), 1, + anon_sym_COMMA, + ACTIONS(176), 1, + anon_sym_RPAREN, + [869] = 2, + ACTIONS(166), 1, + sym_identifier, + ACTIONS(176), 1, + anon_sym_RPAREN, + [876] = 1, + ACTIONS(178), 2, + ts_builtin_sym_end, + anon_sym_func, + [881] = 2, + ACTIONS(180), 1, + anon_sym_COMMA, + ACTIONS(182), 1, + anon_sym_RPAREN, + [888] = 2, + ACTIONS(184), 1, + anon_sym_LPAREN, + STATE(23), 1, + sym_parameters, + [895] = 1, + ACTIONS(166), 1, + sym_identifier, + [899] = 1, + ACTIONS(186), 1, + sym_identifier, + [903] = 1, + ACTIONS(188), 1, + anon_sym_COMMA, + [907] = 1, + ACTIONS(130), 1, + anon_sym_EQ, + [911] = 1, + ACTIONS(164), 1, + anon_sym_else, + [915] = 1, + ACTIONS(190), 1, + anon_sym_, + [919] = 1, + ACTIONS(192), 1, + ts_builtin_sym_end, + [923] = 1, + ACTIONS(162), 1, + anon_sym_SEMI, +}; + +static const uint32_t ts_small_parse_table_map[] = { + [SMALL_STATE(2)] = 0, + [SMALL_STATE(3)] = 38, + [SMALL_STATE(4)] = 76, + [SMALL_STATE(5)] = 107, + [SMALL_STATE(6)] = 134, + [SMALL_STATE(7)] = 162, + [SMALL_STATE(8)] = 187, + [SMALL_STATE(9)] = 212, + [SMALL_STATE(10)] = 237, + [SMALL_STATE(11)] = 262, + [SMALL_STATE(12)] = 287, + [SMALL_STATE(13)] = 312, + [SMALL_STATE(14)] = 337, + [SMALL_STATE(15)] = 362, + [SMALL_STATE(16)] = 387, + [SMALL_STATE(17)] = 400, + [SMALL_STATE(18)] = 413, + [SMALL_STATE(19)] = 426, + [SMALL_STATE(20)] = 440, + [SMALL_STATE(21)] = 454, + [SMALL_STATE(22)] = 468, + [SMALL_STATE(23)] = 482, + [SMALL_STATE(24)] = 498, + [SMALL_STATE(25)] = 508, + [SMALL_STATE(26)] = 518, + [SMALL_STATE(27)] = 528, + [SMALL_STATE(28)] = 546, + [SMALL_STATE(29)] = 562, + [SMALL_STATE(30)] = 576, + [SMALL_STATE(31)] = 588, + [SMALL_STATE(32)] = 598, + [SMALL_STATE(33)] = 617, + [SMALL_STATE(34)] = 636, + [SMALL_STATE(35)] = 647, + [SMALL_STATE(36)] = 666, + [SMALL_STATE(37)] = 682, + [SMALL_STATE(38)] = 698, + [SMALL_STATE(39)] = 710, + [SMALL_STATE(40)] = 722, + [SMALL_STATE(41)] = 738, + [SMALL_STATE(42)] = 748, + [SMALL_STATE(43)] = 764, + [SMALL_STATE(44)] = 771, + [SMALL_STATE(45)] = 778, + [SMALL_STATE(46)] = 785, + [SMALL_STATE(47)] = 792, + [SMALL_STATE(48)] = 805, + [SMALL_STATE(49)] = 815, + [SMALL_STATE(50)] = 821, + [SMALL_STATE(51)] = 831, + [SMALL_STATE(52)] = 838, + [SMALL_STATE(53)] = 845, + [SMALL_STATE(54)] = 852, + [SMALL_STATE(55)] = 857, + [SMALL_STATE(56)] = 862, + [SMALL_STATE(57)] = 869, + [SMALL_STATE(58)] = 876, + [SMALL_STATE(59)] = 881, + [SMALL_STATE(60)] = 888, + [SMALL_STATE(61)] = 895, + [SMALL_STATE(62)] = 899, + [SMALL_STATE(63)] = 903, + [SMALL_STATE(64)] = 907, + [SMALL_STATE(65)] = 911, + [SMALL_STATE(66)] = 915, + [SMALL_STATE(67)] = 919, + [SMALL_STATE(68)] = 923, +}; + +static const TSParseActionEntry ts_parse_actions[] = { + [0] = {.entry = {.count = 0, .reusable = false}}, + [1] = {.entry = {.count = 1, .reusable = false}}, RECOVER(), + [3] = {.entry = {.count = 1, .reusable = true}}, REDUCE(sym_source_file, 0), + [5] = {.entry = {.count = 1, .reusable = true}}, SHIFT(62), + [7] = {.entry = {.count = 1, .reusable = false}}, SHIFT(34), + [9] = {.entry = {.count = 1, .reusable = true}}, SHIFT(16), + [11] = {.entry = {.count = 1, .reusable = false}}, SHIFT(7), + [13] = {.entry = {.count = 1, .reusable = false}}, SHIFT(6), + [15] = {.entry = {.count = 1, .reusable = true}}, SHIFT(10), + [17] = {.entry = {.count = 1, .reusable = true}}, SHIFT(42), + [19] = {.entry = {.count = 1, .reusable = false}}, SHIFT(25), + [21] = {.entry = {.count = 1, .reusable = true}}, SHIFT(18), + [23] = {.entry = {.count = 1, .reusable = true}}, SHIFT(36), + [25] = {.entry = {.count = 1, .reusable = false}}, SHIFT(27), + [27] = {.entry = {.count = 1, .reusable = true}}, SHIFT(3), + [29] = {.entry = {.count = 1, .reusable = false}}, SHIFT(9), + [31] = {.entry = {.count = 1, .reusable = true}}, SHIFT(27), + [33] = {.entry = {.count = 2, .reusable = false}}, REDUCE(aux_sym_block_repeat1, 2), SHIFT_REPEAT(64), + [36] = {.entry = {.count = 1, .reusable = true}}, REDUCE(aux_sym_block_repeat1, 2), + [38] = {.entry = {.count = 2, .reusable = false}}, REDUCE(aux_sym_block_repeat1, 2), SHIFT_REPEAT(14), + [41] = {.entry = {.count = 2, .reusable = false}}, REDUCE(aux_sym_block_repeat1, 2), SHIFT_REPEAT(6), + [44] = {.entry = {.count = 1, .reusable = false}}, REDUCE(aux_sym_block_repeat1, 2), + [46] = {.entry = {.count = 1, .reusable = false}}, SHIFT(37), + [48] = {.entry = {.count = 1, .reusable = true}}, SHIFT(21), + [50] = {.entry = {.count = 1, .reusable = true}}, SHIFT(37), + [52] = {.entry = {.count = 1, .reusable = false}}, SHIFT(35), + [54] = {.entry = {.count = 1, .reusable = true}}, SHIFT(35), + [56] = {.entry = {.count = 1, .reusable = false}}, SHIFT(29), + [58] = {.entry = {.count = 1, .reusable = true}}, SHIFT(29), + [60] = {.entry = {.count = 1, .reusable = false}}, SHIFT(33), + [62] = {.entry = {.count = 1, .reusable = true}}, SHIFT(33), + [64] = {.entry = {.count = 1, .reusable = false}}, SHIFT(26), + [66] = {.entry = {.count = 1, .reusable = true}}, SHIFT(26), + [68] = {.entry = {.count = 1, .reusable = false}}, SHIFT(40), + [70] = {.entry = {.count = 1, .reusable = true}}, SHIFT(40), + [72] = {.entry = {.count = 1, .reusable = false}}, SHIFT(31), + [74] = {.entry = {.count = 1, .reusable = true}}, SHIFT(31), + [76] = {.entry = {.count = 1, .reusable = false}}, SHIFT(30), + [78] = {.entry = {.count = 1, .reusable = true}}, SHIFT(30), + [80] = {.entry = {.count = 1, .reusable = false}}, SHIFT(32), + [82] = {.entry = {.count = 1, .reusable = true}}, SHIFT(32), + [84] = {.entry = {.count = 1, .reusable = false}}, SHIFT(28), + [86] = {.entry = {.count = 1, .reusable = true}}, SHIFT(28), + [88] = {.entry = {.count = 1, .reusable = true}}, REDUCE(sym_block, 3), + [90] = {.entry = {.count = 1, .reusable = true}}, REDUCE(sym_block, 4), + [92] = {.entry = {.count = 1, .reusable = true}}, REDUCE(sym_block, 2), + [94] = {.entry = {.count = 1, .reusable = false}}, REDUCE(sym_assign_statement, 4), + [96] = {.entry = {.count = 1, .reusable = true}}, REDUCE(sym_assign_statement, 4), + [98] = {.entry = {.count = 1, .reusable = false}}, REDUCE(sym_if_statement, 4, .production_id = 3), + [100] = {.entry = {.count = 1, .reusable = true}}, REDUCE(sym_if_statement, 4, .production_id = 3), + [102] = {.entry = {.count = 1, .reusable = false}}, REDUCE(sym_return_statement, 2), + [104] = {.entry = {.count = 1, .reusable = true}}, REDUCE(sym_return_statement, 2), + [106] = {.entry = {.count = 1, .reusable = false}}, REDUCE(sym_return_statement, 3), + [108] = {.entry = {.count = 1, .reusable = true}}, REDUCE(sym_return_statement, 3), + [110] = {.entry = {.count = 1, .reusable = true}}, SHIFT(49), + [112] = {.entry = {.count = 1, .reusable = true}}, REDUCE(sym_if_expression, 5, .production_id = 4), + [114] = {.entry = {.count = 1, .reusable = true}}, REDUCE(sym_boolean_literal, 1), + [116] = {.entry = {.count = 1, .reusable = true}}, REDUCE(sym_unary_expression, 2), + [118] = {.entry = {.count = 1, .reusable = true}}, SHIFT(12), + [120] = {.entry = {.count = 1, .reusable = true}}, SHIFT(13), + [122] = {.entry = {.count = 1, .reusable = true}}, SHIFT(8), + [124] = {.entry = {.count = 1, .reusable = true}}, SHIFT(15), + [126] = {.entry = {.count = 1, .reusable = true}}, REDUCE(sym_binary_expression, 3), + [128] = {.entry = {.count = 1, .reusable = true}}, REDUCE(sym__expression, 1), + [130] = {.entry = {.count = 1, .reusable = true}}, SHIFT(11), + [132] = {.entry = {.count = 1, .reusable = true}}, SHIFT(22), + [134] = {.entry = {.count = 1, .reusable = true}}, REDUCE(aux_sym_source_file_repeat1, 2), + [136] = {.entry = {.count = 2, .reusable = true}}, REDUCE(aux_sym_source_file_repeat1, 2), SHIFT_REPEAT(62), + [139] = {.entry = {.count = 1, .reusable = true}}, REDUCE(sym_source_file, 1), + [141] = {.entry = {.count = 1, .reusable = true}}, SHIFT(19), + [143] = {.entry = {.count = 1, .reusable = true}}, SHIFT(17), + [145] = {.entry = {.count = 1, .reusable = true}}, REDUCE(sym_parameters, 4), + [147] = {.entry = {.count = 1, .reusable = true}}, REDUCE(sym_parameters, 3), + [149] = {.entry = {.count = 1, .reusable = true}}, REDUCE(sym_parameters, 5), + [151] = {.entry = {.count = 1, .reusable = true}}, REDUCE(sym_parameters, 2), + [153] = {.entry = {.count = 1, .reusable = true}}, SHIFT(66), + [155] = {.entry = {.count = 1, .reusable = true}}, SHIFT(46), + [157] = {.entry = {.count = 2, .reusable = true}}, REDUCE(aux_sym_parameters_repeat1, 2), SHIFT_REPEAT(66), + [160] = {.entry = {.count = 1, .reusable = true}}, REDUCE(sym_primitive_type, 1), + [162] = {.entry = {.count = 1, .reusable = true}}, SHIFT(20), + [164] = {.entry = {.count = 1, .reusable = true}}, SHIFT(4), + [166] = {.entry = {.count = 1, .reusable = true}}, REDUCE(aux_sym_parameters_repeat1, 2), + [168] = {.entry = {.count = 1, .reusable = true}}, SHIFT(45), + [170] = {.entry = {.count = 1, .reusable = true}}, REDUCE(sym_parameter, 3), + [172] = {.entry = {.count = 1, .reusable = true}}, REDUCE(sym_function_definition, 5, .production_id = 2), + [174] = {.entry = {.count = 1, .reusable = true}}, SHIFT(53), + [176] = {.entry = {.count = 1, .reusable = true}}, SHIFT(43), + [178] = {.entry = {.count = 1, .reusable = true}}, REDUCE(sym_function_definition, 4, .production_id = 1), + [180] = {.entry = {.count = 1, .reusable = true}}, SHIFT(57), + [182] = {.entry = {.count = 1, .reusable = true}}, SHIFT(44), + [184] = {.entry = {.count = 1, .reusable = true}}, SHIFT(47), + [186] = {.entry = {.count = 1, .reusable = true}}, SHIFT(60), + [188] = {.entry = {.count = 1, .reusable = true}}, SHIFT(61), + [190] = {.entry = {.count = 1, .reusable = true}}, SHIFT(41), + [192] = {.entry = {.count = 1, .reusable = true}}, ACCEPT_INPUT(), +}; + +#ifdef __cplusplus +extern "C" { +#endif +#ifdef _WIN32 +#define extern __declspec(dllexport) +#endif + +extern const TSLanguage *tree_sitter_lila(void) { + static const TSLanguage language = { + .version = LANGUAGE_VERSION, + .symbol_count = SYMBOL_COUNT, + .alias_count = ALIAS_COUNT, + .token_count = TOKEN_COUNT, + .external_token_count = EXTERNAL_TOKEN_COUNT, + .state_count = STATE_COUNT, + .large_state_count = LARGE_STATE_COUNT, + .production_id_count = PRODUCTION_ID_COUNT, + .field_count = FIELD_COUNT, + .max_alias_sequence_length = MAX_ALIAS_SEQUENCE_LENGTH, + .parse_table = &ts_parse_table[0][0], + .small_parse_table = ts_small_parse_table, + .small_parse_table_map = ts_small_parse_table_map, + .parse_actions = ts_parse_actions, + .symbol_names = ts_symbol_names, + .field_names = ts_field_names, + .field_map_slices = ts_field_map_slices, + .field_map_entries = ts_field_map_entries, + .symbol_metadata = ts_symbol_metadata, + .public_symbol_map = ts_symbol_map, + .alias_map = ts_non_terminal_alias_map, + .alias_sequences = &ts_alias_sequences[0][0], + .lex_modes = ts_lex_modes, + .lex_fn = ts_lex, + .keyword_lex_fn = ts_lex_keywords, + .keyword_capture_token = sym_identifier, + .primary_state_ids = ts_primary_state_ids, + }; + return &language; +} +#ifdef __cplusplus +} +#endif diff --git a/src/tree_sitter/parser.h b/src/tree_sitter/parser.h new file mode 100644 index 0000000..2b14ac1 --- /dev/null +++ b/src/tree_sitter/parser.h @@ -0,0 +1,224 @@ +#ifndef TREE_SITTER_PARSER_H_ +#define TREE_SITTER_PARSER_H_ + +#ifdef __cplusplus +extern "C" { +#endif + +#include +#include +#include + +#define ts_builtin_sym_error ((TSSymbol)-1) +#define ts_builtin_sym_end 0 +#define TREE_SITTER_SERIALIZATION_BUFFER_SIZE 1024 + +typedef uint16_t TSStateId; + +#ifndef TREE_SITTER_API_H_ +typedef uint16_t TSSymbol; +typedef uint16_t TSFieldId; +typedef struct TSLanguage TSLanguage; +#endif + +typedef struct { + TSFieldId field_id; + uint8_t child_index; + bool inherited; +} TSFieldMapEntry; + +typedef struct { + uint16_t index; + uint16_t length; +} TSFieldMapSlice; + +typedef struct { + bool visible; + bool named; + bool supertype; +} TSSymbolMetadata; + +typedef struct TSLexer TSLexer; + +struct TSLexer { + int32_t lookahead; + TSSymbol result_symbol; + void (*advance)(TSLexer *, bool); + void (*mark_end)(TSLexer *); + uint32_t (*get_column)(TSLexer *); + bool (*is_at_included_range_start)(const TSLexer *); + bool (*eof)(const TSLexer *); +}; + +typedef enum { + TSParseActionTypeShift, + TSParseActionTypeReduce, + TSParseActionTypeAccept, + TSParseActionTypeRecover, +} TSParseActionType; + +typedef union { + struct { + uint8_t type; + TSStateId state; + bool extra; + bool repetition; + } shift; + struct { + uint8_t type; + uint8_t child_count; + TSSymbol symbol; + int16_t dynamic_precedence; + uint16_t production_id; + } reduce; + uint8_t type; +} TSParseAction; + +typedef struct { + uint16_t lex_state; + uint16_t external_lex_state; +} TSLexMode; + +typedef union { + TSParseAction action; + struct { + uint8_t count; + bool reusable; + } entry; +} TSParseActionEntry; + +struct TSLanguage { + uint32_t version; + uint32_t symbol_count; + uint32_t alias_count; + uint32_t token_count; + uint32_t external_token_count; + uint32_t state_count; + uint32_t large_state_count; + uint32_t production_id_count; + uint32_t field_count; + uint16_t max_alias_sequence_length; + const uint16_t *parse_table; + const uint16_t *small_parse_table; + const uint32_t *small_parse_table_map; + const TSParseActionEntry *parse_actions; + const char * const *symbol_names; + const char * const *field_names; + const TSFieldMapSlice *field_map_slices; + const TSFieldMapEntry *field_map_entries; + const TSSymbolMetadata *symbol_metadata; + const TSSymbol *public_symbol_map; + const uint16_t *alias_map; + const TSSymbol *alias_sequences; + const TSLexMode *lex_modes; + bool (*lex_fn)(TSLexer *, TSStateId); + bool (*keyword_lex_fn)(TSLexer *, TSStateId); + TSSymbol keyword_capture_token; + struct { + const bool *states; + const TSSymbol *symbol_map; + void *(*create)(void); + void (*destroy)(void *); + bool (*scan)(void *, TSLexer *, const bool *symbol_whitelist); + unsigned (*serialize)(void *, char *); + void (*deserialize)(void *, const char *, unsigned); + } external_scanner; + const TSStateId *primary_state_ids; +}; + +/* + * Lexer Macros + */ + +#define START_LEXER() \ + bool result = false; \ + bool skip = false; \ + bool eof = false; \ + int32_t lookahead; \ + goto start; \ + next_state: \ + lexer->advance(lexer, skip); \ + start: \ + skip = false; \ + lookahead = lexer->lookahead; + +#define ADVANCE(state_value) \ + { \ + state = state_value; \ + goto next_state; \ + } + +#define SKIP(state_value) \ + { \ + skip = true; \ + state = state_value; \ + goto next_state; \ + } + +#define ACCEPT_TOKEN(symbol_value) \ + result = true; \ + lexer->result_symbol = symbol_value; \ + lexer->mark_end(lexer); + +#define END_STATE() return result; + +/* + * Parse Table Macros + */ + +#define SMALL_STATE(id) id - LARGE_STATE_COUNT + +#define STATE(id) id + +#define ACTIONS(id) id + +#define SHIFT(state_value) \ + {{ \ + .shift = { \ + .type = TSParseActionTypeShift, \ + .state = state_value \ + } \ + }} + +#define SHIFT_REPEAT(state_value) \ + {{ \ + .shift = { \ + .type = TSParseActionTypeShift, \ + .state = state_value, \ + .repetition = true \ + } \ + }} + +#define SHIFT_EXTRA() \ + {{ \ + .shift = { \ + .type = TSParseActionTypeShift, \ + .extra = true \ + } \ + }} + +#define REDUCE(symbol_val, child_count_val, ...) \ + {{ \ + .reduce = { \ + .type = TSParseActionTypeReduce, \ + .symbol = symbol_val, \ + .child_count = child_count_val, \ + __VA_ARGS__ \ + }, \ + }} + +#define RECOVER() \ + {{ \ + .type = TSParseActionTypeRecover \ + }} + +#define ACCEPT_INPUT() \ + {{ \ + .type = TSParseActionTypeAccept \ + }} + +#ifdef __cplusplus +} +#endif + +#endif // TREE_SITTER_PARSER_H_ diff --git a/test/corpus/booleans.txt b/test/corpus/booleans.txt new file mode 100644 index 0000000..b209b46 --- /dev/null +++ b/test/corpus/booleans.txt @@ -0,0 +1,75 @@ +=============================================================================== +Booleans +=============================================================================== + +func boolean() bool { + right = true; + wrong = false; + right_or_wrong = right || wrong; + right_and_wrong = right && wrong; + return true; +} + +------------------------------------------------------------------------------- + +(source_file + (function_definition + (identifier) + (parameters) + (primitive_type) + (block + (assign_statement + (identifier) + (boolean_literal)) + (assign_statement + (identifier) + (boolean_literal)) + (assign_statement + (identifier) + (binary_expression + (identifier) + (identifier))) + (assign_statement + (identifier) + (binary_expression + (identifier) + (identifier))) + (return_statement (boolean_literal))))) + +=============== +literal 'true' +=============== + +func a() bool { + return true; +} + +--- + +(source_file + (function_definition + (identifier) + (parameters) + (primitive_type) + (block + (return_statement + (boolean_literal))))) + +=============================================================================== +literal 'false' +=============================================================================== + +func a() bool { + return false; +} + +------------------------------------------------------------------------------- + +(source_file + (function_definition + (identifier) + (parameters) + (primitive_type) + (block + (return_statement + (boolean_literal))))) diff --git a/test/corpus/function_parameters.txt b/test/corpus/function_parameters.txt new file mode 100644 index 0000000..5633ded --- /dev/null +++ b/test/corpus/function_parameters.txt @@ -0,0 +1,114 @@ +=============================================================================== +No parameters +=============================================================================== + +func one() int {} + +------------------------------------------------------------------------------- + +(source_file + (function_definition + name: (identifier) + parameters: (parameters) + return_type: (primitive_type) + body: (block))) + +=============================================================================== +1 parameter +=============================================================================== + +func one(x int) int {} + +------------------------------------------------------------------------------- + +(source_file + (function_definition + name: (identifier) + parameters: (parameters + (parameter + (identifier) + (primitive_type))) + return_type: (primitive_type) + body: (block))) + +=============================================================================== +1 parameter with trailing comma +=============================================================================== + +func one( + x int, +) int {} + +------------------------------------------------------------------------------- + +(source_file + (function_definition + name: (identifier) + parameters: (parameters + (parameter + (identifier) + (primitive_type))) + return_type: (primitive_type) + body: (block))) + +=============================================================================== +2 parameters +=============================================================================== + +func one(x int, y int) int {} + +------------------------------------------------------------------------------- + +(source_file + (function_definition + name: (identifier) + parameters: (parameters + (parameter + (identifier) + (primitive_type)) + (parameter + (identifier) + (primitive_type))) + return_type: (primitive_type) + body: (block))) + +=============================================================================== +2 parameters with trailing comma +=============================================================================== + +func one( + x int, + y int, +) int {} + +------------------------------------------------------------------------------- + +(source_file + (function_definition + name: (identifier) + parameters: (parameters + (parameter + (identifier) + (primitive_type)) + (parameter + (identifier) + (primitive_type))) + return_type: (primitive_type) + body: (block))) + +=============================================================================== +[Error] No space between parameter name and type +=============================================================================== + +func one(xint) int {} + +------------------------------------------------------------------------------- + +(source_file + (function_definition + name: (identifier) + parameters: (parameters + (ERROR + (identifier))) + return_type: (primitive_type) + body: (block))) diff --git a/test/corpus/if.txt b/test/corpus/if.txt new file mode 100644 index 0000000..115cd5c --- /dev/null +++ b/test/corpus/if.txt @@ -0,0 +1,95 @@ +==================================================== +if statement +==================================================== + +func main() { + if true { + false + }; +} + +---------------------------------------------------- + +(source_file + (function_definition + name: (identifier) + parameters: (parameters) + body: (block + (if_statement + condition: (boolean_literal) + body: (block + (boolean_literal)))))) + +==================================================== +if else expression +==================================================== + +func main() { + return if true { + false + } else { + true + }; +} + +---------------------------------------------------- + +(source_file + (function_definition + name: (identifier) + parameters: (parameters) + body: (block + (return_statement + (if_expression + condition: (boolean_literal) + body: (block + (boolean_literal)) + else: (block + (boolean_literal))))))) + +==================================================== +if else expression (no brackets in else) +==================================================== + +func main() { + return if true { + false + } else true; +} + +---------------------------------------------------- + +(source_file + (function_definition + name: (identifier) + parameters: (parameters) + body: (block + (return_statement + (if_expression + condition: (boolean_literal) + body: (block + (boolean_literal)) + else: (boolean_literal)))))) + +==================================================== +[Error] if expression (missing else) +==================================================== + +func main() { + return if true { + false + }; +} + +---------------------------------------------------- + +(source_file + (function_definition + name: (identifier) + parameters: (parameters) + body: (block + (return_statement + (ERROR + (boolean_literal) + (block + (boolean_literal))))))) diff --git a/test/corpus/return.txt b/test/corpus/return.txt new file mode 100644 index 0000000..7f7896b --- /dev/null +++ b/test/corpus/return.txt @@ -0,0 +1,56 @@ +=============================================================================== +Return int literal +=============================================================================== + +func x() int { + return 1; +} + +------------------------------------------------------------------------------- + +(source_file + (function_definition + name: (identifier) + parameters: (parameters) + return_type: (primitive_type) + body: + (block + (return_statement (integer_literal))))) + +=============================================================================== +Return nothing +=============================================================================== + +func x() int { + return; +} + +------------------------------------------------------------------------------- + +(source_file + (function_definition + name: (identifier) + parameters: (parameters) + return_type: (primitive_type) + body: + (block + (return_statement)))) + +=============================================================================== +[Error] Return nothing with missing ';' +=============================================================================== + +func x() int { + return +} + +------------------------------------------------------------------------------- + +(source_file + (function_definition + name: (identifier) + parameters: (parameters) + return_type: (primitive_type) + body: + (block + (ERROR)))) diff --git a/test/corpus/return_types.txt b/test/corpus/return_types.txt new file mode 100644 index 0000000..ea58098 --- /dev/null +++ b/test/corpus/return_types.txt @@ -0,0 +1,30 @@ +=============================================================================== +int return type +=============================================================================== + +func a() int { +} + +------------------------------------------------------------------------------- + +(source_file + (function_definition + name: (identifier) + parameters: (parameters) + return_type: (primitive_type) + body: (block))) + +=============================================================================== +No return type +=============================================================================== + +func a() { +} + +------------------------------------------------------------------------------- + +(source_file + (function_definition + name: (identifier) + parameters: (parameters) + body: (block))) diff --git a/ts_verify_ts.conf b/ts_verify_ts.conf new file mode 100644 index 0000000..75f0992 --- /dev/null +++ b/ts_verify_ts.conf @@ -0,0 +1 @@ +module_raw input