From 0d83290e1367edc1a8f9c7686bb2032067c177fa Mon Sep 17 00:00:00 2001 From: Dominik Nakamura Date: Thu, 5 Oct 2023 16:17:04 +0900 Subject: [PATCH] feat: extend syntax highlighting --- .../tests/inputs/struct-tuple.stef | 2 + .../parser__parse@struct-tuple.stef.snap | 41 ++++++++++ .../parser__print@struct-tuple.stef.snap | 9 +++ .../syntaxes/stef.tmLanguage.yaml | 75 ++++++++++++++++--- 4 files changed, 116 insertions(+), 11 deletions(-) create mode 100644 crates/stef-parser/tests/inputs/struct-tuple.stef create mode 100644 crates/stef-parser/tests/snapshots/parser__parse@struct-tuple.stef.snap create mode 100644 crates/stef-parser/tests/snapshots/parser__print@struct-tuple.stef.snap diff --git a/crates/stef-parser/tests/inputs/struct-tuple.stef b/crates/stef-parser/tests/inputs/struct-tuple.stef new file mode 100644 index 0000000..c0edb19 --- /dev/null +++ b/crates/stef-parser/tests/inputs/struct-tuple.stef @@ -0,0 +1,2 @@ +/// Basic struct. +struct Sample(u32 @1, bool @2) diff --git a/crates/stef-parser/tests/snapshots/parser__parse@struct-tuple.stef.snap b/crates/stef-parser/tests/snapshots/parser__parse@struct-tuple.stef.snap new file mode 100644 index 0000000..df761c2 --- /dev/null +++ b/crates/stef-parser/tests/snapshots/parser__parse@struct-tuple.stef.snap @@ -0,0 +1,41 @@ +--- +source: crates/stef-parser/tests/parser.rs +expression: "Schema :: parse(input.as_str()).unwrap()" +input_file: crates/stef-parser/tests/inputs/struct-tuple.stef +--- +Schema { + definitions: [ + Struct( + Struct { + comment: Comment( + [ + "Basic struct.", + ], + ), + attributes: Attributes( + [], + ), + name: "Sample", + generics: Generics( + [], + ), + fields: Unnamed( + [ + UnnamedField { + ty: U32, + id: Id( + 1, + ), + }, + UnnamedField { + ty: Bool, + id: Id( + 2, + ), + }, + ], + ), + }, + ), + ], +} diff --git a/crates/stef-parser/tests/snapshots/parser__print@struct-tuple.stef.snap b/crates/stef-parser/tests/snapshots/parser__print@struct-tuple.stef.snap new file mode 100644 index 0000000..b5a0274 --- /dev/null +++ b/crates/stef-parser/tests/snapshots/parser__print@struct-tuple.stef.snap @@ -0,0 +1,9 @@ +--- +source: crates/stef-parser/tests/parser.rs +expression: "Schema :: parse(input.as_str()).unwrap()" +input_file: crates/stef-parser/tests/inputs/struct-tuple.stef +--- +/// Basic struct. +struct Sample(u32 @1, bool @2) + + diff --git a/vscode-extension/syntaxes/stef.tmLanguage.yaml b/vscode-extension/syntaxes/stef.tmLanguage.yaml index a8c68fb..7e74e8f 100644 --- a/vscode-extension/syntaxes/stef.tmLanguage.yaml +++ b/vscode-extension/syntaxes/stef.tmLanguage.yaml @@ -5,9 +5,13 @@ fileTypes: [stef] patterns: - include: "#comments" + - include: "#attributes" - include: "#mod" - include: "#struct" - include: "#enum" + - include: "#const" + - include: "#use" + - include: "#alias" repository: comments: @@ -20,7 +24,7 @@ repository: patterns: - name: meta.preprocessor.stef begin: '#\[' - end: $\]\n? + end: \]$\n? mod: begin: (mod)\s+([a-z][a-z0-9_]*)\s*(?=\{) @@ -30,27 +34,45 @@ repository: end: (?<=\}) patterns: - include: "#comments" + - include: "#attributes" - include: "$self" - include: "#struct" - include: "#enum" + - include: "#const" + - include: "#use" + - include: "#alias" struct: - begin: (struct)\s+([A-Z][A-Za-z0-9_]*)\s*(<.+>)?\s*(?=\{) - beginCaptures: - "1": { name: keyword.declaration.struct.stef storage.type.struct.stef } - "2": { name: entity.name.class.struct.stef } - "3": { patterns: [include: "#generics"] } - end: (?<=\}) patterns: - - include: "#comments" - - include: "#fieldsNamed" - - include: "#fieldsUnnamed" + - begin: (struct)\s+([A-Z][A-Za-z0-9_]*)\s*(<.+>)?\s*(?=\{) + beginCaptures: + "1": { name: keyword.declaration.struct.stef storage.type.struct.stef } + "2": { name: entity.name.class.struct.named.stef } + "3": { patterns: [include: "#generics"] } + end: (?<=\}) + patterns: + - include: "#comments" + - include: "#fieldsNamed" + - begin: (struct)\s+([A-Z][A-Za-z0-9_]*)\s*(<.+>)?\s*(?=\() + beginCaptures: + "1": { name: keyword.declaration.struct.stef storage.type.struct.stef } + "2": { name: entity.name.class.struct.unnamed.stef } + "3": { patterns: [include: "#generics"] } + end: (?<=\)) + patterns: + - include: "#comments" + - include: "#fieldsUnnamed" + - match: (struct)\s+([A-Z][A-Za-z0-9_]*) + captures: + "1": { name: keyword.declaration.struct.stef storage.type.struct.stef } + "2": { name: entity.name.class.struct.unit.stef } enum: - begin: (enum)\s+([A-Z][A-Za-z0-9_]*)\s*(?=\{) + begin: (enum)\s+([A-Z][A-Za-z0-9_]*)\s*(<.+>)?\s*(?=\{) beginCaptures: "1": { name: keyword.declaration.enum.stef storage.type.enum.stef } "2": { name: entity.name.class.enum.stef } + "3": { patterns: [include: "#generics"] } end: (?<=\}) patterns: - include: "#comments" @@ -176,6 +198,37 @@ repository: match: non_zero(<.+>) captures: "1": { patterns: [include: "#generics"] } + - name: storage.type.external.stef + match: '[A-Za-z0-9_&<>\[\]\(\);, ]+' + + const: + patterns: + - match: (const)\s+([A-Z][A-Z_]+)\s*(:)\s*(.+)\s*=\s*(.+)(;) + captures: + "1": { name: keyword.declaration.const.stef } + "2": { name: entity.name.class.const.stef } + "3": { name: punctuation.colon.stef } + "4": { patterns: [include: "#type"] } + "5": { name: constant.numeric.stef } + "6": { name: punctuation.semicolon.stef } + + use: + patterns: + - match: (use)\s+([a-z][a-z0-9_]+(?:::[a-z][a-z0-9_]+))(.+)?(;) + captures: + "1": { name: keyword.declaration.use.stef } + "2": { name: entity.name.namespace.stef } + "3": { patterns: [include: "#type"] } + "4": { name: punctuation.semicolon.stef } + + alias: + patterns: + - match: (type)\s+(.+)\s*=\s*(.+)(;) + captures: + "1": { name: keyword.declaration.alias.stef } + "2": { patterns: [include: "#type"] } + "3": { patterns: [include: "#type"] } + "4": { name: punctuation.semicolon.stef } id: patterns: