mirror of
https://github.com/Myriad-Dreamin/tinymist.git
synced 2025-08-04 18:28:02 +00:00
feat: parse arrow functions like binary expr (#1128)
This commit is contained in:
parent
691a28ef55
commit
13e7325b7b
10 changed files with 94 additions and 101 deletions
|
@ -16,29 +16,10 @@ import {
|
|||
SYNTAX_WITH_MATH,
|
||||
} from "./feature.mjs";
|
||||
|
||||
// JS-Snippet to generate pattern
|
||||
function generatePattern(maxDepth: number, lb: string, rb: string) {
|
||||
const NOT_BRACE_PATTERN = `[^${rb}${lb}]`;
|
||||
|
||||
// Unrolled Pattern variants: 0=default, 1=unrolled (more efficient)
|
||||
let p = [`${lb}${NOT_BRACE_PATTERN}*(?:`, `${NOT_BRACE_PATTERN}*)*${rb}`];
|
||||
|
||||
// Generate and display the pattern
|
||||
return (
|
||||
p[0].repeat(maxDepth) +
|
||||
`${lb}${NOT_BRACE_PATTERN}*${rb}` +
|
||||
p[1].repeat(maxDepth)
|
||||
);
|
||||
}
|
||||
|
||||
function lookAhead(pattern: RegExp) {
|
||||
return new RegExp(`(?=(?:${pattern.source}))`);
|
||||
}
|
||||
|
||||
function braceMatch(pattern: RegExp) {
|
||||
return ("(?x)" + pattern.source) as unknown as RegExp;
|
||||
}
|
||||
|
||||
function oneOf(...patterns: RegExp[]) {
|
||||
return new RegExp(
|
||||
patterns
|
||||
|
@ -61,10 +42,8 @@ function replaceGroup(pattern: RegExp, group: string, replacement: RegExp) {
|
|||
);
|
||||
}
|
||||
|
||||
const PAREN_BLOCK = generatePattern(6, "\\(", "\\)");
|
||||
const exprEndReg = (() => {
|
||||
const tokens = [
|
||||
// while|if|and|or|not|in|!=|==|<=|>=|<|>|\+|-|\*|\/|=|\+=|-=|\*=|\/=
|
||||
/while/,
|
||||
/if/,
|
||||
/and/,
|
||||
|
@ -81,6 +60,7 @@ const exprEndReg = (() => {
|
|||
/-/,
|
||||
/\*/,
|
||||
/\//,
|
||||
/=>/,
|
||||
/=/,
|
||||
/\+=/,
|
||||
/-=/,
|
||||
|
@ -645,7 +625,6 @@ const expressions = (): textmate.Grammar => {
|
|||
const expression: textmate.Pattern = {
|
||||
patterns: [
|
||||
{ include: "#comments" },
|
||||
{ include: "#arrowFunc" },
|
||||
{ include: "#arrayOrDict" },
|
||||
{ include: "#contentBlock" },
|
||||
{
|
||||
|
@ -718,6 +697,10 @@ const expressions = (): textmate.Grammar => {
|
|||
/\+|\\|\/|(?<![[:alpha:]])(?<!\w)(?<!\d)-(?![[:alnum:]-][[:alpha:]_])/,
|
||||
name: "keyword.operator.arithmetic.typst",
|
||||
},
|
||||
{
|
||||
match: /=>/,
|
||||
name: "storage.type.function.arrow.typst",
|
||||
},
|
||||
{
|
||||
match: /==|!=|<=|<|>=|>/,
|
||||
name: "keyword.operator.relational.typst",
|
||||
|
@ -1579,67 +1562,6 @@ const mathFuncCallOrPropAccess = (): textmate.Pattern => {
|
|||
};
|
||||
};
|
||||
|
||||
// todo: #x => y should be parsed as |#x|=>|y
|
||||
// https://github.com/microsoft/vscode-textmate/blob/main/test-cases/themes/syntaxes/TypeScript.tmLanguage.json
|
||||
const arrowFunc: textmate.Pattern = {
|
||||
name: "meta.expr.arrow-function.typst",
|
||||
patterns: [
|
||||
{
|
||||
match: new RegExp(`(${IDENTIFIER.source})` + /\s*(?==>)/.source),
|
||||
captures: {
|
||||
"1": {
|
||||
name: "variable.parameter.typst",
|
||||
},
|
||||
},
|
||||
},
|
||||
{
|
||||
begin: braceMatch(lookAhead(new RegExp(PAREN_BLOCK + /\s*=>/.source))),
|
||||
end: /(?==>)/,
|
||||
patterns: [
|
||||
{
|
||||
include: "#comments",
|
||||
},
|
||||
{
|
||||
begin: /\(/,
|
||||
end: /\)/,
|
||||
beginCaptures: {
|
||||
"0": {
|
||||
name: "meta.brace.round.typst",
|
||||
},
|
||||
},
|
||||
endCaptures: {
|
||||
"0": {
|
||||
name: "meta.brace.round.typst",
|
||||
},
|
||||
},
|
||||
patterns: [
|
||||
{
|
||||
include: "#patternOrArgsBody",
|
||||
},
|
||||
],
|
||||
},
|
||||
],
|
||||
},
|
||||
{
|
||||
begin: /=>/,
|
||||
end: /(?<=\}|\])|(?:(?!\{|\[)(?=[\n\S;\}\]\)]))/,
|
||||
beginCaptures: {
|
||||
"0": {
|
||||
name: "storage.type.function.arrow.typst",
|
||||
},
|
||||
},
|
||||
patterns: [
|
||||
{
|
||||
include: "#comments",
|
||||
},
|
||||
{
|
||||
include: "#expression",
|
||||
},
|
||||
],
|
||||
},
|
||||
],
|
||||
};
|
||||
|
||||
export const typst: textmate.Grammar = {
|
||||
repository: {
|
||||
common,
|
||||
|
@ -1707,15 +1629,12 @@ export const typst: textmate.Grammar = {
|
|||
mathCallArgs,
|
||||
codeBlock,
|
||||
contentBlock,
|
||||
arrowFunc,
|
||||
},
|
||||
};
|
||||
|
||||
function generate() {
|
||||
const dirname = fileURLToPath(new URL(".", import.meta.url));
|
||||
|
||||
const typstPath = path.join(dirname, "../typst.tmLanguage");
|
||||
|
||||
let compiled = textmate.compile(typst);
|
||||
|
||||
if (POLYFILL_P_XID) {
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
>#(it => it)
|
||||
#^ source.typst keyword.control.hash.typst
|
||||
# ^ source.typst meta.brace.round.typst
|
||||
# ^^ source.typst variable.parameter.typst
|
||||
# ^^ source.typst variable.other.readwrite.typst
|
||||
# ^ source.typst
|
||||
# ^^ source.typst storage.type.function.arrow.typst
|
||||
# ^ source.typst
|
||||
|
@ -149,7 +149,7 @@
|
|||
>#(it => {})
|
||||
#^ source.typst keyword.control.hash.typst
|
||||
# ^ source.typst meta.brace.round.typst
|
||||
# ^^ source.typst variable.parameter.typst
|
||||
# ^^ source.typst variable.other.readwrite.typst
|
||||
# ^ source.typst
|
||||
# ^^ source.typst storage.type.function.arrow.typst
|
||||
# ^ source.typst
|
||||
|
|
5
syntaxes/textmate/tests/unit/basic/arrow-hard.typ
Normal file
5
syntaxes/textmate/tests/unit/basic/arrow-hard.typ
Normal file
|
@ -0,0 +1,5 @@
|
|||
#a => {
|
||||
#((a: ")") => { })
|
||||
#((a: ") => 1") => { })
|
||||
#(a: ") =>") => {
|
||||
#((a: ") =>") => { })
|
72
syntaxes/textmate/tests/unit/basic/arrow-hard.typ.snap
Normal file
72
syntaxes/textmate/tests/unit/basic/arrow-hard.typ.snap
Normal file
|
@ -0,0 +1,72 @@
|
|||
>#a => {
|
||||
#^ source.typst variable.other.readwrite.hash.typst
|
||||
# ^ source.typst variable.other.readwrite.typst
|
||||
# ^^^^ source.typst
|
||||
# ^ source.typst markup.content.brace.typst
|
||||
>#((a: ")") => { })
|
||||
#^ source.typst keyword.control.hash.typst
|
||||
# ^ source.typst meta.brace.round.typst
|
||||
# ^ source.typst meta.brace.round.typst
|
||||
# ^ source.typst variable.other.readwrite.typst
|
||||
# ^ source.typst punctuation.separator.colon.typst
|
||||
# ^ source.typst
|
||||
# ^ source.typst string.quoted.double.typst punctuation.definition.string.typst
|
||||
# ^ source.typst string.quoted.double.typst
|
||||
# ^ source.typst string.quoted.double.typst punctuation.definition.string.typst
|
||||
# ^ source.typst meta.brace.round.typst
|
||||
# ^ source.typst
|
||||
# ^^ source.typst storage.type.function.arrow.typst
|
||||
# ^ source.typst
|
||||
# ^ source.typst meta.brace.curly.typst
|
||||
# ^ source.typst
|
||||
# ^ source.typst meta.brace.curly.typst
|
||||
# ^ source.typst meta.brace.round.typst
|
||||
>#((a: ") => 1") => { })
|
||||
#^ source.typst keyword.control.hash.typst
|
||||
# ^ source.typst meta.brace.round.typst
|
||||
# ^ source.typst meta.brace.round.typst
|
||||
# ^ source.typst variable.other.readwrite.typst
|
||||
# ^ source.typst punctuation.separator.colon.typst
|
||||
# ^ source.typst
|
||||
# ^ source.typst string.quoted.double.typst punctuation.definition.string.typst
|
||||
# ^^^^^^ source.typst string.quoted.double.typst
|
||||
# ^ source.typst string.quoted.double.typst punctuation.definition.string.typst
|
||||
# ^ source.typst meta.brace.round.typst
|
||||
# ^ source.typst
|
||||
# ^^ source.typst storage.type.function.arrow.typst
|
||||
# ^ source.typst
|
||||
# ^ source.typst meta.brace.curly.typst
|
||||
# ^ source.typst
|
||||
# ^ source.typst meta.brace.curly.typst
|
||||
# ^ source.typst meta.brace.round.typst
|
||||
>#(a: ") =>") => {
|
||||
#^ source.typst keyword.control.hash.typst
|
||||
# ^ source.typst meta.brace.round.typst
|
||||
# ^ source.typst variable.other.readwrite.typst
|
||||
# ^ source.typst punctuation.separator.colon.typst
|
||||
# ^ source.typst
|
||||
# ^ source.typst string.quoted.double.typst punctuation.definition.string.typst
|
||||
# ^^^^ source.typst string.quoted.double.typst
|
||||
# ^ source.typst string.quoted.double.typst punctuation.definition.string.typst
|
||||
# ^ source.typst meta.brace.round.typst
|
||||
# ^^^^ source.typst
|
||||
# ^ source.typst markup.content.brace.typst
|
||||
>#((a: ") =>") => { })
|
||||
#^ source.typst keyword.control.hash.typst
|
||||
# ^ source.typst meta.brace.round.typst
|
||||
# ^ source.typst meta.brace.round.typst
|
||||
# ^ source.typst variable.other.readwrite.typst
|
||||
# ^ source.typst punctuation.separator.colon.typst
|
||||
# ^ source.typst
|
||||
# ^ source.typst string.quoted.double.typst punctuation.definition.string.typst
|
||||
# ^^^^ source.typst string.quoted.double.typst
|
||||
# ^ source.typst string.quoted.double.typst punctuation.definition.string.typst
|
||||
# ^ source.typst meta.brace.round.typst
|
||||
# ^ source.typst
|
||||
# ^^ source.typst storage.type.function.arrow.typst
|
||||
# ^ source.typst
|
||||
# ^ source.typst meta.brace.curly.typst
|
||||
# ^ source.typst
|
||||
# ^ source.typst meta.brace.curly.typst
|
||||
# ^ source.typst meta.brace.round.typst
|
||||
>
|
|
@ -16,7 +16,7 @@
|
|||
# ^ 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 variable.other.readwrite.typst
|
||||
# ^ source.typst meta.expr.call.typst
|
||||
# ^^ source.typst meta.expr.call.typst storage.type.function.arrow.typst
|
||||
# ^ source.typst meta.expr.call.typst
|
||||
|
@ -25,7 +25,7 @@
|
|||
# ^ 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 variable.other.readwrite.typst
|
||||
# ^ source.typst meta.expr.call.typst
|
||||
# ^^ source.typst meta.expr.call.typst storage.type.function.arrow.typst
|
||||
# ^ source.typst meta.expr.call.typst
|
||||
|
|
|
@ -5,7 +5,7 @@
|
|||
# ^^^^ source.typst meta.expr.show.typst variable.other.readwrite.typst
|
||||
# ^ source.typst meta.expr.show.typst punctuation.separator.colon.typst
|
||||
# ^ source.typst meta.expr.show.typst
|
||||
# ^^ source.typst meta.expr.show.typst variable.parameter.typst
|
||||
# ^^ source.typst meta.expr.show.typst variable.other.readwrite.typst
|
||||
# ^ source.typst meta.expr.show.typst
|
||||
# ^^ source.typst meta.expr.show.typst storage.type.function.arrow.typst
|
||||
# ^ source.typst meta.expr.show.typst
|
||||
|
@ -21,7 +21,7 @@
|
|||
# ^ source.typst meta.expr.show.typst meta.expr.call.typst keyword.operator.accessor.typst
|
||||
# ^^^ source.typst meta.expr.show.typst meta.expr.call.typst entity.name.function.typst
|
||||
# ^ source.typst meta.expr.show.typst meta.expr.call.typst meta.brace.round.typst
|
||||
# ^ source.typst meta.expr.show.typst meta.expr.call.typst variable.parameter.typst
|
||||
# ^ source.typst meta.expr.show.typst meta.expr.call.typst variable.other.readwrite.typst
|
||||
# ^ source.typst meta.expr.show.typst meta.expr.call.typst
|
||||
# ^^ source.typst meta.expr.show.typst meta.expr.call.typst storage.type.function.arrow.typst
|
||||
# ^ source.typst meta.expr.show.typst meta.expr.call.typst
|
||||
|
|
|
@ -3,7 +3,7 @@
|
|||
# ^^^^ source.typst meta.expr.show.typst keyword.control.other.typst
|
||||
# ^ source.typst meta.expr.show.typst punctuation.separator.colon.typst
|
||||
# ^ source.typst meta.expr.show.typst
|
||||
# ^^ source.typst meta.expr.show.typst variable.parameter.typst
|
||||
# ^^ source.typst meta.expr.show.typst variable.other.readwrite.typst
|
||||
# ^ source.typst meta.expr.show.typst
|
||||
# ^^ source.typst meta.expr.show.typst storage.type.function.arrow.typst
|
||||
# ^ source.typst meta.expr.show.typst
|
||||
|
@ -15,7 +15,7 @@
|
|||
# ^^^^ source.typst meta.expr.show.typst variable.other.readwrite.typst
|
||||
# ^ source.typst meta.expr.show.typst punctuation.separator.colon.typst
|
||||
# ^ source.typst meta.expr.show.typst
|
||||
# ^^ source.typst meta.expr.show.typst variable.parameter.typst
|
||||
# ^^ source.typst meta.expr.show.typst variable.other.readwrite.typst
|
||||
# ^ source.typst meta.expr.show.typst
|
||||
# ^^ source.typst meta.expr.show.typst storage.type.function.arrow.typst
|
||||
# ^ source.typst meta.expr.show.typst
|
||||
|
|
|
@ -1,7 +1,4 @@
|
|||
>#x => y
|
||||
#^ source.typst variable.other.readwrite.hash.typst
|
||||
# ^ source.typst variable.parameter.typst
|
||||
# ^ source.typst
|
||||
# ^^ source.typst storage.type.function.arrow.typst
|
||||
# ^ source.typst
|
||||
# ^ source.typst variable.other.readwrite.typst
|
||||
# ^ source.typst variable.other.readwrite.typst
|
||||
# ^^^^^^ source.typst
|
|
@ -5,7 +5,7 @@
|
|||
# ^^^ 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 variable.parameter.typst
|
||||
# ^ source.typst meta.expr.let.typst variable.other.readwrite.typst
|
||||
# ^ source.typst meta.expr.let.typst
|
||||
# ^^ source.typst meta.expr.let.typst storage.type.function.arrow.typst
|
||||
# ^ source.typst meta.expr.let.typst
|
||||
|
|
|
@ -6,7 +6,7 @@
|
|||
# ^^^^ source.typst meta.expr.show.typst keyword.control.other.typst
|
||||
# ^ source.typst meta.expr.show.typst punctuation.separator.colon.typst
|
||||
# ^ source.typst meta.expr.show.typst
|
||||
# ^^^^ source.typst meta.expr.show.typst variable.parameter.typst
|
||||
# ^^^^ source.typst meta.expr.show.typst variable.other.readwrite.typst
|
||||
# ^ source.typst meta.expr.show.typst
|
||||
# ^^ source.typst meta.expr.show.typst storage.type.function.arrow.typst
|
||||
# ^ source.typst meta.expr.show.typst
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue