Skip to content

Commit 0e4f50b

Browse files
committed
parser: added support for comments, set up tree-sitter boilerplate
1 parent fa4d4f8 commit 0e4f50b

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

44 files changed

+2254
-11
lines changed

Cargo.lock

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,7 @@ members = [
55
"fen_parser",
66
"fen_lsp"
77
]
8+
exclude = [
9+
"tree-sitter-fen",
10+
"documentation"
11+
]

fen_cli/Cargo.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "fen_cli"
3-
version = "0.2.1"
3+
version = "0.3.0"
44
description = "The command line interface for Fen"
55
edition = "2021"
66
repository = "https://github.com/kiahjh/fen"
@@ -29,4 +29,4 @@ colored = "2.2.0"
2929
serde = "1.0.217"
3030
serde_json = "1.0.134"
3131
toml = "0.8.19"
32-
fen_parser = { path = "../fen_parser", version = "0.2.0" }
32+
fen_parser = { path = "../fen_parser", version = "0.3.0" }

changelog.md renamed to fen_cli/changelog.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
# Changelog
22

3+
## [0.3.0]
4+
- fen: support for comments with `//`
5+
36
## [0.2.0]
47
- all: appended `_fen_/` to all paths
58
- all: groundwork laid for annotation support

fen_parser/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "fen_parser"
3-
version = "0.2.0"
3+
version = "0.3.0"
44
description = "A parser for Fen"
55
edition = "2021"
66
repository = "https://github.com/kiahjh/fen"

fen_parser/src/lexer.rs

Lines changed: 61 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,35 @@ impl Lexer {
4545
self.peek_token = None;
4646
}
4747

