Skip to content

Commit

Permalink
Completely overhaul the grammar
Browse files Browse the repository at this point in the history
  • Loading branch information
Mango0x45 committed Sep 14, 2024
1 parent 291cb06 commit 642d526
Show file tree
Hide file tree
Showing 6 changed files with 1,037 additions and 1,486 deletions.
1 change: 1 addition & 0 deletions .exrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
let g:editorconfig = v:false
38 changes: 38 additions & 0 deletions example
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
html lang="en" {
head {
meta charset="UTF-8" {}
meta
name="viewport"
content="width=device-width, initial-scale=1.0"
{}
link href="/favicon.svg" rel="shortcut icon" type="image/svg" {}
link href="/style.svg" rel="stylesheet" {}
title {-My Website Title}
}

body {
p #my-id  {= This is a paragraph with the id ‘my-id’ }
p .my-cls {= This is a paragraph with the class ‘my-cls’ }

/ div {
p {- This entire div is commented out. }
p {- Isn’t that neat? }
}

p
#some-id
.class-1
.class-2
key-1="value-1"
key-2 = "value-2"
{-
This paragraph has an ID, two classes, and two additional
attributes. GSP allows us to use the ‘#ident’ and ‘.ident’
syntaxes as shorthands for applying IDs, and classes. This
is a text node, so nothing is being interpreted as GSP nodes,
but we can include them inline if we want. As an example,
here is some @em {-emphatic} text. Your inline nodes can
also have attributes @em #id {-just like a regular node}.
}
}
}
70 changes: 24 additions & 46 deletions grammar.js
Original file line number Diff line number Diff line change
@@ -1,60 +1,38 @@
const IDENT = /\p{XID_Start}[-\p{XID_Continue}]*/u

module.exports = grammar({
name: 'gsp',
extras: $ => [$._S],
extras: $ => [/\p{Pattern_White_Space}+/u],
rules: {
source_file: $ => repeat(choice($.comment, $.node)),
document: $ => repeat($._toplevel),
_toplevel: $ => choice($.comment, $.node),

comment: $ => seq(
'/',
optional($.node_name),
optional($.attribute_list),
'{',
optional($.node_body),
'}',
),
comment: $ => seq('/', $.node),
node: $ => seq($.node_name, optional($.attrs), $.node_body),

node: $ => seq(
optional('>'),
$.node_name,
optional($.attribute_list),
node_name: $ => IDENT,
node_body: $ => seq(
'{',
optional($.node_body),
'}',
),

node_body: $ => choice(
repeat1(choice($.comment, $.node)),
seq(choice('-', '='), optional($.text)),
),

node_name: $ => /[a-zA-Z:_\u{000C0}-\u{000D6}\u{000D8}-\u{000F6}\u{000F8}-\u{002FF}\u{00370}-\u{0037D}\u{0037F}-\u{01FFF}\u{0200C}-\u{0200D}\u{02070}-\u{0218F}\u{02C00}-\u{02FEF}\u{03001}-\u{0D7FF}\u{0F900}-\u{0FDCF}\u{0FDF0}-\u{0FFFD}\u{10000}-\u{EFFFF}][a-zA-Z0-9:_\-.·\u{00300}-\u{0036F}\u{0203F}-\u{02040}\u{000C0}-\u{000D6}\u{000D8}-\u{000F6}\u{000F8}-\u{002FF}\u{00370}-\u{0037D}\u{0037F}-\u{01FFF}\u{0200C}-\u{0200D}\u{02070}-\u{0218F}\u{02C00}-\u{02FEF}\u{03001}-\u{0D7FF}\u{0F900}-\u{0FDCF}\u{0FDF0}-\u{0FFFD}\u{10000}-\u{EFFFF}]*/u,

text: $ => repeat1(
choice(
$.literal_text,
seq('@', choice($.comment, $.node)),
),
),

literal_text: $ => /(\\[@}\\]|[^@}\\])+/,

attribute_list: $ => repeat1($.attribute),

attribute: $ => choice(
$.class_shorthand,
$.id_shorthand,
seq(
$.attribute_name,
optional(seq('=', $.attribute_value)),
repeat($._toplevel),
seq(choice('-', '='), optional($.text)),
),
'}',
),

class_shorthand: $ => /\.\P{White_Space}+/u,
id_shorthand: $ => /#\P{White_Space}+/u,
attrs: $ => repeat1(choice(
$.attr,
$.id_attr,
$.class_attr,
)),

attribute_name: $ => /[a-zA-Z0-9_-]+/,
attribute_value: $ => /"(\\.|[^"\\])*"/,
attr: $ => seq(IDENT, '=', /"(\\["\\]|[^"\\])+"/),
id_attr: $ => seq('#', token.immediate(IDENT)),
class_attr: $ => seq('.', token.immediate(IDENT)),

_S: $ => /\p{White_Space}+/u,
text: $ => repeat1(choice(
/(\\[@}\\]|[^@}\\])+/,
seq('@', $._toplevel),
)),
},
})
Loading

0 comments on commit 642d526

Please sign in to comment.