-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgrammar.pegjs
32 lines (23 loc) · 892 Bytes
/
grammar.pegjs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
// Simple BBCode Parser in PEG
// ==========================
//
// Produces SAX-style events based on what is parsed.
// Can be reviewed and tested at https://pegjs.org/online
Expression
= (EndTag / StartTag / String)*
TagChars
= !("]" / " ") name:(. TagChars*) { return name.join("") }
TagCharsWithoutEquals
= !("]" / " " / "=") name:(. TagCharsWithoutEquals*) { return name.join("") }
Tag
= name:(TagChars) { return {"name": name, value: null } }
TagWithValue
= name:(TagCharsWithoutEquals) "=" value:(TagChars) { return { name: name, value: value } }
StartTag
= "[" tagName:(TagWithValue / Tag) "]" {return {type: "startTag", tag: tagName} }
EndTag
= "[/" tagName:(TagWithValue / Tag) "]" {return {type: "endTag", tag: tagName} }
NotTag
= !(EndTag / StartTag) s:(. NotTag*) { return s.join("") }
String
= s:NotTag { return {type: "text", text: s} }