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

Try to update syntax highlighting #59

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
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
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'],
Copy link
Contributor

Choose a reason for hiding this comment

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

Apparently this does not work (as before):

/*
 * Used as a type for `Exception` in functions which expect a non-empty
 * container (e.g. option or list).
 */
record MissingValue()

Here, the lines following beyond the */ are also greyed out

[/[\/*]/, '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'],
Copy link
Contributor

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