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

String escapes #3

Merged
merged 4 commits into from Jul 26, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
11 changes: 3 additions & 8 deletions corpus/test.txt
Expand Up @@ -481,7 +481,6 @@ module my-string-testing-module {
description 'Single tick stings!';

description "Can contain \"escaped\" double quotes";
description 'Can contain \'escaped\' single quotes';
}
}

Expand Down Expand Up @@ -513,20 +512,16 @@ module my-string-testing-module {
(escape_sequence)
(string_fragment)
(escape_sequence)
(string_fragment))))
(statement
(statement_keyword)
(argument
(string))))))))
(string_fragment)))))))))

================================================================================
Can parse strings with escape sequences
================================================================================

module my-string-testing-module {
container lets-test-some-strings {
description "Can contain escape sequences like \n and \r";
description 'Can\'t contain any escape sequences like \n and \r';
description "Can contain escape sequences like \n and \t";
description 'Cannot contain escape sequences like \n and \t';
}
}

Expand Down
22 changes: 6 additions & 16 deletions grammar.js
Expand Up @@ -194,18 +194,13 @@ module.exports = grammar({
$._unescaped_double_string_fragment,
$.string_fragment
),
alias($._escape_sequence, $.escape_sequence),
$.escape_sequence,
)),
'"'
),
seq(
"'",
repeat(choice(
$._unescaped_single_string_fragment,

// Escape sequences should be hidden in single tick strings
$._escape_sequence,
)),
optional($._unescaped_single_string_fragment),
"'"
)
),
Expand All @@ -219,18 +214,13 @@ module.exports = grammar({
token.immediate(prec(1, /[^"\\]+/)),

_unescaped_single_string_fragment: $ =>
token.immediate(prec(1, /[^'\\]+/)),
token.immediate(prec(1, /[^']+/)),

_escape_sequence: $ => token.immediate(seq(
escape_sequence: $ => token.immediate(seq(
'\\',
choice(
/[^xu0-7]/,
/[0-7]{1,3}/,
/x[0-9a-fA-F]{2}/,
/u[0-9a-fA-F]{4}/,
/u{[0-9a-fA-F]+}/
choice('n', 't', '"', '\\')
)
)),
),

date: $ => /\d{4}-\d{2}-\d{2}/,

Expand Down
3 changes: 3 additions & 0 deletions queries/highlights.scm
Expand Up @@ -38,3 +38,6 @@
(plus_symbol) @operator
["{" "}"] @punctuation.bracket
[";"] @punctuation.delimiter

; Errors
(ERROR) @error
87 changes: 87 additions & 0 deletions scripts/fix-bad-patterns.patch
@@ -0,0 +1,87 @@
diff --git a/standard/ietf/DRAFT/ietf-isis.yang b/standard/ietf/DRAFT/ietf-isis.yang
index 1849e992..d5ea6139 100644
--- a/standard/ietf/DRAFT/ietf-isis.yang
+++ b/standard/ietf/DRAFT/ietf-isis.yang
@@ -126,8 +126,8 @@ module ietf-isis {
typedef isis-lsp-id {
type string {
pattern
- "[0-9A-Fa-f]{4}\.[0-9A-Fa-f]{4}\.[0-9A-Fa-f]"
- +"{4}.[0-9][0-9]-[0-9][0-9]";
+ '[0-9A-Fa-f]{4}\.[0-9A-Fa-f]{4}\.[0-9A-Fa-f]'
+ +'{4}.[0-9][0-9]-[0-9][0-9]';
}
description
"This type defines isis LSP ID using pattern,
@@ -135,9 +135,9 @@ module ietf-isis {
}
typedef simple-iso-address {
type string {
- pattern "[0-9A-Fa-f]{2}\.([0-9A-Fa-f]{4}\.){0,3}"
- +"[0-9A-Fa-f]{4}\.[0-9A-Fa-f]{4}\.[0-9A-Fa-f]{4}."
- +"[0-9][0-9]";
+ pattern '[0-9A-Fa-f]{2}\.([0-9A-Fa-f]{4}\.){0,3}'
+ +'[0-9A-Fa-f]{4}\.[0-9A-Fa-f]{4}\.[0-9A-Fa-f]{4}.'
+ +'[0-9][0-9]';
}
description
"This type defines simple iso address format,
@@ -147,7 +147,7 @@ module ietf-isis {

typedef isis-system-id {
type string {
- pattern "[0-9A-Fa-f]{4}\.[0-9A-Fa-f]{4}\.[0-9A-Fa-f]{4}.00";
+ pattern '[0-9A-Fa-f]{4}\.[0-9A-Fa-f]{4}\.[0-9A-Fa-f]{4}.00';
}
description
"This type defines isis system id using pattern,
@@ -1701,5 +1701,3 @@ module ietf-isis {
}

}
-
-
diff --git a/standard/ietf/RFC/ietf-ipfix-psamp@2012-09-05.yang b/standard/ietf/RFC/ietf-ipfix-psamp@2012-09-05.yang
index 744f0964..a9f9607b 100644
--- a/standard/ietf/RFC/ietf-ipfix-psamp@2012-09-05.yang
+++ b/standard/ietf/RFC/ietf-ipfix-psamp@2012-09-05.yang
@@ -256,7 +256,7 @@ module ietf-ipfix-psamp {
typedef ieNameType {
type string {
length "1..max";
- pattern "\S+";
+ pattern '\S+';
}
description "Type for Information Element names. Whitespaces
are not allowed.";
@@ -276,7 +276,7 @@ module ietf-ipfix-psamp {
typedef nameType {
type string {
length "1..max";
- pattern "\S(.*\S)?";
+ pattern '\S(.*\S)?';
}
description "Type for 'name' leafs, which are used to identify
specific instances within lists, etc.
diff --git a/standard/ietf/RFC/ietf-netconf-acm@2012-02-22.yang b/standard/ietf/RFC/ietf-netconf-acm@2012-02-22.yang
index 93ac229f..19ab8345 100644
--- a/standard/ietf/RFC/ietf-netconf-acm@2012-02-22.yang
+++ b/standard/ietf/RFC/ietf-netconf-acm@2012-02-22.yang
@@ -100,7 +100,7 @@ module ietf-netconf-acm {

typedef matchall-string-type {
type string {
- pattern "\*";
+ pattern '\*';
}
description
"The string containing a single asterisk '*' is used
@@ -141,7 +141,7 @@ module ietf-netconf-acm {
typedef group-name-type {
type string {
length "1..max";
- pattern "[^\*].*";
+ pattern '[^\*].*';
}
description
"Name of administrative group to which
3 changes: 3 additions & 0 deletions scripts/update-yang-models.sh
Expand Up @@ -4,4 +4,7 @@ rm -rf yang-models

git clone --depth=1 https://github.com/YangModels/yang yang-models

# Patch up faulty models.
patch -dyang-models -p1 < scripts/fix-bad-patterns.patch

touch yang-models/.gitkeep
57 changes: 22 additions & 35 deletions src/grammar.json
Expand Up @@ -599,13 +599,8 @@
"value": "string_fragment"
},
{
"type": "ALIAS",
"content": {
"type": "SYMBOL",
"name": "_escape_sequence"
},
"named": true,
"value": "escape_sequence"
"type": "SYMBOL",
"name": "escape_sequence"
}
]
}
Expand All @@ -624,20 +619,16 @@
"value": "'"
},
{
"type": "REPEAT",
"content": {
"type": "CHOICE",
"members": [
{
"type": "SYMBOL",
"name": "_unescaped_single_string_fragment"
},
{
"type": "SYMBOL",
"name": "_escape_sequence"
}
]
}
"type": "CHOICE",
"members": [
{
"type": "SYMBOL",
"name": "_unescaped_single_string_fragment"
},
{
"type": "BLANK"
}
]
},
{
"type": "STRING",
Expand Down Expand Up @@ -695,11 +686,11 @@
"value": 1,
"content": {
"type": "PATTERN",
"value": "[^'\\\\]+"
"value": "[^']+"
}
}
},
"_escape_sequence": {
"escape_sequence": {
"type": "IMMEDIATE_TOKEN",
"content": {
"type": "SEQ",
Expand All @@ -712,24 +703,20 @@
"type": "CHOICE",
"members": [
{
"type": "PATTERN",
"value": "[^xu0-7]"
},
{
"type": "PATTERN",
"value": "[0-7]{1,3}"
"type": "STRING",
"value": "n"
},
{
"type": "PATTERN",
"value": "x[0-9a-fA-F]{2}"
"type": "STRING",
"value": "t"
},
{
"type": "PATTERN",
"value": "u[0-9a-fA-F]{4}"
"type": "STRING",
"value": "\""
},
{
"type": "PATTERN",
"value": "u{[0-9a-fA-F]+}"
"type": "STRING",
"value": "\\"
}
]
}
Expand Down