fix: continue parsing expression on func/method call (#909)

This commit is contained in:
Myriad-Dreamin 2024-11-29 15:30:51 +08:00 committed by GitHub
parent 5423976278
commit adef0381be
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
7 changed files with 106 additions and 5 deletions

View file

@ -33,11 +33,25 @@ function braceMatch(pattern: RegExp) {
return ("(?x)" + pattern.source) as unknown as RegExp;
}
function oneOf(...patterns: RegExp[]) {
return new RegExp(
patterns
.map((p) => {
const src = p.source;
if (src.startsWith("(")) {
return src;
}
return `(?:${src})`;
})
.join("|")
);
}
const PAREN_BLOCK = generatePattern(6, "\\(", "\\)");
const exprEndReg =
/(?<!(?:if|and|or|not|in|!=|==|<=|>=|<|>|\+|-|\*|\/|=|\+=|-=|\*=|\/=)\s*)(?=[\[\{\n])|(?=[;\}\]\)\n]|$)/;
// todo: This is invocable
const codeBlock: textmate.Pattern = {
// name: "meta.block.continuous.typst",
begin: /\{/,
@ -350,9 +364,21 @@ const enterExpression = (kind: string, seek: RegExp): textmate.Pattern => {
return {
/// name: 'markup.expr.typst'
begin: new RegExp("#" + seek.source),
// `?=(?<![\d#])\.[^\p{XID_Start}_]`: This means that we are on a dot and the next character is not a valid identifier start, but we are not at the beginning of hash or number
end: /(?<=;)|(?<=[\)\]\}])(?![;\(\[\$])|(?<!#)(?=")|(?=\.(?:[^0-9\p{XID_Start}_]|$))|(?=[\s\}\]\)\$]|$)|(;)/u
.source,
end: oneOf(
/(?<=;)/,
// Ends unless we are in a call or method call
new RegExp(
/(?<=[\)\]\}])(?![;\(\[\$]|(?:\.method-continue))/.source.replace(
/method-continue/g,
IDENTIFIER.source + /(?=[\(\[])/.source
)
),
/(?<!#)(?=")/,
// This means that we are on a dot and the next character is not a valid identifier start, but we are not at the beginning of hash or number
/(?=\.(?:[^0-9\p{XID_Start}_]|$))/u,
/(?=[\s\}\]\)\$]|$)/,
/(;)/
).source,
beginCaptures: {
"0": {
name: kind,
@ -631,7 +657,6 @@ const expressions = (): textmate.Grammar => {
},
},
/// parentheisized expressions: (...)
// todo: This is invocable
{
begin: /\(/,
end: /\)/,

View file

@ -0,0 +1 @@
#(module,).at(0).push(); ()

View file

@ -0,0 +1,20 @@
>#(module,).at(0).push(); ()
#^ source.typst keyword.control.hash.typst
# ^ source.typst meta.brace.round.typst
# ^^^^^^ source.typst variable.other.readwrite.typst
# ^ source.typst punctuation.separator.comma.typst
# ^ source.typst meta.brace.round.typst
# ^ source.typst meta.expr.call.typst keyword.operator.accessor.typst
# ^^ source.typst meta.expr.call.typst entity.name.function.typst
# ^ source.typst meta.expr.call.typst meta.brace.round.typst
# ^ source.typst meta.expr.call.typst constant.numeric.integer.typst
# ^ source.typst meta.expr.call.typst meta.brace.round.typst
# ^ source.typst meta.expr.call.typst keyword.operator.accessor.typst
# ^^^^ source.typst meta.expr.call.typst entity.name.function.typst
# ^ source.typst meta.expr.call.typst meta.brace.round.typst
# ^ source.typst meta.expr.call.typst meta.brace.round.typst
# ^ source.typst punctuation.terminator.statement.typst
# ^ source.typst
# ^ source.typst markup.content.brace.typst
# ^ source.typst markup.content.brace.typst
>

View file

@ -0,0 +1,2 @@
#let a = (1,);
#a.map(it => it).map(it => it); ()

View file

@ -0,0 +1,37 @@
>#let a = (1,);
#^ source.typst keyword.control.hash.typst
# ^^^ source.typst meta.expr.let.typst storage.type.typst
# ^ source.typst meta.expr.let.typst
# ^ source.typst meta.expr.let.typst variable.other.readwrite.typst
# ^ source.typst meta.expr.let.typst
# ^^ source.typst meta.expr.let.typst keyword.operator.assignment.typst
# ^ source.typst meta.expr.let.typst meta.brace.round.typst
# ^ source.typst meta.expr.let.typst constant.numeric.integer.typst
# ^ source.typst meta.expr.let.typst punctuation.separator.comma.typst
# ^ source.typst meta.expr.let.typst meta.brace.round.typst
# ^ source.typst punctuation.terminator.statement.typst
>#a.map(it => it).map(it => it); ()
#^ source.typst variable.other.readwrite.hash.typst
# ^ source.typst variable.other.readwrite.typst
# ^ source.typst meta.expr.call.typst keyword.operator.accessor.typst
# ^^^ source.typst meta.expr.call.typst entity.name.function.typst
# ^ source.typst meta.expr.call.typst meta.brace.round.typst
# ^^ source.typst meta.expr.call.typst variable.parameter.typst
# ^ source.typst meta.expr.call.typst
# ^^ source.typst meta.expr.call.typst storage.type.function.arrow.typst
# ^ source.typst meta.expr.call.typst
# ^^ source.typst meta.expr.call.typst variable.other.readwrite.typst
# ^ source.typst meta.expr.call.typst meta.brace.round.typst
# ^ source.typst meta.expr.call.typst keyword.operator.accessor.typst
# ^^^ source.typst meta.expr.call.typst entity.name.function.typst
# ^ source.typst meta.expr.call.typst meta.brace.round.typst
# ^^ source.typst meta.expr.call.typst variable.parameter.typst
# ^ source.typst meta.expr.call.typst
# ^^ source.typst meta.expr.call.typst storage.type.function.arrow.typst
# ^ source.typst meta.expr.call.typst
# ^^ source.typst meta.expr.call.typst variable.other.readwrite.typst
# ^ source.typst meta.expr.call.typst meta.brace.round.typst
# ^ source.typst punctuation.terminator.statement.typst
# ^ source.typst
# ^ source.typst markup.content.brace.typst
# ^ source.typst markup.content.brace.typst

View file

@ -0,0 +1 @@
#[test].at("text") ()

View file

@ -0,0 +1,15 @@
>#[test].at("text") ()
#^ source.typst keyword.control.hash.typst
# ^ source.typst meta.brace.square.typst
# ^^^^ source.typst
# ^ source.typst meta.brace.square.typst
# ^ source.typst meta.expr.call.typst keyword.operator.accessor.typst
# ^^ source.typst meta.expr.call.typst entity.name.function.typst
# ^ source.typst meta.expr.call.typst meta.brace.round.typst
# ^ source.typst meta.expr.call.typst string.quoted.double.typst punctuation.definition.string.typst
# ^^^^ source.typst meta.expr.call.typst string.quoted.double.typst
# ^ source.typst meta.expr.call.typst string.quoted.double.typst punctuation.definition.string.typst
# ^ source.typst meta.expr.call.typst meta.brace.round.typst
# ^ source.typst
# ^ source.typst markup.content.brace.typst
# ^ source.typst markup.content.brace.typst