48+
fn skip_whitespace(&mut self) {
49+
while self.peek_char().map_or(false, u8::is_ascii_whitespace) {
50+
self.pos += 1;
51+
}
52+
}
53+
54+
fn skip_extras(&mut self) -> Result<(), Error> {
55+
self.skip_whitespace();
56+
57+
// skip over comments
58+
if self.peek_char().map_or(false, |c| c == &b'/') {
59+
self.next_char();
60+
if self.peek_char().map_or(false, |c| c == &b'/') {
61+
while self.peek_char().map_or(false, |c| c != &b'\n') {
62+
self.pos += 1;
63+
}
64+
self.skip_whitespace();
65+
// recurse to skip any other comments:
66+
if self.peek_char().map_or(false, |c| c == &b'/') {
67+
return self.skip_extras();
68+
}
69+
} else {
70+
return Err(Error::new("Expected '/'", self.pos - 1));
71+
}
72+
}
73+
74+
Ok(())
75+
}
76+
4877
pub(crate) fn peek_tok(&mut self) -> Result<Option<&Token>, Error> {
4978
if self.peek_token.is_none() {
5079
self.peek_token = self.next_tok()?;
@@ -57,10 +86,7 @@ impl Lexer {
5786
return Ok(Some(peeked));
5887
}
5988

60-
// skip over whitespace
61-
while self.peek_char().map_or(false, u8::is_ascii_whitespace) {
62-
self.pos += 1;
63-
}
89+
self.skip_extras()?;
6490

6591
let mut return_val: Result<Option<Token>, Error> = Ok(None);
6692

@@ -281,32 +307,57 @@ mod tests {
281307
assert_eq!(lexer.next_tok(), Ok(None));
282308
}
283309

310+
#[test]
311+
fn comments() {
312+
expect_tokens(
313+
r#"
314+
// this is a comment
315+
foo
316+
// another comment
317+
"bar"
318+
"#,
319+
&[
320+
TokenKind::Identifier("foo".to_string()),
321+
TokenKind::StringLiteral("bar".to_string()),
322+
],
323+
);
324+
}
325+
284326
#[test]
285327
fn all_syntax() {
286328
expect_tokens(
287329
r#"
330+
// comment
288331
name: "GetTodos"
289332
description: "Get all todos for a user"
290-
authed: true
333+
authed: true // here's another comment
291334
292335
---
293336
337+
// this is the input (nice no?)
294338
@input { user_id: String }
295339
296340
@output [Todo]
297341
298342
---
299343
344+
@someAnnotation
300345
Todo {
301346
name: String
302347
due: Date?
348+
// refers to custom enum
303349
priority: Priority?
304350
subtasks: [Todo]
305351
}
306352
353+
// we add this because this is actually
354+
// a postgres enum
355+
// these comments can be many lines long btw
356+
// just double checking that it works
357+
@sqlxType
307358
Priority (
308359
low
309-
medium
360+
medium // for the bourgeois case
310361
high
311362
other(String)
312363
)
@@ -335,6 +386,8 @@ mod tests {
335386
TokenKind::Identifier("Todo".to_string()),
336387
TokenKind::RightBracket,
337388
TokenKind::Rule,
389+
TokenKind::At,
390+
TokenKind::Identifier("someAnnotation".to_string()),
338391
TokenKind::Identifier("Todo".to_string()),
339392
TokenKind::LeftBrace,
340393
TokenKind::Identifier("name".to_string()),
@@ -354,6 +407,8 @@ mod tests {
354407
TokenKind::Identifier("Todo".to_string()),
355408
TokenKind::RightBracket,
356409
TokenKind::RightBrace,
410+
TokenKind::At,
411+
TokenKind::Identifier("sqlxType".to_string()),
357412
TokenKind::Identifier("Priority".to_string()),
358413
TokenKind::LeftParen,
359414
TokenKind::Identifier("low".to_string()),

tree-sitter-fen/.editorconfig

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
root = true
2+
3+
[*]
4+
charset = utf-8
5+
6+
[*.{json,toml,yml,gyp}]
7+
indent_style = space
8+
indent_size = 2
9+
10+
[*.js]
11+
indent_style = space
12+
indent_size = 2
13+
14+
[*.scm]
15+
indent_style = space
16+
indent_size = 2
17+
18+
[*.{c,cc,h}]
19+
indent_style = space
20+
indent_size = 4
21+
22+
[*.rs]
23+
indent_style = space
24+
indent_size = 4
25+
26+
[*.{py,pyi}]
27+
indent_style = space
28+
indent_size = 4
29+
30+
[*.swift]
31+
indent_style = space
32+
indent_size = 4
33+
34+
[*.go]
35+
indent_style = tab
36+
indent_size = 8
37+
38+
[Makefile]
39+
indent_style = tab
40+
indent_size = 8
41+
42+
[parser.c]
43+
indent_size = 2
44+
45+
[{alloc,array,parser}.h]
46+
indent_size = 2

tree-sitter-fen/.gitattributes

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
* text=auto eol=lf
2+
3+
# Generated source files
4+
src/*.json linguist-generated
5+
src/parser.c linguist-generated
6+
src/tree_sitter/* linguist-generated
7+
8+
# C bindings
9+
bindings/c/** linguist-generated
10+
CMakeLists.txt linguist-generated
11+
Makefile linguist-generated
12+
13+
# Rust bindings
14+
bindings/rust/* linguist-generated
15+
Cargo.toml linguist-generated
16+
Cargo.lock linguist-generated
17+
18+
# Node.js bindings
19+
bindings/node/* linguist-generated
20+
binding.gyp linguist-generated
21+
package.json linguist-generated
22+
package-lock.json linguist-generated
23+
24+
# Python bindings
25+
bindings/python/** linguist-generated
26+
setup.py linguist-generated
27+
pyproject.toml linguist-generated
28+
29+
# Go bindings
30+
bindings/go/* linguist-generated
31+
go.mod linguist-generated
32+
go.sum linguist-generated
33+
34+
# Swift bindings
35+
bindings/swift/** linguist-generated
36+
Package.swift linguist-generated
37+
Package.resolved linguist-generated
38+
39+
# Zig bindings
40+
build.zig linguist-generated
41+
build.zig.zon linguist-generated

tree-sitter-fen/.gitignore

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
# Rust artifacts
2+
target/
3+
4+
# Node artifacts
5+
build/
6+
prebuilds/
7+
node_modules/
8+
9+
# Swift artifacts
10+
.build/
11+
12+
# Go artifacts
13+
_obj/
14+
15+
# Python artifacts
16+
.venv/
17+
dist/
18+
*.egg-info
19+
*.whl
20+
21+
# C artifacts
22+
*.a
23+
*.so
24+
*.so.*
25+
*.dylib
26+
*.dll
27+
*.pc
28+
*.exp
29+
*.lib
30+
31+
# Zig artifacts
32+
.zig-cache/
33+
zig-cache/
34+
zig-out/
35+
36+
# Example dirs
37+
/examples/*/
38+
39+
# Grammar volatiles
40+
*.wasm
41+
*.obj
42+
*.o
43+
44+
# Archives
45+
*.tar.gz
46+
*.tgz
47+
*.zip

tree-sitter-fen/CMakeLists.txt

Lines changed: 66 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)