Verified Commit f550e9ca authored by Camil Staps's avatar Camil Staps 🚀

Make highlighting more modular using _prepend and _append magic goals; reset...

Make highlighting more modular using _prepend and _append magic goals; reset on layout-breaking newline
parent 5571fcf8
......@@ -2,6 +2,10 @@ function highlight(lex, istr, start) {
var out = [];
for (var group in lex) {
if ('_prepend' in lex && group[0] != '_')
lex[group] = lex['_prepend'].concat(lex[group]);
if ('_append' in lex && group[0] != '_')
lex[group] = lex[group].concat(lex['_append']);
for (var i in lex[group]) {
lex[group][i][0] = new RegExp(
/^/.source +
......@@ -47,6 +51,9 @@ function highlight(lex, istr, start) {
state = state_stack.pop();
} else if (new_state.substring(0,5) == 'jump:') {
state = new_state.substring(5);
} else if (new_state.substring(0,6) == 'reset:') {
state = new_state.substring(6);
state_stack = [];
} else {
state_stack.push(state);
state = new_state;
......@@ -101,11 +108,19 @@ function highlightClean(func, callback, start) {
};
return highlightToHTML({
_prepend: [
[/(\/\/.*)/, ['comment']],
[/(\/\*)/, ['comment'], 'comment'],
],
_append: [
[/([^\n\S]+)/, ['whitespace']],
[/(\n(?=\S))/, ['whitespace'], 'reset:start'],
[/(\n)/, ['whitespace']]
],
start: [
[/(\s+)/, ['whitespace']],
[/(generic)(\s)/,
['keyword', 'whitespace'], 'generic'],
[/(from(?: library)?|import(?: code)?|qualified|as|(?:definition|implementation|system)?|module)(\s)/,
[/(where|from(?: library)?|import(?: code)?|qualified|as|(?:(?:definition|implementation|system) )?module)(\s)/,
['keyword', 'whitespace']],
[/(instance)(\s)/, ['keyword', 'whitespace'], 'instance'],
[/(derive)(\s)/, ['keyword', 'whitespace'], 'derive'],
......@@ -122,8 +137,6 @@ function highlightClean(func, callback, start) {
['keyword', 'whitespace', 'keyword']],
[/(foreign export(?: (?:c|std)call)?)/,
['keyword']],
[/(\/\/.*)/, ['comment']],
[/(\/\*)/, ['comment'], 'comment'],
[/([\(\)])/, ['punctuation']],
[/(::)/, ['punctuation'], 'type'],
[/(=>)/, ['punctuation']],
......@@ -131,12 +144,11 @@ function highlightClean(func, callback, start) {
[/([^\(\)\s]+)/, ['funcname'], 'funcargs']
],
funcargs: [
[/(\s+)/, ['whitespace']],
[/(\/\/.*)/, ['comment']],
[/(\/\*)/, ['comment'], 'comment'],
[/((?::=)?=)/, ['punctuation'], 'pop;rhs'],
[/(\n)(?=[^\s\/])/, ['whitespace'], 'start'],
[/(\s)/, ['whitespace']],
[/((?::=)?=)/, ['punctuation'], 'jump:rhs'],
[/([:\[\](),]+)/, ['punctuation']],
[/(\|)/, ['punctuation'], 'pop;guard'],
[/(\|)/, ['punctuation'], 'jump:guard'],
[/([^:\[\](),\s]+)/, ['argument']]
],
comment: [
......@@ -146,50 +158,42 @@ function highlightClean(func, callback, start) {
[/(\*|\\)/, ['comment']]
],
startConstructor: [ // alternative entry point in case this is a constructor
[/(\s+)/, ['whitespace']],
[/(.*)(infix[lr]?)(\s*)(\d*)(\s*)(::)/,
['constructor', 'keyword', 'whitespace', 'keyword',
'whitespace', 'punctuation'], 'type'],
[/(.*)(::)/, ['constructor', 'punctuation'], 'type']
],
startRecordField: [ // alternative entry point in case this is a record field
[/(\s+)/, ['whitespace']],
[/(.*)(::)/, ['field', 'punctuation'], 'type']
],
generic: [
[/(\s+)/, ['whitespace']],
[/(class)(\s)/, ['keyword', 'whitespace'], 'genericClass'],
[/(\S+)/, ['funcname funcname-generic'], 'genericVars']
],
genericClass: [
[/(\s+)/, ['whitespace']],
[/(\S+)/, ['classname classname-generic'], 'genericVars']
],
genericVars: [
[/(\s+)/, ['whitespace']],
[/(::)/, ['punctuation'], 'type'],
[/(\S)/, ['typevar']]
],
type: [
[/(\n)/, ['whitespace'], 'pop'],
[/(\s)/, ['whitespace']],
[/(\/\/.*)/, ['comment']],
[/(where)/, ['keyword'], 'pop'],
[/([a-z][\w`]*)/, ['typevar']],
[/(A)(\.)/, ['existential', 'punctuation'], 'quantifiedVariables'],
[/([A-Z_]\w*)/, ['type']],
[/(\(\))/, ['type']],
[/(\|)/, ['punctuation'], 'context'],
[/(\W)/, ['punctuation']]
[/([^\s\w])/, ['punctuation']]
],
quantifiedVariables: [
[/(\s+)/, ['whitespace']],
[/(\*)/, ['punctuation']],
[/(:)/, ['punctuation'], 'pop'],
[/([a-z][\w`]*)/, ['typevar']]
],
context: [
[/(\s+)/, ['whitespace']],
[/(,)/, ['punctuation']],
[/(\[)/, ['punctuation'], 'attrenv'],
[/(\S+)(\{\|)/, ['generic', 'punctuation'], 'genericContext'],
......@@ -206,7 +210,6 @@ function highlightClean(func, callback, start) {
],
contextType: [
[/(\n)/, ['whitespace'], 'pop:2;macro'],
[/(\s+)/, ['whitespace']],
[/(,)/, ['punctuation']],
[/(&)/, ['punctuation'], 'pop'],
[/(\[)/, ['punctuation'], 'attrenv'],
......@@ -217,27 +220,23 @@ function highlightClean(func, callback, start) {
[/([^\s\(\)\[\],]+)/, ['typevar']]
],
attrenv: [
[/(\s+)/, ['whitespace']],
[/(\w)/, ['typevar']],
[/(<=)/, ['punctuation']],
[/(,)/, ['punctuation']],
[/(\])/, ['punctuation'], 'pop']
],
macro: [
[/(\s+)/, ['whitespace']],
[/(\/\/.*)/, ['comment']],
[/(\(.+\)\s+infix.*)/,
['__type__']],
[/([\w`]+\s*::.*)/,
['__type__']],
[/\b(where|with)\b/, ['keyword'], 'pop;macro'],
[/\b(where|with)\b/, ['keyword'], 'jump:macro'],
[/(special)(\s)/,['keyword', 'whitespace'], 'special'],
[/(\()(\S+)(\))/, ['punctuation', 'funcname', 'punctuation'], 'funcargs'],
[/(\S+)/, ['funcname'], 'funcargs']
],
guard: [
[/((?:^|\s)= )/, ['punctuation'], 'pop;rhs'],
[/(\s+)/, ['whitespace']],
[/((?:^|\s)= )/, ['punctuation'], 'jump:rhs'],
[/(otherwise)/, ['keyword']],
[/('(?:[^'\\]|\\(?:x[0-9a-fA-F]+|\d+|.))')/,
['literal literal-char']],
......@@ -255,13 +254,11 @@ function highlightClean(func, callback, start) {
[/([A-Z][\w`]*)/,['constructor']],
[/\b(_)\b/, ['argument argument-wildcard']],
[/([\w`]+)/, ['funcname funcname-onlyused']],
[/(.)/, ['punctuation']]
[/(\S)/, ['punctuation']]
],
rhs: [
[/(\n)(\|)/, ['whitespace', 'punctuation'], 'pop;guard'],
[/(\n)(\|)/, ['whitespace', 'punctuation'], 'jump:guard'],
[/(\n)/, ['whitespace'], 'pop'],
[/(\s+)/, ['whitespace']],
[/(\/\/.*)/, ['comment']],
[/(code\s+(?:inline\s*)?)({)/,
['keyword', 'punctuation'], 'abc'],
[/\b(if|let|in|with|case|of|otherwise)\b/,
......@@ -287,10 +284,9 @@ function highlightClean(func, callback, start) {
[/([A-Z][\w`]*)/,['constructor']],
[/\b(_)\b/, ['argument argument-wildcard']],
[/([\w`]+)/, ['funcname funcname-onlyused']],
[/(.)/, ['punctuation']]
[/(\S)/, ['punctuation']]
],
special: [
[/(\s+)/, ['whitespace']],
[/([a-z][\w`]*)/,['typevar']],
[/(=)/, ['punctuation'], 'type']
],
......@@ -300,28 +296,22 @@ function highlightClean(func, callback, start) {
[/([^\\"]+)/, ['literal literal-string']]
],
instance: [
[/(\s+)/, ['whitespace']],
[/(\S+)/, ['classname'], 'pop;macro;type'],
[/(\S+)/, ['classname'], 'jump:macro;type'],
],
className: [
[/(\s+)/, ['whitespace']],
[/(\S+)/, ['classname'], 'pop;classHeading']
[/(\S+)/, ['classname'], 'jump:classHeading']
],
classHeading: [
[/(\/\/.*)/, ['comment']],
[/(\s+)(where)/, ['whitespace', 'keyword']],
[/(\n)/, ['whitespace'], 'pop'],
[/(\s)/, ['whitespace']],
[/(class)/, ['keyword'], 'className'],
[/(where)/, ['keyword']],
[/(where)/, ['keyword'], 'pop'],
[/([a-z][\w`]*)/, ['typevar']],
[/(~)/, ['punctuation']],
[/(\|)/, ['punctuation'], 'pop;classContext'],
[/(::)/, ['punctuation'], 'pop;type']
[/(\|)/, ['punctuation'], 'jump:classContext'],
[/(::)/, ['punctuation'], 'jump:type']
],
classContext: [
[/(where)/, ['keyword'], 'pop'],
[/(\s+)/, ['whitespace']],
[/(,)/, ['punctuation']],
[/(\S+)(\{\|)/, ['generic', 'punctuation'], 'classGeneric'],
[/([^\s{]+)(,)/, ['classname', 'punctuation']],
......@@ -333,12 +323,10 @@ function highlightClean(func, callback, start) {
],
classContextType: [
[/(where)/, ['keyword'], 'pop:2'],
[/(\s+)/, ['whitespace']],
[/([,&])/, ['punctuation'], 'pop'],
[/([^\s,]+)/, ['typevar']]
],
derive: [
[/(\s+)/, ['whitespace']],
[/(\S+)/, ['generic'], 'type'],
],
abc: [
......@@ -350,13 +338,11 @@ function highlightClean(func, callback, start) {
],
abcInstruction: [
[/(\n)/, ['whitespace'], 'pop'],
[/(\s)/, ['whitespace']],
[/(\|.*\n)/, ['comment'], 'pop'],
[/(\S+)/, ['abc-instruction'], 'pop;abcArgument']
[/(\S+)/, ['abc-instruction'], 'jump:abcArgument']
],
abcArgument: [
[/(\n)/, ['whitespace'], 'pop'],
[/(\s)/, ['whitespace']],
[/(\|.*\n)/, ['comment'], 'pop'],
[/('(?:[^'\\]|\\(?:x[0-9a-fA-F]+|\d+|.))')/,
['literal literal-char']],
......@@ -373,91 +359,76 @@ function highlightClean(func, callback, start) {
],
abcDirective: [
[/(\n)/, ['whitespace'], 'pop'],
[/(\s)/, ['whitespace']],
[/(\|.*\n)/, ['comment'], 'pop'],
[/(\S+)/, ['abc-directive'], 'pop;abcArgument']
[/(\S+)/, ['abc-directive'], 'jump:abcArgument']
],
typeDefName: [
[/(\s+)/, ['whitespace']],
[/(\/\*)/, ['comment'], 'comment'],
[/(\*)/, ['punctuation']],
[/(\(?:==)/, ['punctuation'], 'pop;typeDefSynonym'],
[/(=:?)/, ['punctuation'], 'pop;typeDefRhs'],
[/(\(?:==)/, ['punctuation'], 'jump:typeDefSynonym'],
[/(=:?)/, ['punctuation'], 'jump:typeDefRhs'],
[/([A-Z_][\w`]*)/, ['type']],
[/(\[! \])/, ['type']], // Special cases for head-strict and tail-strict lists;
[/(\[ !\])/, ['type']], // we don't want to parse whitespace there
[/([~@#\$%\^\?!\+\-\*<>\\\/\|&=:\[\]{}(,)]+)/, ['type']],
[/([a-z_`][\w`]*)/, ['typevar'], 'pop;typeDefVars']
[/([a-z_`][\w`]*)/, ['typevar'], 'jump:typeDefVars']
],
typeDefVars: [
[/(\s+)/, ['whitespace']],
[/(\/\*)/, ['comment'], 'comment'],
[/([a-z][\w`]*)/, ['typevar']],
[/(\(?:==)/, ['punctuation'], 'pop;typeDefSynonym'],
[/(=:?)/, ['punctuation'], 'pop;typeDefRhs']
[/(\(?:==)/, ['punctuation'], 'jump:typeDefSynonym'],
[/(=:?)/, ['punctuation'], 'jump:typeDefRhs']
],
typeDefSynonym: [
[/(\s+)/, ['whitespace']],
[/([a-z][a-zA-Z]*)/, ['typevar']],
[/([A-Z_]\w*)/, ['type']],
[/(\W)/, ['punctuation']]
[/([^\w\s])/, ['punctuation']]
],
typeDefRhs: [
[/(\s*)(E)(\.)/, ['whitespace', 'existential', 'punctuation'], 'pop;typeDefRhsExi'],
[/(\s*)(\{)/, ['whitespace', 'punctuation'], 'pop;typeDefRecord'],
[/(\s*)/, ['whitespace'], 'pop;typeDefADT']
[/(\s*)(E)(\.)/, ['whitespace', 'existential', 'punctuation'], 'jump:typeDefRhsExi'],
[/(\s*)(\{)/, ['whitespace', 'punctuation'], 'jump:typeDefRecord'],
[/(\s*)/, ['whitespace'], 'jump:typeDefADT']
],
typeDefRhsExi: [
[/(\s+)/, ['whitespace']],
[/([a-z][\w`]*)/, ['typevar']],
[/(:)/, ['punctuation'], 'pop;typeDefRhs']
[/(:)/, ['punctuation'], 'jump:typeDefRhs']
],
typeDefRecord: [
[/(\s+)/, ['whitespace']],
[/([a-z_][\w`]*)(\s+)(::)/,
['field', 'whitespace', 'punctuation'], 'typeDefFieldType'],
[/(\})/, ['punctuation']]
],
typeDefFieldType: [
[/(\/\/.*)/, ['comment']],
[/(\s+)/, ['whitespace']],
[/([a-z]\w*)/, ['typevar']],
[/([A-Z_]\w*)/, ['type']],
[/(\()/, ['punctuation'], 'typeDefTuple'],
[/([\[\{])/, ['punctuation'], 'typeDefFieldType'],
[/([\]\},])/, ['punctuation'], 'pop'],
[/(\W)/, ['punctuation']]
[/([^\w\s])/, ['punctuation']]
],
typeDefTuple: [
[/(\s+)/, ['whitespace']],
[/([a-z]\w*)/, ['typevar']],
[/([A-Z_]\w*)/, ['type']],
[/([\(\[\{])/, ['punctuation'], 'typeDefTuple'],
[/([\)\]\}])/, ['punctuation'], 'pop'],
[/(\W)/, ['punctuation']]
[/([^\w\s])/, ['punctuation']]
],
typeDefADT: [
[/(\s+)/, ['whitespace']],
[/(E)(\.)/, ['existential', 'punctuation'], 'quantifiedVariables'],
[/([A-Z_][\w`]*)/, ['constructor'], 'typeDefConsArgs'],
[/(\([~@#\$%\^\?!\+\-\*<>\\\/\|&=:]*\))/, ['constructor'], 'typeDefConsArgs'],
[/(\.\.)/, ['punctuation']]
],
typeDefConsArgs: [
[/(\/\/.*)/, ['comment']],
[/(\n(?=\S))/, ['whitespace'], 'pop:2'],
[/(\s+)/, ['whitespace']],
[/(infix[lr]?)(\s*)(\d*)(\s+)/,
['keyword', 'whitespace', 'keyword', 'whitespace']],
[/([a-z][\w`]*)/, ['typevar']],
[/(A)(\.)/, ['existential', 'punctuation'], 'quantifiedVariables'],
[/([A-Z]\w*)/, ['type']],
[/(\|)/, ['punctuation'], 'pop'],
[/(&)/, ['punctuation'], 'pop;typeDefContext'],
[/(\W)/, ['punctuation']]
[/(&)/, ['punctuation'], 'jump:typeDefContext'],
[/([^\w\s])/, ['punctuation']]
],
typeDefContext: [
[/(\s+)/, ['whitespace']],
[/(,)/, ['punctuation']],
[/(\S+)(\{\|)/, ['generic', 'punctuation'], 'typeDefContextGeneric'],
[/([^\s{]+)(,)/, ['classname', 'punctuation']],
......@@ -465,10 +436,9 @@ function highlightClean(func, callback, start) {
],
typeDefContextGeneric: [
[/([*>-]+\|\},)/, ['punctuation'], 'pop'],
[/([*>-]+\|\})/, ['punctuation'], 'pop;typeDefContextType']
[/([*>-]+\|\})/, ['punctuation'], 'jump:typeDefContextType']
],
typeDefContextType: [
[/(\s+)/, ['whitespace']],
[/(&)/, ['punctuation'], 'pop'],
[/(\|)/, ['punctuation'], 'pop:2'],
[/([^\s,]+)/, ['typevar']]
......
-----
class Traversable t | Functor, Foldable t where
class Traversable t | Functor, Foldable t
where
traverse :: (a -> f b) !(t a) -> f (t b) | Applicative f
sequenceA :: !(t (f a)) -> f (t a) | Applicative f
mapM :: (a -> m b) !(t a) -> m (t b) | Monad m
......@@ -20,9 +20,9 @@ whitespace
classname Foldable
whitespace
typevar t
whitespace
whitespace \n
keyword where
whitespace \n\n
whitespace \n
funcname traverse
whitespace
punctuation ::
......
-----
definition module StdList // Exported definitions of list functions
-----
keyword definition
whitespace
keyword module
keyword definition module
whitespace
funcname StdList
whitespace
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment