Skip to content
This repository has been archived by the owner on Nov 5, 2021. It is now read-only.

YAML colorization #12

Merged
merged 4 commits into from
Oct 5, 2016
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: 2 additions & 1 deletion gulpfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,8 @@ gulp.task('release', ['clean-release','compile'], function() {
bundleOne('src/sql'),
bundleOne('src/swift'),
bundleOne('src/vb'),
bundleOne('src/xml')
bundleOne('src/xml'),
bundleOne('src/yaml')
)
.pipe(uglify({
preserveComments: 'some'
Expand Down
7 changes: 7 additions & 0 deletions src/monaco.contribution.ts
Original file line number Diff line number Diff line change
Expand Up @@ -247,3 +247,10 @@ registerLanguage({
mimetypes: ['text/css'],
module: './css'
});
registerLanguage({
id: 'yaml',
extensions: ['.yaml', '.yml'],
aliases: ['YAML', 'yaml', 'YML', 'yml'],
mimetypes: ['application/x-yaml'],
module: './yaml'
});
193 changes: 193 additions & 0 deletions src/yaml.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,193 @@
import IRichLanguageConfiguration = monaco.languages.LanguageConfiguration;
import ILanguage = monaco.languages.IMonarchLanguage;

export const conf: IRichLanguageConfiguration = {
comments: {
lineComment: '#'
},
brackets: [['{', '}'], ['[', ']']],
autoClosingPairs: [
{ open: '"', close: '"', notIn: ['string', 'comment'] },
{ open: '\'', close: '\'', notIn: ['string', 'comment'] },
{ open: '{', close: '}', notIn: ['string', 'comment'] },
{ open: '[', close: ']', notIn: ['string', 'comment'] }
]
};

export const language = <ILanguage> {
tokenPostfix: '.yaml',

brackets: [
{ token: 'delimiter.bracket', open: '{', close: '}' },
{ token: 'delimiter.square', open: '[', close: ']' }
],

keywords: ['true', 'True', 'TRUE', 'false', 'False', 'FALSE', 'null', 'Null', 'Null', '~'],

numberInteger: /(?:0|[+-]?[0-9]+)/,
numberFloat: /(?:0|[+-]?[0-9]+)(?:\.[0-9]+)?(?:e[-+][1-9][0-9]*)?/,
numberOctal: /0o[0-7]+/,
numberHex: /0x[0-9a-fA-F]+/,
numberInfinity: /[+-]?\.(?:inf|Inf|INF)/,
numberNaN: /\.(?:nan|Nan|NAN)/,
numberDate: /\d{4}-\d\d-\d\d([Tt ]\d\d:\d\d:\d\d(\.\d+)?(( ?[+-]\d\d?(:\d\d)?)|Z)?)?/,

escapes: /\\(?:[btnfr\\"']|[0-7][0-7]?|[0-3][0-7]{2})/,

tokenizer: {
root: [
{include: '@whitespace'},
{include: '@comment'},

// Directive
[/%[^ ]+.*$/, 'meta.directive'],

// Document Markers
[/---/, 'operators.directivesEnd'],
[/\.{3}/, 'operators.documentEnd'],

// Block Structure Indicators
[/[-?:](?= )/, 'operators'],

{include: '@anchor'},
{include: '@tagHandle'},
{include: '@flowCollections'},
{include: '@blockStyle'},

// Numbers
[/@numberInteger(?![ \t]*\S+)/, 'number'],
[/@numberFloat(?![ \t]*\S+)/, 'number.float'],
[/@numberOctal(?![ \t]*\S+)/, 'number.octal'],
[/@numberHex(?![ \t]*\S+)/, 'number.hex'],
[/@numberInfinity(?![ \t]*\S+)/, 'number.infinity'],
[/@numberNaN(?![ \t]*\S+)/, 'number.nan'],
[/@numberDate(?![ \t]*\S+)/, 'number.date'],

// Key:Value pair
[/(".*?"|'.*?'|.*?)([ \t]*)(:)( |$)/, ['type', 'white', 'operators', 'white']],

{include: '@flowScalars'},

// String nodes
[/.+$/, {cases: {'@keywords': 'keyword', '@default': 'string'}}]
],

// Flow Collection: Flow Mapping
object: [
{include: '@whitespace'},
{include: '@comment'},

// Flow Mapping termination
[/\}/, '@brackets', '@pop'],

// Flow Mapping delimiter
[/,/, 'delimiter.comma'],

// Flow Mapping Key:Value delimiter
[/:(?= )/, 'operators'],

// Flow Mapping Key:Value key
[/(?:".*?"|'.*?'|[^,\{\[]+?)(?=: )/, 'type'],

// Start Flow Style
{include: '@flowCollections'},
{include: '@flowScalars'},

// Scalar Data types
{include: '@tagHandle'},
{include: '@anchor'},
{include: '@flowNumber'},

// Other value (keyword or string)
[/[^\},]+/, {cases: {'@keywords': 'keyword', '@default': 'string'}}]
],

// Flow Collection: Flow Sequence
array: [
{include: '@whitespace'},
{include: '@comment'},

// Flow Sequence termination
[/\]/, '@brackets', '@pop'],

// Flow Sequence delimiter
[/,/, 'delimiter.comma'],

// Start Flow Style
{include: '@flowCollections'},
{include: '@flowScalars'},

// Scalar Data types
{include: '@tagHandle'},
{include: '@anchor'},
{include: '@flowNumber'},

// Other value (keyword or string)
[/[^\],]+/, {cases: {'@keywords': 'keyword', '@default': 'string'}}]
],

// Flow Scalars (quoted strings)
string: [
[/[^\\"']+/, 'string'],
[/@escapes/, 'string.escape'],
[/\\./, 'string.escape.invalid'],
[/["']/, {cases: {'$#==$S2': {token: 'string', next: '@pop'}, '@default': 'string'}}]
],

// First line of a Block Style
multiString: [
[/^( +).+$/, 'string', '@multiStringContinued.$1']
],

// Further lines of a Block Style
// Workaround for indentation detection
multiStringContinued: [
[/^( *).+$/, {cases: {'$1==$S2': 'string', '@default': {token: '@rematch', next: '@popall'}}}]
],

whitespace: [
[/[ \t\r\n]+/, 'white']
],

// Only line comments
comment: [
[/#.*$/, 'comment']
],

// Start Flow Collections
flowCollections: [
[/\[/, '@brackets', '@array'],
[/\{/, '@brackets', '@object']
],

// Start Flow Scalars (quoted strings)
flowScalars: [
[/"/, 'string', '@string."'],
[/'/, 'string', '@string.\'']
],

// Start Block Scalar
blockStyle: [
[/[>|][0-9]*[+-]?$/, 'operators', '@multiString']
],

// Numbers in Flow Collections (terminate with ,]})
flowNumber: [
[/@numberInteger(?=[ \t]*[,\]\}])/, 'number'],
[/@numberFloat(?=[ \t]*[,\]\}])/, 'number.float'],
[/@numberOctal(?=[ \t]*[,\]\}])/, 'number.octal'],
[/@numberHex(?=[ \t]*[,\]\}])/, 'number.hex'],
[/@numberInfinity(?=[ \t]*[,\]\}])/, 'number.infinity'],
[/@numberNaN(?=[ \t]*[,\]\}])/, 'number.nan'],
[/@numberDate(?=[ \t]*[,\]\}])/, 'number.date']
],

tagHandle: [
[/\![^ ]*/, 'tag']
],

anchor: [
[/[&*][^ ]+/, 'namespace']
]
}
};
1 change: 1 addition & 0 deletions test/all.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ requirejs([
'out/test/sql.test',
'out/test/vb.test',
'out/test/xml.test',
'out/test/yaml.test'
], function() {
run(); // We can launch the tests!
});
Expand Down
Loading