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

Html support #78

Draft
wants to merge 4 commits into
base: main
Choose a base branch
from
Draft
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
10 changes: 9 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ members = [
"crates/auth",
"crates/externals",
"crates/wasm-bindings",
"crates/marzano_messenger", "crates/cli_bin",
"crates/marzano_messenger", "crates/cli_bin", "crates/test_html_grammar",
]
exclude = [
"resources",
Expand Down
41 changes: 41 additions & 0 deletions crates/core/src/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12816,6 +12816,47 @@ fn css_property_value() {
.unwrap();
}

#[test]
fn html_match_element() {
run_test_match(TestArg {
pattern: r#"
|language html
|
|contains `<Hello attr="foo"></$name>`
|"#
.trim_margin()
.unwrap(),
source: r#"<Hello attr="foo"></Hello>"#
.to_owned(),
})
.unwrap();
}

#[test]
fn html_prefer_self_closing_tags() {
run_test_expected(TestArgExpected {
pattern: r#"
|language html
|
|`<$name $props>$body</$name>` => `<$name $props />` where $body <: .
|"#
.trim_margin()
.unwrap(),
source: r#"
|<Hello attr="foo"></Hello>
|"#
.trim_margin()
.unwrap(),
expected: r#"
|<Hello attr="foo" />
|"#
.trim_margin()
.unwrap(),
})
.unwrap();
}


#[test]
fn json_empty_string_should_not_match_everything() {
run_test_no_match(TestArg {
Expand Down
37 changes: 37 additions & 0 deletions crates/language/src/html.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,3 +61,40 @@ impl Language for Html {
self.metavariable_sort
}
}




#[cfg(test)]
mod tests {
use marzano_util::print_node::print_node;

use crate::language::nodes_from_indices;

use super::*;

#[test]
fn import_variable() {
let snippet = r#"<Hello attr="foo"></Hello>"#;
let lang = Html::new(None);
let mut parser = tree_sitter::Parser::new().unwrap();
parser.set_language(lang.get_ts_language()).unwrap();
let snippets = lang.parse_snippet_contexts(snippet);
let nodes = nodes_from_indices(&snippets);
assert!(!nodes.is_empty());
print_node(&nodes[0].node);
}

#[test]
fn print_sexp() {
let code = r#"<Hello attr="foo"></Hello>"#;
let mut parser = tree_sitter::Parser::new().unwrap();
let lang = tree_sitter_html::language().into();
// let lang = Html::new(None);
parser.set_language(&lang).unwrap();
let tree = parser.parse(code, None).unwrap().unwrap();
let root = tree.root_node();
let sexp = root.to_sexp();
println!("{sexp}");
}
}
11 changes: 11 additions & 0 deletions crates/test_html_grammar/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
[package]
name = "test_html_grammar"
version = "0.1.0"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
tree-sitter = { path = "../../vendor/tree-sitter-facade", package = "tree-sitter-facade-sg" }
# tree-sitter-html = "0.20.0"
tree-sitter-html = { path = "../../resources/language-metavariables/tree-sitter-html" }
11 changes: 11 additions & 0 deletions crates/test_html_grammar/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@


fn main() {
let code = r#"<Hello attr="foo"></Hello>"#;
let mut parser = tree_sitter::Parser::new().unwrap();
parser.set_language(&tree_sitter_html::language().into()).unwrap();
let tree = parser.parse(code, None).unwrap().unwrap();
let root = tree.root_node();
let sexp = root.to_sexp();
println!("{sexp}");
}
Binary file modified crates/wasm-bindings/wasm_parsers/tree-sitter-html.wasm
Binary file not shown.
4 changes: 0 additions & 4 deletions resources/edit_grammars.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -121,10 +121,6 @@ async function rsyncGrammars(language) {
? `${LANGUAGE_METAVARIABLES}/${treeSitterLang}`
: LANGUAGE_METAVARIABLES;

if (languagesWithoutMetaVariables.includes(language)) {
return;
}

await fs.rmdir(mvDir, { recursive: true });
await fs.mkdir(mvDir, { recursive: true });

Expand Down
22 changes: 0 additions & 22 deletions resources/language-metavariables/tree-sitter-html/.appveyor.yml

This file was deleted.

10 changes: 0 additions & 10 deletions resources/language-metavariables/tree-sitter-html/.travis.yml

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[package]
name = "tree-sitter-html"
description = "HTML grammar for tree-sitter"
version = "0.20.0"
version = "0.20.2"
authors = [
"Max Brunsfeld <[email protected]>",
"Amaan Qureshi <[email protected]>",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
VERSION := 0.20.0
VERSION := 0.20.2

# Repository
SRC_DIR := src
Expand Down
14 changes: 12 additions & 2 deletions resources/language-metavariables/tree-sitter-html/README.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,19 @@
# tree-sitter-html

[![build](https://github.com/tree-sitter/tree-sitter-html/actions/workflows/ci.yml/badge.svg)](https://github.com/tree-sitter/tree-sitter-html/actions/workflows/ci.yml)
[![CI][ci]](https://github.com/tree-sitter/tree-sitter-html/actions/workflows/ci.yml)
[![discord][discord]](https://discord.gg/w7nTvsVJhm)
[![matrix][matrix]](https://matrix.to/#/#tree-sitter-chat:matrix.org)
[![crates][crates]](https://crates.io/crates/tree-sitter-html)
[![npm][npm]](https://www.npmjs.com/package/tree-sitter-html)

HTML grammar for [tree-sitter](https://github.com/tree-sitter/tree-sitter).

References

* [The HTML5 Spec](https://www.w3.org/TR/html5/syntax.html)
- [The HTML5 Spec](https://www.w3.org/TR/html5/syntax.html)

[ci]: https://img.shields.io/github/actions/workflow/status/tree-sitter/tree-sitter-html/ci.yml?logo=github&label=CI
[discord]: https://img.shields.io/discord/1063097320771698699?logo=discord&label=discord
[matrix]: https://img.shields.io/matrix/tree-sitter-chat%3Amatrix.org?logo=matrix&label=matrix
[npm]: https://img.shields.io/npm/v/tree-sitter-html?logo=npm
[crates]: https://img.shields.io/crates/v/tree-sitter-html?logo=rust
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ way.)
```toml
[dependencies]
tree-sitter = "0.20.10"
tree-sitter-html = "0.20.0"
tree-sitter-html = "0.20"
```

Typically, you will use the [language][language func] function to add this
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ module.exports = grammar({
],

rules: {
fragment: $ => repeat($._node),
document: $ => repeat($._node),

doctype: $ => seq(
'<!',
Expand Down Expand Up @@ -131,7 +131,7 @@ module.exports = grammar({
// An entity can be named, numeric (decimal), or numeric (hexacecimal). The
// longest entity name is 29 characters long, and the HTML spec says that
// no more will ever be added.
entity: _ => /&(#([xX][0-9a-fA-F]{1,6}|[0-9]{1,5})|[A-Za-z]{1,30});/,
entity: _ => /&(#([xX][0-9a-fA-F]{1,6}|[0-9]{1,5})|[A-Za-z]{1,30});?/,

quoted_attribute_value: $ => choice(
seq('\'', optional(alias(/[^']+/, $.attribute_value)), '\''),
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "tree-sitter-html",
"version": "0.20.0",
"version": "0.20.2",
"description": "HTML grammar for tree-sitter",
"main": "bindings/node",
"keywords": [
Expand Down Expand Up @@ -37,7 +37,7 @@
},
"tree-sitter": [
{
"scope": "text.html.basic",
"scope": "source.html",
"file-types": [
"html"
],
Expand All @@ -47,6 +47,9 @@
],
"injections": [
"queries/injections.scm"
],
"external-files": [
"src/tag.h"
]
}
]
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "html",
"rules": {
"fragment": {
"document": {
"type": "REPEAT",
"content": {
"type": "SYMBOL",
Expand Down Expand Up @@ -373,7 +373,7 @@
},
"entity": {
"type": "PATTERN",
"value": "&(#([xX][0-9a-fA-F]{1,6}|[0-9]{1,5})|[A-Za-z]{1,30});"
"value": "&(#([xX][0-9a-fA-F]{1,6}|[0-9]{1,5})|[A-Za-z]{1,30});?"
},
"quoted_attribute_value": {
"type": "CHOICE",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,45 @@
"named": true,
"fields": {}
},
{
"type": "document",
"named": true,
"fields": {},
"children": {
"multiple": true,
"required": false,
"types": [
{
"type": "doctype",
"named": true
},
{
"type": "element",
"named": true
},
{
"type": "entity",
"named": true
},
{
"type": "erroneous_end_tag",
"named": true
},
{
"type": "script_element",
"named": true
},
{
"type": "style_element",
"named": true
},
{
"type": "text",
"named": true
}
]
}
},
{
"type": "element",
"named": true,
Expand Down Expand Up @@ -108,45 +147,6 @@
]
}
},
{
"type": "fragment",
"named": true,
"fields": {},
"children": {
"multiple": true,
"required": false,
"types": [
{
"type": "doctype",
"named": true
},
{
"type": "element",
"named": true
},
{
"type": "entity",
"named": true
},
{
"type": "erroneous_end_tag",
"named": true
},
{
"type": "script_element",
"named": true
},
{
"type": "style_element",
"named": true
},
{
"type": "text",
"named": true
}
]
}
},
{
"type": "quoted_attribute_value",
"named": true,
Expand Down
Loading
Loading