Add rudimentary Twig support

This commit is contained in:
Marco Petersen 2019-09-10 10:45:32 +02:00
parent d511bfdcc7
commit 532285bcf5
7 changed files with 159 additions and 0 deletions

View file

@ -43,6 +43,7 @@ Colorization and configuration supports for multiple languages for the Monaco Ed
* sql * sql
* st * st
* swift * swift
* twig
* typescript * typescript
* vb * vb
* xml * xml

View file

@ -75,6 +75,7 @@ bundleOne('azcli/azcli');
bundleOne('apex/apex'); bundleOne('apex/apex');
bundleOne('tcl/tcl'); bundleOne('tcl/tcl');
bundleOne('graphql/graphql'); bundleOne('graphql/graphql');
bundleOne('twig/twig');
function bundleOne(moduleId, exclude) { function bundleOne(moduleId, exclude) {
requirejs.optimize({ requirejs.optimize({

View file

@ -48,6 +48,7 @@ import './sql/sql.contribution';
import './st/st.contribution'; import './st/st.contribution';
import './swift/swift.contribution'; import './swift/swift.contribution';
import './tcl/tcl.contribution'; import './tcl/tcl.contribution';
import './twig/twig.contribution';
import './typescript/typescript.contribution'; import './typescript/typescript.contribution';
import './vb/vb.contribution'; import './vb/vb.contribution';
import './xml/xml.contribution'; import './xml/xml.contribution';

View file

@ -0,0 +1,15 @@
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
'use strict';
import { registerLanguage } from '../_.contribution';
registerLanguage({
id: 'twig',
extensions: ['.twig'],
aliases: ['Twig', 'twig'],
mimetypes: ['text/x-twig'],
loader: () => import('./twig')
});

0
src/twig/twig.test.ts Normal file
View file

140
src/twig/twig.ts Normal file
View file

@ -0,0 +1,140 @@
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
'use strict';
import IRichLanguageConfiguration = monaco.languages.LanguageConfiguration;
import ILanguage = monaco.languages.IMonarchLanguage;
export const conf: IRichLanguageConfiguration = {
};
export const language = <ILanguage>{
defaultToken: 'invalid',
keywords: [
// (opening) tags
'apply', 'autoescape', 'block', 'deprecated', 'do', 'embed', 'extends',
'flush', 'for', 'from', 'if', 'import', 'include', 'macro', 'sandbox',
'set', 'use', 'verbatim', 'with',
// closing tags
'endapply', 'endautoescape', 'endblock', 'endembed', 'endfor', 'endif',
'endmacro', 'endsandbox', 'endset', 'endwith',
],
tokenizer: {
root: [
[/{#/, 'comment.twig', '@commentState'],
[/{%[-~]?/, 'delimiter.twig', '@blockState'],
[/{{[-~]?/, 'delimiter.twig', '@variableState'],
],
/**
* Comment Tag Handling
*/
commentState: [
[/#}/, 'comment.twig', '@pop'],
[/./, 'comment.twig'],
],
/**
* Block Tag Handling
*/
blockState: [
[/[-~]?%}/, 'delimiter.twig', '@pop'],
// whitespace
[/\s+/],
// verbatim
// Unlike other blocks, verbatim ehas its own state
// transition to ensure we mark its contents as strings.
[/(verbatim)(\s*)([-~]?%})/, [
'keyword',
'',
{ token: 'delimiter.twig', next: '@rawDataState' },
]],
{ include: 'expression' }
],
rawDataState: [
// endverbatim
[/({%[-~]?)(\s*)(endverbatim)(\s*)([-~]?%})/, [
'delimiter.twig',
'',
'keyword',
'',
{ token: 'delimiter.twig', next: '@popall' },
]],
[/./, 'string'],
],
/**
* Variable Tag Handling
*/
variableState: [
[/[-~]?}}/, 'delimiter.twig', '@pop'],
{ include: 'expression' },
],
stringState: [
// closing double quoted string
[/"/, 'string.twig', '@pop'],
// interpolation start
[/#{\s*/, 'string.twig', '@interpolationState'],
// string part
[/[^#"\\]*(?:(?:\\.|#(?!\{))[^#"\\]*)*/, 'string.twig'],
],
interpolationState: [
// interpolation end
[/}/, 'string.twig', '@pop'],
{ include: 'expression' },
],
/**
* Expression Handling
*/
expression: [
// whitespace
[/\s+/],
// operators - math
[/\+|-|\/{1,2}|%|\*{1,2}/, 'operators.twig'],
// operators - logic
[/(and|or|not|b-and|b-xor|b-or)(\s+)/, ['operators.twig', '']],
// operators - comparison (symbols)
[/==|!=|<|>|>=|<=/, 'operators.twig'],
// operators - comparison (words)
[/(starts with|ends with|matches)(\s+)/, ['operators.twig', '']],
// operators - containment
[/(in)(\s+)/, ['operators.twig', '']],
// operators - test
[/(is)(\s+)/, ['operators.twig', '']],
// operators - misc
[/\||~|:|\.{1,2}|\?{1,2}/, 'operators.twig'],
// names
[/[^\W\d][\w]*/, {
cases: {
'@keywords': 'keyword.twig',
'@default': 'variable.twig'
}
}],
// numbers
[/\d+(\.\d+)?/, 'number.twig'],
// punctuation
[/\(|\)|\[|\]|{|}|,/, 'delimiter.twig'],
// strings
[/"([^#"\\]*(?:\\.[^#"\\]*)*)"|\'([^\'\\]*(?:\\.[^\'\\]*)*)\'/, 'string.twig'],
// opening double quoted string
[/"/, 'string.twig', '@stringState'],
// misc syntactic constructs
// These are not operators per se, but for the purposes of lexical analysis we
// can treat them as such.
// arrow functions
[/=>/, 'operators.twig'],
// assignment
[/=/, 'operators.twig'],
],
}
};

View file

@ -75,6 +75,7 @@ define(['require'], function () {
'release/dev/st/st.test', 'release/dev/st/st.test',
'release/dev/swift/swift.test', 'release/dev/swift/swift.test',
'release/dev/tcl/tcl.test', 'release/dev/tcl/tcl.test',
'release/dev/twig/twig.test',
'release/dev/typescript/typescript.test', 'release/dev/typescript/typescript.test',
'release/dev/vb/vb.test', 'release/dev/vb/vb.test',
'release/dev/xml/xml.test', 'release/dev/xml/xml.test',