Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[feat] Implement basic highlighting #14

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,11 @@ node_modules
# Grammar description file
!grammar.js

# Track the queries directory
!queries/**

# Track packaging files
!package*.json
!package.json
!Cargo.toml

# Track the project readme and software license
Expand Down
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
MIT License

Copyright (c) 2023 Adam Henley
Copyright (c) 2023 Adam Henley <[email protected]>

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,8 @@ This is a from-scratch implementation of a tree-sitter parser for the Liquid tem

## Support

If you've found this useful, please star the repository and consider sponsoring me, for any amount, to help support the open source ecosystem.
If you've found this useful, please consider:
🌟 starring the repo
💲 [sponsoring](https://github.com/sponsors/adamazing) me
⚧️ supporting [OutLine](https://outline.org.nz/donate)
✍️ following me on [Medium](https://medium.com/@adamhenley)
68 changes: 56 additions & 12 deletions grammar.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
module.exports = grammar({
name: 'liquid',

word: $ => $.identifier,

rules: {
template: $ => repeat(
choice(
Expand All @@ -21,8 +23,18 @@ module.exports = grammar({
choice("}}", "-}}")
),

comment: $ => token(choice(
seq("{%", /\s#/, /[^%-]+/,),
)),

code: $ => repeat1(
choice(/[^%}-]+|[%}-]/)
choice(
$._expression
)
),

control_code: $ => choice(
""
),

_literal: $ => choice($.string, $.number, $.boolean),
Expand All @@ -33,30 +45,62 @@ module.exports = grammar({
),

number: $ => choice(
/\d+/, // TODO: Check if this is correct
/-?\d*\.?\d+/,
),

boolean: $ => choice('true', 'false'),

_expression: $ => choice(
$.identifier,
$._literal,
$.include_expression,
$.render_expression,
$.selector_expression,
$.binary_expression,
$._literal
//TODO: add more expression types
),

selector_expression: $ => choice(
prec(1, seq(
field('operand', $.identifier),
repeat1(
seq(
'.',
field('field', $.identifier)
)
)
)
)),

identifier: $ => /[a-zA-Z_][a-zA-Z0-9]+/,

binary_operator: $ => choice(
"==",
"!=",
">",
"<",
">=",
"<=",
"or",
"and",
),

// special case binary operator
contains_operator: $ => "contains",

include_expression: $ => seq(
field('include', "include"),
field('included_file',$.string)
),

render_expression: $ => seq(
field('render', "render"),
field('rendered_file', $.string)
),

binary_expression: $ => choice(
seq($._expression, "==", $._expression),
seq($._expression, "!=", $._expression),
seq($._expression, ">", $._expression),
seq($._expression, "<", $._expression),
seq($._expression, ">=", $._expression),
seq($._expression, "<=", $._expression),
seq($._expression, "or", $._expression),
seq($._expression, "and", $._expression),
seq($._expression, "contains", $.string)
prec.right(1,seq($._expression, $.binary_operator, $._expression)),
prec.right(1,seq($._expression, $.contains_operator, $.string))
)
}
});
10 changes: 9 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,13 @@
},
"devDependencies": {
"tree-sitter-cli": "^0.20.8"
}
},
"tree-sitter": [
{
"scope": "text.html.liquid",
"file-types": [
"liquid"
]
}
]
}
6 changes: 6 additions & 0 deletions queries/highlights.scm
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
(output_directive) @keyword
(directive) @keyword
(code) @comment.line
(string) @string
(number) @constant.numeric
(boolean) @constant.language
Loading