Skip to content

Commit

Permalink
[YAML] Fix unbalanced quoted scalars (#3923)
Browse files Browse the repository at this point in the history
This commit scopes tokens after closing quotation mark illegal to discover
unbalanced quotation marks.
  • Loading branch information
deathaxe authored Feb 18, 2024
1 parent 2c829d1 commit 225202d
Show file tree
Hide file tree
Showing 2 changed files with 114 additions and 14 deletions.
54 changes: 44 additions & 10 deletions YAML/YAML.sublime-syntax
Original file line number Diff line number Diff line change
Expand Up @@ -265,20 +265,20 @@ contexts:

flow-scalar-11:
# http://yaml.org/spec/1.2/spec.html#style/flow/scalar
- include: flow-scalar-double-quoted
- include: flow-scalar-single-quoted
- include: flow-scalar-double-quoted-in
- include: flow-scalar-single-quoted-in
- include: flow-scalar-plain-in-11

flow-scalar-12:
# http://yaml.org/spec/1.2/spec.html#style/flow/scalar
- include: flow-scalar-double-quoted
- include: flow-scalar-single-quoted
- include: flow-scalar-double-quoted-in
- include: flow-scalar-single-quoted-in
- include: flow-scalar-plain-in-12

flow-scalar-out-12:
# for block keys
- include: flow-scalar-double-quoted
- include: flow-scalar-single-quoted
- include: flow-scalar-double-quoted-out
- include: flow-scalar-single-quoted-out
- include: flow-scalar-plain-out-12

###[ COMMENTS ]###############################################################
Expand Down Expand Up @@ -466,12 +466,23 @@ contexts:
2: variable.other.alias.yaml
3: invalid.illegal.character.anchor.yaml

flow-scalar-double-quoted:
flow-scalar-double-quoted-in:
# http://yaml.org/spec/1.2/spec.html#style/flow/double-quoted
# c-double-quoted(n,c)
- match: \"
scope: punctuation.definition.string.begin.yaml
push: flow-scalar-double-quoted-body
push:
- flow-scalar-in-expect-end
- flow-scalar-double-quoted-body

flow-scalar-double-quoted-out:
# http://yaml.org/spec/1.2/spec.html#style/flow/double-quoted
# c-double-quoted(n,c)
- match: \"
scope: punctuation.definition.string.begin.yaml
push:
- flow-scalar-out-expect-end
- flow-scalar-double-quoted-body

flow-scalar-double-quoted-body:
# TODO consider scoping meaningful trailing whitespace for color
Expand All @@ -486,12 +497,23 @@ contexts:
scope: punctuation.definition.string.end.yaml
pop: 1

flow-scalar-single-quoted:
flow-scalar-single-quoted-in:
# http://yaml.org/spec/1.2/spec.html#style/flow/single-quoted
# c-single-quoted(n,c)
- match: \'
scope: punctuation.definition.string.begin.yaml
push:
- flow-scalar-in-expect-end
- flow-scalar-single-quoted-body

flow-scalar-single-quoted-out:
# http://yaml.org/spec/1.2/spec.html#style/flow/single-quoted
# c-single-quoted(n,c)
- match: \'
scope: punctuation.definition.string.begin.yaml
push: flow-scalar-single-quoted-body
push:
- flow-scalar-out-expect-end
- flow-scalar-single-quoted-body

flow-scalar-single-quoted-body:
- meta_include_prototype: false
Expand All @@ -502,6 +524,18 @@ contexts:
scope: punctuation.definition.string.end.yaml
pop: 1

flow-scalar-in-expect-end:
- meta_include_prototype: false
- match: (?:(?!{{_flow_scalar_end_plain_in}}).)*
scope: invalid.illegal.unexpected.yaml
pop: 1

flow-scalar-out-expect-end:
- meta_include_prototype: false
- match: (?:(?!{{_flow_scalar_end_plain_out}}).)*
scope: invalid.illegal.unexpected.yaml
pop: 1

flow-scalar-plain-in-implicit-type-11:
- match: '{{_type_bool_11}}{{_flow_scalar_end_plain_in}}'
scope: constant.language.boolean.yaml
Expand Down
74 changes: 70 additions & 4 deletions YAML/tests/syntax_test_flow.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,13 @@
# http://yaml.org/spec/1.2/spec.html#style/flow/double-quoted

"flow string"
#^^^^^^^^^^^^ string.quoted.double
#^^^^^^^^^^^^ string.quoted.double - invalid
# ^ punctuation.definition.string.end
# <- string.quoted.double punctuation.definition.string.begin

"flow string
more string"
#^^^^^^^^^^^^ string.quoted.double
#^^^^^^^^^^^^ string.quoted.double - invalid
# ^ punctuation.definition.string.end

"\" \0 \\ \x1b \u12c4 \U1234F678"
Expand Down Expand Up @@ -59,19 +59,27 @@
"string\
continuation"

"string"illegal
# <- meta.string.yaml string.quoted.double.yaml - invalid
#^^^^^^^ meta.string.yaml string.quoted.double.yaml - invalid
# ^^^^^^^ invalid.illegal.unexpected.yaml

unquoted"string"
# <- meta.string.yaml string.unquoted.plain.out.yaml
#^^^^^^^^^^^^^^^ meta.string.yaml string.unquoted.plain.out.yaml - invalid

##############################################################################
## Single-quoted
# http://yaml.org/spec/1.2/spec.html#style/flow/single-quoted

'flow string'
#^^^^^^^^^^^^ string.quoted.single
#^^^^^^^^^^^^ string.quoted.single - invalid
# ^ punctuation.definition.string.end
# <- string.quoted.single punctuation.definition.string.begin

'flow string
more string'
#^^^^^^^^^^^^ string.quoted.single
#^^^^^^^^^^^^ string.quoted.single - invalid
# ^ punctuation.definition.string.end
# <- string.quoted.single

Expand All @@ -80,6 +88,14 @@ continuation"
# ^^ constant.character.escape
# ^ -constant

'string'illegal
# <- meta.string.yaml string.quoted.single.yaml - invalid
#^^^^^^^ meta.string.yaml string.quoted.single.yaml - invalid
# ^^^^^^^ invalid.illegal.unexpected.yaml

unquoted'string'
# <- meta.string.yaml string.unquoted.plain.out.yaml
#^^^^^^^^^^^^^^^ meta.string.yaml string.unquoted.plain.out.yaml - invalid

##############################################################################
## Flow-sequence
Expand Down Expand Up @@ -107,6 +123,36 @@ continuation"
# ^ punctuation.separator.sequence
# ^^^ string.unquoted.plain.in - entity.name.tag

[ "str"err, 'str'err, "str"err ]
#^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ meta.sequence.flow.yaml
# ^^^^^ meta.string.yaml string.quoted.double.yaml
# ^^^ invalid.illegal.unexpected.yaml
# ^ punctuation.separator.sequence.yaml
# ^^^^^ meta.string.yaml string.quoted.single.yaml
# ^^^ invalid.illegal.unexpected.yaml
# ^ punctuation.separator.sequence.yaml
# ^^^^^ meta.string.yaml string.quoted.double.yaml
# ^^^ invalid.illegal.unexpected.yaml
# ^ punctuation.definition.sequence.end.yaml

# mapping keys are not matched
[ "k"err: "v"err, "k"err : "v"err ]
# <- meta.sequence.flow.yaml punctuation.definition.sequence.begin.yaml
#^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ meta.sequence.flow.yaml
# ^^^ meta.string.yaml string.quoted.double.yaml
# ^^^ invalid.illegal.unexpected.yaml
# ^ meta.mapping.yaml punctuation.separator.key-value.mapping.yaml
# ^^^ meta.mapping.value.yaml meta.string.yaml string.quoted.double.yaml
# ^^^ meta.mapping.value.yaml invalid.illegal.unexpected.yaml
# ^ punctuation.separator.sequence.yaml
# ^^^ meta.string.yaml string.quoted.double.yaml
# ^^^ invalid.illegal.unexpected.yaml
# ^ - invalid
# ^ meta.mapping.yaml punctuation.separator.key-value.mapping.yaml
# ^^^ meta.mapping.value.yaml meta.string.yaml string.quoted.double.yaml
# ^^^ meta.mapping.value.yaml invalid.illegal.unexpected.yaml
# ^ meta.mapping.value.yaml - invalid
# ^ punctuation.definition.sequence.end.yaml

##############################################################################
## Flow-mapping
Expand Down Expand Up @@ -155,6 +201,26 @@ continuation"
# ^ meta.number.integer.decimal.yaml constant.numeric.value.yaml
# ^ meta.mapping.yaml punctuation.definition.mapping.end.yaml

{ "k"err: "v"err, 'k'err: 'v'err }
# <- meta.mapping.yaml punctuation.definition.mapping.begin.yaml
#^^^^^^^^ meta.mapping.yaml
# ^^^^^^^ meta.mapping.value.yaml
# ^^^^^^^^^ meta.mapping.yaml
# ^^^^^^^^ meta.mapping.value.yaml
# ^ meta.mapping.yaml
# ^^^ meta.string.yaml string.quoted.double.yaml
# ^^^ invalid.illegal.unexpected.yaml - string
# ^ punctuation.separator.key-value.mapping.yaml
# ^^^ meta.string.yaml string.quoted.double.yaml
# ^^^ invalid.illegal.unexpected.yaml - string
# ^ punctuation.separator.sequence.yaml
# ^^^ meta.string.yaml string.quoted.single.yaml
# ^^^ invalid.illegal.unexpected.yaml - string
# ^ punctuation.separator.key-value.mapping.yaml
# ^^^ meta.string.yaml string.quoted.single.yaml
# ^^^ invalid.illegal.unexpected.yaml - string
# ^ punctuation.definition.mapping.end.yaml

{? !!str 'key' : !!str value,
# ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ meta.mapping - meta.mapping meta.mapping
# ^^^^^^^^^^^^ meta.mapping.value
Expand Down

0 comments on commit 225202d

Please sign in to comment.