Skip to content

Commit edf87be

Browse files
authored
Add newline, indent, dedent external tokens for raw_text rules (#3)
Attempt to capture inline source code nodes written within script and style tags. Recognizes where source code begins but not where it ends.
1 parent cf56b27 commit edf87be

File tree

6 files changed

+967
-493
lines changed

6 files changed

+967
-493
lines changed

binding.gyp

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
"sources": [
1010
"bindings/node/binding.cc",
1111
"src/parser.c",
12-
# If your language uses an external scanner, add it here.
12+
"src/scanner.cc"
1313
],
1414
"cflags_c": [
1515
"-std=c99",

example-file.pug

+1-1
Original file line numberDiff line numberDiff line change
@@ -39,4 +39,4 @@ html
3939
include includes/footer.pug
4040

4141
script(src='https://code.jquery.com/jquery-3.4.1.slim.min.js')
42-
script(src='https://getbootstrap.com/docs/4.4/dist/js/bootstrap.bundle)
42+
script(src='https://getbootstrap.com/docs/4.4/dist/js/bootstrap.bundle')

grammar.js

+8-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
module.exports = grammar({
22
name: "pug",
33

4+
externals: ($) => [$._newline, $._indent, $._dedent],
5+
46
rules: {
57
source_file: ($) =>
68
repeat(
@@ -20,12 +22,16 @@ module.exports = grammar({
2022

2123
link_tag: ($) => seq("link", optional($._attributes)),
2224

23-
script_tag: ($) => seq("script", optional($._attributes)),
25+
script_tag: ($) =>
26+
seq("script", optional($._attributes), optional(seq(".", $._newline))),
2427

25-
style_tag: ($) => seq("style", optional($._attributes)),
28+
style_tag: ($) =>
29+
seq("style", optional($._attributes), optional(seq(".", $._newline))),
2630

2731
path: ($) => seq(repeat1(choice(/\w/, "/")), ".", repeat1(/\w/)),
2832

33+
raw_text: ($) => choice(seq($._indent, /\w/, $._dedent)),
34+
2935
_attributes: ($) =>
3036
seq("(", repeat(seq($.attribute, optional(choice(",", " ")))), ")"),
3137

src/grammar.json

+78-1
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,27 @@
106106
"type": "BLANK"
107107
}
108108
]
109+
},
110+
{
111+
"type": "CHOICE",
112+
"members": [
113+
{
114+
"type": "SEQ",
115+
"members": [
116+
{
117+
"type": "STRING",
118+
"value": "."
119+
},
120+
{
121+
"type": "SYMBOL",
122+
"name": "_newline"
123+
}
124+
]
125+
},
126+
{
127+
"type": "BLANK"
128+
}
129+
]
109130
}
110131
]
111132
},
@@ -127,6 +148,27 @@
127148
"type": "BLANK"
128149
}
129150
]
151+
},
152+
{
153+
"type": "CHOICE",
154+
"members": [
155+
{
156+
"type": "SEQ",
157+
"members": [
158+
{
159+
"type": "STRING",
160+
"value": "."
161+
},
162+
{
163+
"type": "SYMBOL",
164+
"name": "_newline"
165+
}
166+
]
167+
},
168+
{
169+
"type": "BLANK"
170+
}
171+
]
130172
}
131173
]
132174
},
@@ -162,6 +204,28 @@
162204
}
163205
]
164206
},
207+
"raw_text": {
208+
"type": "CHOICE",
209+
"members": [
210+
{
211+
"type": "SEQ",
212+
"members": [
213+
{
214+
"type": "SYMBOL",
215+
"name": "_indent"
216+
},
217+
{
218+
"type": "PATTERN",
219+
"value": "\\w"
220+
},
221+
{
222+
"type": "SYMBOL",
223+
"name": "_dedent"
224+
}
225+
]
226+
}
227+
]
228+
},
165229
"_attributes": {
166230
"type": "SEQ",
167231
"members": [
@@ -340,7 +404,20 @@
340404
],
341405
"conflicts": [],
342406
"precedences": [],
343-
"externals": [],
407+
"externals": [
408+
{
409+
"type": "SYMBOL",
410+
"name": "_newline"
411+
},
412+
{
413+
"type": "SYMBOL",
414+
"name": "_indent"
415+
},
416+
{
417+
"type": "SYMBOL",
418+
"name": "_dedent"
419+
}
420+
],
344421
"inline": [],
345422
"supertypes": []
346423
}

0 commit comments

Comments
 (0)