From a4dc5b56e589d74c69d5cd21772ba345652a62df Mon Sep 17 00:00:00 2001 From: Sung-Shik Jongmans Date: Wed, 13 Nov 2024 16:37:40 +0100 Subject: [PATCH 1/5] Move main modules to `lang::textmate::main::...` --- .../main/rascal/{ => lang/textmate/main}/Main.rsc | 4 ++-- .../main/rascal/{ => lang/textmate/main}/VSCode.rsc | 12 ++++++------ .../rascal/{ => lang/textmate/main}/VSCodePico.rsc | 7 ++++--- .../rascal/{ => lang/textmate/main}/VSCodeRascal.rsc | 12 +++++++----- 4 files changed, 19 insertions(+), 16 deletions(-) rename rascal-textmate-core/src/main/rascal/{ => lang/textmate/main}/Main.rsc (98%) rename rascal-textmate-core/src/main/rascal/{ => lang/textmate/main}/VSCode.rsc (87%) rename rascal-textmate-core/src/main/rascal/{ => lang/textmate/main}/VSCodePico.rsc (87%) rename rascal-textmate-core/src/main/rascal/{ => lang/textmate/main}/VSCodeRascal.rsc (93%) diff --git a/rascal-textmate-core/src/main/rascal/Main.rsc b/rascal-textmate-core/src/main/rascal/lang/textmate/main/Main.rsc similarity index 98% rename from rascal-textmate-core/src/main/rascal/Main.rsc rename to rascal-textmate-core/src/main/rascal/lang/textmate/main/Main.rsc index bc2aab0..0f7af4c 100644 --- a/rascal-textmate-core/src/main/rascal/Main.rsc +++ b/rascal-textmate-core/src/main/rascal/lang/textmate/main/Main.rsc @@ -28,7 +28,7 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. Main functions to generate TextMate grammars for Rascal grammars } -module Main +module lang::textmate::main::Main import Grammar; import lang::textmate::Conversion; @@ -44,4 +44,4 @@ int main(RscGrammar rsc, str scopeName, loc l) { TmGrammar tm = toTmGrammar(rsc, scopeName, nameGeneration = short()); toJSON(tm, indent = 2, l = l); return 0; -} \ No newline at end of file +} diff --git a/rascal-textmate-core/src/main/rascal/VSCode.rsc b/rascal-textmate-core/src/main/rascal/lang/textmate/main/VSCode.rsc similarity index 87% rename from rascal-textmate-core/src/main/rascal/VSCode.rsc rename to rascal-textmate-core/src/main/rascal/lang/textmate/main/VSCode.rsc index 0b5f923..0e9caf9 100644 --- a/rascal-textmate-core/src/main/rascal/VSCode.rsc +++ b/rascal-textmate-core/src/main/rascal/lang/textmate/main/VSCode.rsc @@ -29,13 +29,13 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. used in the special VS Code extension } -module VSCode +module lang::textmate::main::VSCode -import VSCodePico; -import VSCodeRascal; +import lang::textmate::main::VSCodePico; +import lang::textmate::main::VSCodeRascal; int main() { - VSCodePico::main(); - VSCodeRascal::main(); + lang::textmate::main::VSCodePico::main(); + lang::textmate::main::VSCodeRascal::main(); return 0; -} \ No newline at end of file +} diff --git a/rascal-textmate-core/src/main/rascal/VSCodePico.rsc b/rascal-textmate-core/src/main/rascal/lang/textmate/main/VSCodePico.rsc similarity index 87% rename from rascal-textmate-core/src/main/rascal/VSCodePico.rsc rename to rascal-textmate-core/src/main/rascal/lang/textmate/main/VSCodePico.rsc index fbf6b8d..dac6c9a 100644 --- a/rascal-textmate-core/src/main/rascal/VSCodePico.rsc +++ b/rascal-textmate-core/src/main/rascal/lang/textmate/main/VSCodePico.rsc @@ -29,9 +29,10 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. special VS Code extension } -module VSCodePico +module lang::textmate::main::VSCodePico -import Main; +import lang::textmate::main::Main; extend lang::textmate::conversiontests::PicoWithCategories; -int main() = Main::main(rsc, "source.pico", |project://vscode-extension/syntaxes/pico.tmLanguage.json|); \ No newline at end of file +int main() = lang::textmate::main::Main::main( + rsc, "source.pico", |project://vscode-extension/syntaxes/pico.tmLanguage.json|); diff --git a/rascal-textmate-core/src/main/rascal/VSCodeRascal.rsc b/rascal-textmate-core/src/main/rascal/lang/textmate/main/VSCodeRascal.rsc similarity index 93% rename from rascal-textmate-core/src/main/rascal/VSCodeRascal.rsc rename to rascal-textmate-core/src/main/rascal/lang/textmate/main/VSCodeRascal.rsc index 1d7ce96..2e94e69 100644 --- a/rascal-textmate-core/src/main/rascal/VSCodeRascal.rsc +++ b/rascal-textmate-core/src/main/rascal/lang/textmate/main/VSCodeRascal.rsc @@ -29,13 +29,15 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. the special VS Code extension } -module VSCodeRascal +module lang::textmate::main::VSCodeRascal import Grammar; -import Main; +import lang::textmate::main::Main; extend lang::textmate::conversiontests::Rascal; -int main() = Main::main(getRscGrammar(), "source.rascalmpl", |project://vscode-extension/syntaxes/rascal.tmLanguage.json|); +int main() = lang::textmate::main::Main::main( + getRscGrammar(), "source.rascalmpl", + |project://vscode-extension/syntaxes/rascal.tmLanguage.json|); // Relevant comment regarding grammar precedence in VS Code: // - https://github.com/microsoft/vscode-docs/issues/2862#issuecomment-599994967 @@ -45,7 +47,7 @@ private Grammar getRscGrammar() { = {\tag("category"(_)), *rest} := attributes ? p[attributes = rest + \tag("category"(category))] : p[attributes = attributes + \tag("category"(category))]; - + return visit (rsc) { // Temporarily hot-patch Rascal's own grammar as discussed here: // - https://github.com/SWAT-engineering/rascal-textmate/pull/6 @@ -60,4 +62,4 @@ private Grammar getRscGrammar() { case p: prod(lex("MidStringChars"), _, _) => setCategory(p, "string.quoted.double") case p: prod(lex("PostStringChars"), _, _) => setCategory(p, "string.quoted.double") }; -} \ No newline at end of file +} From 0786737edb51fbb927a5e86ea5d456ba37b74c9b Mon Sep 17 00:00:00 2001 From: Sung-Shik Jongmans Date: Fri, 15 Nov 2024 11:20:56 +0100 Subject: [PATCH 2/5] Update Rascal version --- rascal-textmate-core/pom.xml | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/rascal-textmate-core/pom.xml b/rascal-textmate-core/pom.xml index a1b2eaf..6b85a1e 100644 --- a/rascal-textmate-core/pom.xml +++ b/rascal-textmate-core/pom.xml @@ -64,7 +64,7 @@ org.rascalmpl rascal - 0.40.4 + 0.40.17 @@ -77,6 +77,19 @@ + + + . + false + + META-INF/RASCAL.MF + + + + src/main/rascal + false + + org.apache.maven.plugins From 26a570297a4439febb8328b02bfba8a4a13a2721 Mon Sep 17 00:00:00 2001 From: Sung-Shik Jongmans Date: Fri, 15 Nov 2024 11:21:41 +0100 Subject: [PATCH 3/5] Fix a small bug that caused dynamic dispatch to pick the wrong function on an empty list --- .../src/main/rascal/lang/textmate/Conversion.rsc | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/rascal-textmate-core/src/main/rascal/lang/textmate/Conversion.rsc b/rascal-textmate-core/src/main/rascal/lang/textmate/Conversion.rsc index ac9f8e6..14b6242 100644 --- a/rascal-textmate-core/src/main/rascal/lang/textmate/Conversion.rsc +++ b/rascal-textmate-core/src/main/rascal/lang/textmate/Conversion.rsc @@ -227,10 +227,10 @@ list[ConversionUnit] analyze(RscGrammar rsc, str name) { // Analyze delimiters jobStep(jobLabel, "Analyzing delimiters"); - set[Symbol] delimiters - = removeStrictPrefixes({s | /Symbol s := rsc, isDelimiter(delabel(s))}) - - {s | p <- prods, /just(s) := getOuterDelimiterPair(rsc, p)} - - {s | p <- prods, /just(s) := getInnerDelimiterPair(rsc, p, getOnlyFirst = true)}; + set[Symbol] delimiters = {s | /Symbol s := rsc, isDelimiter(delabel(s))}; + delimiters &= removeStrictPrefixes(delimiters); + delimiters -= {s | p <- prods, /just(s) := getOuterDelimiterPair(rsc, p)}; + delimiters -= {s | p <- prods, /just(s) := getInnerDelimiterPair(rsc, p, getOnlyFirst = true)}; list[Production] prodsDelimiters = [prod(lex(DELIMITERS_PRODUCTION_NAME), [\alt(delimiters)], {})]; // Analyze keywords @@ -552,4 +552,4 @@ private TmRule toTmRule(RegExp begin, RegExp end, list[TmRule] patterns) end.string, beginCaptures = toCaptures(begin.categories), endCaptures = toCaptures(end.categories), - patterns = patterns); \ No newline at end of file + patterns = patterns); From 6960967728bcad24ce56b94d1e6451a388d8be1a Mon Sep 17 00:00:00 2001 From: Sung-Shik Jongmans Date: Fri, 15 Nov 2024 11:21:47 +0100 Subject: [PATCH 4/5] Update README --- README.md | 52 +++++++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 49 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 5d66b6d..77914f4 100644 --- a/README.md +++ b/README.md @@ -1,5 +1,7 @@ # rascal-textmate +## Background + The aim of this project is to design and implement a scalable converter from Rascal grammars to TextMate grammars. TextMate grammars generated in this way are intended to be included in VS Code extensions to provide syntax highlighting @@ -16,12 +18,56 @@ TextMate grammars, this project applies partial conversion. Alternatively, a previous [project](https://github.com/TarVK/syntax-highlighter) by [@TarVK](https://github.com/TarVK) applies total conversion. -## Documentation +## Usage + +### Installing `rascal-textmate` + +Enter the following commands in a terminal: + +``` +git clone https://github.com/SWAT-engineering/rascal-textmate.git +cd rascal-textmate/rascal-textmate-core +mvn install -Drascal.compile.skip -Drascal.tutor.skip -DskipTests +cd ../.. +rm -rf rascal-textmate +``` + +### Running `rascal-textmate` in an existing Rascal project + + 1. Add the following dependency in `pom.xml` of the project: + + ``` + + org.rascalmpl + rascal-textmate-core + 0.1.0-SNAPSHOT + + ``` + + 2. Add `|lib://rascal-textmate-core|` to `Require-Libraries` in + `META-INF/RASCAL.MF` of the project. + + 3. Open a REPL in a grammar module of the project, import module + `lang::textmate::main::Main` in the REPL, and run function + [`main`](https://github.com/SWAT-engineering/rascal-textmate/blob/69bd92c1e39b51c78ad6d49e680bba212a8df2a7/rascal-textmate-core/src/main/rascal/Main.rsc#L38-L47) + on the start symbol of the grammar. For instance: + + ``` + main(#Foo, "source.foo", |home:///Desktop/foo.json|) + ``` + + The generated TextMate grammar (as a JSON file) can now be integrated in a + VS Code extension as explained + [here](https://code.visualstudio.com/api/language-extensions/syntax-highlight-guide#contributing-a-basic-grammar). + +## Contributing + +### Documentation The [walkthrough](rascal-textmate-core/src/main/rascal/lang/textmate/conversiontests/Walkthrough.rsc) -explains the main ideas behind the conversion algorithm in this project. +explains the main ideas behind the conversion algorithm. -## Tests +### Tests To test tokenization (as part of the conversion [tests](rascal-textmate-core/src/main/rascal/lang/textmate/conversiontests)), From 7aaa5819022999b3fd42dfa6f3a7cbd22763fba6 Mon Sep 17 00:00:00 2001 From: Sung-Shik Jongmans Date: Fri, 15 Nov 2024 11:50:46 +0100 Subject: [PATCH 5/5] Add languages to code blocks in README --- README.md | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 77914f4..4cfd928 100644 --- a/README.md +++ b/README.md @@ -24,7 +24,7 @@ previous [project](https://github.com/TarVK/syntax-highlighter) by Enter the following commands in a terminal: -``` +```shell git clone https://github.com/SWAT-engineering/rascal-textmate.git cd rascal-textmate/rascal-textmate-core mvn install -Drascal.compile.skip -Drascal.tutor.skip -DskipTests @@ -36,7 +36,7 @@ rm -rf rascal-textmate 1. Add the following dependency in `pom.xml` of the project: - ``` + ```xml org.rascalmpl rascal-textmate-core @@ -50,9 +50,9 @@ rm -rf rascal-textmate 3. Open a REPL in a grammar module of the project, import module `lang::textmate::main::Main` in the REPL, and run function [`main`](https://github.com/SWAT-engineering/rascal-textmate/blob/69bd92c1e39b51c78ad6d49e680bba212a8df2a7/rascal-textmate-core/src/main/rascal/Main.rsc#L38-L47) - on the start symbol of the grammar. For instance: + on the `start` symbol of the grammar. For instance: - ``` + ```rascal main(#Foo, "source.foo", |home:///Desktop/foo.json|) ```