Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds Monarch Grammar File Generation #620

Merged
merged 7 commits into from
Sep 6, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions examples/statemachine/langium-config.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@
"fileExtensions": [".statemachine"],
"textMate": {
"out": "./syntaxes/statemachine.tmLanguage.json"
},
"monarch": {
"out": "./syntaxes/statemachine.monarch.ts"
}
}
]
Expand Down
30 changes: 30 additions & 0 deletions examples/statemachine/syntaxes/statemachine.monarch.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
// Monarch syntax highlighting for the statemachine language.
export default {
keywords: [
'actions','commands','end','events','initialState','state','statemachine'
],
operators: [
'=>'
],
symbols: /\{|\}|=>/,

tokenizer: {
initial: [
{ regex: /[_a-zA-Z][\w_]*/, action: { cases: { '@keywords': {"token":"keyword"}, '@default': {"token":"ID"} }} },
{ regex: /[0-9]+/, action: {"token":"number"} },
{ regex: /"[^"]*"|'[^']*'/, action: {"token":"string"} },
{ include: '@whitespace' },
{ regex: /@symbols/, action: { cases: { '@operators': {"token":"operator"}, '@default': {"token":""} }} },
],
whitespace: [
{ regex: /\s+/, action: {"token":"white"} },
{ regex: /\/\*/, action: {"token":"comment","next":"@comment"} },
{ regex: /\/\/[^\n\r]*/, action: {"token":"comment"} },
],
comment: [
{ regex: /[^\/\*]+/, action: {"token":"comment"} },
{ regex: /\*\//, action: {"token":"comment","next":"@pop"} },
{ regex: /[\/\*]/, action: {"token":"comment"} },
],
}
};
26 changes: 22 additions & 4 deletions packages/langium-cli/src/generate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ import { URI } from 'vscode-uri';
import { generateAst } from './generator/ast-generator';
import { serializeGrammar } from './generator/grammar-serializer';
import { generateModule } from './generator/module-generator';
import { generateTextMate } from './generator/textmate-generator';
import { generateTextMate } from './generator/highlighting/textmate-generator';
import { generateMonarch } from './generator/highlighting/monarch-generator';
import { getUserChoice, log } from './generator/util';
import { getFilePath, LangiumConfig, LangiumLanguageConfig, RelativePath } from './package';
import { validateParser } from './parser-validation';
Expand Down Expand Up @@ -190,15 +191,32 @@ export async function generate(config: LangiumConfig, options: GenerateOptions):
const genTmGrammar = generateTextMate(grammar, languageConfig);
const textMatePath = path.resolve(relPath, languageConfig.textMate.out);
log('log', options, `Writing textmate grammar to ${chalk.white.bold(textMatePath)}`);
const parentDir = path.dirname(textMatePath).split(path.sep).pop();
parentDir && await mkdirWithFail(parentDir, options);
await writeWithFail(textMatePath, genTmGrammar, options);
await writeHighlightGrammar(genTmGrammar, textMatePath, options);
}

if (languageConfig?.monarch) {
const genMonarchGrammar = generateMonarch(grammar, languageConfig);
const monarchPath = path.resolve(relPath, languageConfig.monarch.out);
log('log', options, `Writing monarch grammar to ${chalk.white.bold(monarchPath)}`);
await writeHighlightGrammar(genMonarchGrammar, monarchPath, options);
}
}

return 'success';
}

/**
* Writes contents of a grammar for syntax highlighting to a file, logging any errors and continuing without throwing
* @param grammar Grammar contents to write
* @param grammarPath Path to write, verifying the parent dir exists first
* @param options Generation options
*/
async function writeHighlightGrammar(grammar: string, grammarPath: string, options: GenerateOptions): Promise<void> {
const parentDir = path.dirname(grammarPath).split(path.sep).pop();
parentDir && await mkdirWithFail(parentDir, options);
await writeWithFail(grammarPath, grammar, options);
}

async function rmdirWithFail(dirPath: string, expectedFiles: string[], options: GenerateOptions): Promise<boolean> {
try {
let deleteDir = true;
Expand Down
Loading