Skip to content
Open
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
64 changes: 44 additions & 20 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,9 @@ export const syntax = <ILanguage>{
[/[{}()\[\]]/, '@brackets'],
[/[<>](?!@symbols)/, '@brackets'],
[/@symbols/, { cases: { '@operators': 'operator',
'@default' : '' } } ],
'@default': '' } } ],
// strings
[/"/, { token: 'string.quote', bracket: '@open', next: '@string' }],

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

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

Expand All @@ -80,14 +86,32 @@ export const syntax = <ILanguage>{
[new RegExp(""),'','@pop']
],

string: [
[/[^\\"$]+/, 'string'],
[/@escapes/, 'string.escape'],
[/\\./, 'string.escape.invalid'],
[/\$\{/, { token: 'string.escape', next: '@stringInterpolation' }],
[/\$/, 'string'],
[/"/, { token: 'string.quote', bracket: '@close', next: '@pop' }]
],

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

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

string: [
[/[^\\"]+/, 'string'],
[/\\./, 'string.escape.invalid'],
[/"/, { token: 'string.quote', bracket: '@close', next: '@pop' } ]
bracketCounting: [
[/\{/, 'delimiter.bracket', '@bracketCounting'],
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This does seem to get confused on this example:

extern io def panic[R](msg: String): R =
  js "${ msg ++ "{42}"}"

Here, the 42 is coloured like a literal (blue) while it should coloured like a string (green)

[/\}/, '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;]/,
Comment on lines -69 to -72
Copy link
Contributor Author

@jiribenes jiribenes Jul 20, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think we really used this anywhere except for type declarations so I merged the important parts with METHOD to make DEFINITION.

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