Skip to content

Commit

Permalink
Try to update syntax highlighting
Browse files Browse the repository at this point in the history
  • Loading branch information
jiribenes committed Jul 20, 2024
1 parent 2f66a31 commit a6eae04
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 55 deletions.
54 changes: 35 additions & 19 deletions src/effekt-syntax.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,48 +5,51 @@ import ITheme = monaco.editor.IStandaloneThemeData;

export const syntax = <ILanguage>{
// defaultToken: 'invalid',
tokenPostfix: '.effekt',

keywords: [
'module', 'import', 'def', 'val', 'var', 'effect', 'type', 'match',
'case', 'record', 'extern', 'include', 'resume', 'with', 'if', 'try',
'else', 'do', 'handle', 'while', 'fun', 'region', 'in', 'new',
'box', 'unbox', 'interface', 'resource', 'and', 'is', 'namespace'
'box', 'unbox', 'interface', 'resource', 'and', 'is', 'namespace',
'return', 'as'
],

definitionKeywords: [
'def', 'type', 'effect'
'def', 'type', 'effect', 'val', 'var', 'extern', 'fun', 'interface', 'resource', 'namespace'
],

literals: ['true', 'false'],

operators: [
'=', '>', '<', '!', '~', '?', ':', '==', '<=', '>=', '!=',
'&&', '||', '++', '--', '+', '-', '*', '/', '&', '|', '^', '%',
'<<', '>>', '>>>', '+=', '-=', '*=', '/=', '&=', '|=', '^=',
'%=', '<<=', '>>=', '>>>='
'=',
':',
'>', '<', '==', '<=', '>=', '!=',
'&&', '||',
'++',
'+', '-', '*', '/',
'=>', '::'
],

// we include these common regular expressions
symbols: /[=><!~?:&|+\-*\/\^%]+/,

// supported escapes
escapes: /\\(?:[btnfr\\"']|u[0-9A-Fa-f]{4})/,

// The main tokenizer for our languages
tokenizer: {
root: [
// identifiers and keywords
[/[a-z_$][\w$?!]*/, {
cases: {
'@keywords': {
cases: {
'@definitionKeywords': { token: 'keyword', next: '@definition' },
'@default': 'keyword'
}
},
'@definitionKeywords': { token: 'keyword', next: '@definition' },
'@keywords': 'keyword',
'@literals': 'literal',
'@default': 'identifier'
}
}],

[/[A-Z][\w\$?!]*/, 'type.identifier' ],
[/[A-Z][\w\$?!]*/, 'type.identifier'],

// whitespace
{ include: '@whitespace' },
Expand All @@ -55,7 +58,7 @@ export const syntax = <ILanguage>{
[/[{}()\[\]]/, '@brackets'],
[/[<>](?!@symbols)/, '@brackets'],
[/@symbols/, { cases: { '@operators': 'operator',
'@default' : '' } } ],
'@default': '' } } ],

// numbers
[/\d*\.\d+([eE][\-+]?\d+)?/, 'number.float'],
Expand All @@ -71,6 +74,7 @@ export const syntax = <ILanguage>{

// characters
[/'[^\\']'/, 'string'],
[/(')(@escapes)(')/, ['string', 'string.escape', 'string']],
[/'/, 'string.invalid']
],

Expand All @@ -81,13 +85,25 @@ export const syntax = <ILanguage>{
],

comment: [
[/[^\/*]+/, 'comment' ]
[/[^\/*]+/, 'comment'],
[/\/\*/, 'comment', '@push'],
["\\*/", 'comment', '@pop'],
[/[\/*]/, 'comment']
],

string: [
[/[^\\"]+/, 'string'],
[/\\./, 'string.escape.invalid'],
[/"/, { token: 'string.quote', bracket: '@close', next: '@pop' } ]
[/[^\\"$]+/, 'string'],
[/@escapes/, 'string.escape'],
[/\\./, 'string.escape.invalid'],
[/\$\{/, { token: 'delimiter.bracket', next: '@bracketCounting' }],
[/\$[a-z_][\w$]*/, 'variable'],
[/"/, { token: 'string.quote', bracket: '@close', next: '@pop' }]
],

bracketCounting: [
[/\{/, 'delimiter.bracket', '@bracketCounting'],
[/\}/, 'delimiter.bracket', '@pop'],
{ include: 'root' }
],

whitespace: [
Expand Down
40 changes: 4 additions & 36 deletions src/highlight-effekt.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,40 +66,9 @@ hljs.registerLanguage("effekt", function highlightEffekt(hljs) {
relevance: 0
};

var CLASS = {
className: 'class',
beginKeywords: 'class object trait type',
end: /[:={\[\n;]/,
excludeEnd: true,
contains: [
{
beginKeywords: 'extends with',
relevance: 10
},
{
begin: /\[/,
end: /\]/,
excludeBegin: true,
excludeEnd: true,
relevance: 0,
contains: [TYPE]
},
{
className: 'params',
begin: /\(/,
end: /\)/,
excludeBegin: true,
excludeEnd: true,
relevance: 0,
contains: [TYPE]
},
NAME
]
};

var METHOD = {
var DEFINITION = {
className: 'function',
beginKeywords: 'def',
beginKeywords: 'def effect type val var extern fun interface resource namespace',
end: /[:={\[(\n;]/,
excludeEnd: true,
contains: [NAME]
Expand All @@ -108,7 +77,7 @@ hljs.registerLanguage("effekt", function highlightEffekt(hljs) {
return {
name: 'Effekt',
keywords: {
literal: 'true false null',
literal: 'true false',
keyword: 'module effect type def with val var if for while import return else case try match resume do record region in new interface let box unbox fun extern and is namespace'
},
contains: [
Expand All @@ -117,8 +86,7 @@ hljs.registerLanguage("effekt", function highlightEffekt(hljs) {
STRING,
SYMBOL,
TYPE,
METHOD,
CLASS,
DEFINITION,
hljs.C_NUMBER_MODE,
ANNOTATION
]
Expand Down

0 comments on commit a6eae04

Please sign in to comment.