Skip to content

Commit

Permalink
Merge pull request #18 from jigfox/add-uci
Browse files Browse the repository at this point in the history
feat: add support for icu message format
  • Loading branch information
dlvandenberg authored Mar 13, 2024
2 parents 52594e2 + 0cabb37 commit f1bbc4b
Show file tree
Hide file tree
Showing 7 changed files with 14,284 additions and 10,274 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ This parser extends [tree-sitter-html](https://github.com/tree-sitter/tree-sitte
- [x] For-statements (v17)
- [x] Switch-statements (v17)
- [x] Defer-statements (v17)
- [x] ICU message format

## Filetype

Expand Down
152 changes: 152 additions & 0 deletions corpus/icu.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,152 @@
=====
Basic
=====

Updated {minutes, plural,
=0 {just now}
=1 {one minute ago}
other {some minutes ago}}

---

(fragment
(text)
(icu
(expression
(identifier))
(icu_clause)
(icu_case
(icu_category)
(text))
(icu_case
(icu_category)
(text))
(icu_case
(icu_category)
(text))))


=====
With interpolation
=====

Updated {minutes, plural, =0 {just now} =1 {one minute ago} other {{{minutes}} minutes ago}}

---

(fragment
(text)
(icu
(expression
(identifier))
(icu_clause)
(icu_case
(icu_category)
(text))
(icu_case
(icu_category)
(text))
(icu_case
(icu_category)
(interpolation
(expression
(identifier)))
(text))))

======
Select
======

The author is {gender, select, male {male} female {female} other {other}}

---

(fragment
(text)
(icu
(expression
(identifier))
(icu_clause)
(icu_case
(icu_category)
(text))
(icu_case
(icu_category)
(text))
(icu_case
(icu_category)
(text))))

=====
Mixed
=====

Updated: {minutes, plural,
=0 {just now}
=1 {one minute ago}
other {{{minutes}} minutes ago by {gender, select, male {male} female {female} other {other}}}}

---

(fragment
(text)
(icu
(expression
(identifier))
(icu_clause)
(icu_case
(icu_category)
(text))
(icu_case
(icu_category)
(text))
(icu_case
(icu_category)
(interpolation
(expression
(identifier)))
(text)
(icu
(expression
(identifier))
(icu_clause)
(icu_case
(icu_category)
(text))
(icu_case
(icu_category)
(text))
(icu_case
(icu_category)
(text))))))

=====
With pipe
=====

Updated {minutes | async, plural,
=0 {just now}
=1 {one minute ago}
other {some minutes ago}}

---

(fragment
(text)
(icu
(expression
(identifier)
(pipe_sequence
(pipe_operator)
(pipe_call
(identifier))))
(icu_clause)
(icu_case
(icu_category)
(text))
(icu_case
(icu_category)
(text))
(icu_case
(icu_category)
(text))))
6 changes: 6 additions & 0 deletions examples/test.component.html
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,12 @@ <h1>Hi {{ user.name }}</h1>
</div>
</div>

<div class="icu">
<span i18n>
Updated {minutes, plural, =0 {just now} =1 {one minute ago} other {{{minutes}}}}
</span>
</div>

<div class="event-binding">
<button (click)="onClick()">Click me</button>
<button (click)="onClick($event)">Click me</button>
Expand Down
25 changes: 24 additions & 1 deletion grammar.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ module.exports = grammar(HTML, {
rules: {
// ---------- Root ---------
_node: ($, original) =>
choice(prec(1, $.interpolation), prec(1, $._any_statement), original),
choice(prec(1, $.icu), prec(1, $.interpolation), prec(1, $._any_statement), original),

// ---------- Overrides ----------
attribute_name: (_) => /[^<>\*.\[\]\(\)"'=\s]+/,
Expand Down Expand Up @@ -278,6 +278,29 @@ module.exports = grammar(HTML, {

assignment_expression: ($) =>
seq(field('name', $.identifier), '=', field('value', $._any_expression)),

// -------- ICU expressions ---------
icu: ($) =>
seq(
'{',
choice($._any_expression, $.concatenation_expression),
',',
$.icu_clause,
',',
repeat1($.icu_case),
'}',
),

icu_clause: () => choice('plural', 'select'),

icu_case: ($) => seq(
$.icu_category,
'{',
repeat1($._node),
'}'
),

icu_category: () => /[^{}]+/i,

// ---------- Interpolation ---------
interpolation: ($) =>
Expand Down
96 changes: 95 additions & 1 deletion src/grammar.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,14 @@
"_node": {
"type": "CHOICE",
"members": [
{
"type": "PREC",
"value": 1,
"content": {
"type": "SYMBOL",
"name": "icu"
}
},
{
"type": "PREC",
"value": 1,
Expand Down Expand Up @@ -1757,6 +1765,93 @@
}
]
},
"icu": {
"type": "SEQ",
"members": [
{
"type": "STRING",
"value": "{"
},
{
"type": "CHOICE",
"members": [
{
"type": "SYMBOL",
"name": "_any_expression"
},
{
"type": "SYMBOL",
"name": "concatenation_expression"
}
]
},
{
"type": "STRING",
"value": ","
},
{
"type": "SYMBOL",
"name": "icu_clause"
},
{
"type": "STRING",
"value": ","
},
{
"type": "REPEAT1",
"content": {
"type": "SYMBOL",
"name": "icu_case"
}
},
{
"type": "STRING",
"value": "}"
}
]
},
"icu_clause": {
"type": "CHOICE",
"members": [
{
"type": "STRING",
"value": "plural"
},
{
"type": "STRING",
"value": "select"
}
]
},
"icu_case": {
"type": "SEQ",
"members": [
{
"type": "SYMBOL",
"name": "icu_category"
},
{
"type": "STRING",
"value": "{"
},
{
"type": "REPEAT1",
"content": {
"type": "SYMBOL",
"name": "_node"
}
},
{
"type": "STRING",
"value": "}"
}
]
},
"icu_category": {
"type": "PATTERN",
"value": "[^{}]+",
"flags": "i"
},
"interpolation": {
"type": "SEQ",
"members": [
Expand Down Expand Up @@ -3218,4 +3313,3 @@
"inline": [],
"supertypes": []
}

Loading

0 comments on commit f1bbc4b

Please sign in to comment.