From 1dc59ba6c20ed13577170c261ed5ee22b6626f64 Mon Sep 17 00:00:00 2001 From: skanaar Date: Sun, 21 May 2023 14:27:39 +0200 Subject: [PATCH] comments only supported at the start of a line --- .prettierignore | 2 +- codemirror/nomnoml.codemirror-mode.js | 64 +++++++++++++-------------- codemirror/solarized.nomnoml.css | 8 ++-- dist/nomnoml.js | 2 +- index.html | 5 +++ src/linearParse.ts | 2 +- test/test.comments.js | 14 +++--- test/test.parse-directives.js | 2 +- 8 files changed, 53 insertions(+), 46 deletions(-) diff --git a/.prettierignore b/.prettierignore index 7b89981..be51ae9 100644 --- a/.prettierignore +++ b/.prettierignore @@ -1,2 +1,2 @@ -nomnoml.css +*.css webapp/ \ No newline at end of file diff --git a/codemirror/nomnoml.codemirror-mode.js b/codemirror/nomnoml.codemirror-mode.js index 26d595a..f73af3b 100644 --- a/codemirror/nomnoml.codemirror-mode.js +++ b/codemirror/nomnoml.codemirror-mode.js @@ -1,38 +1,36 @@ -CodeMirror.defineMode('nomnoml', function() { - return { - startState: function() { return { inSymbol: false } }, - token: function(stream, state) { - if (stream.sol()){ - stream.eatSpace() - if (stream.peek() === '#'){ - stream.skipToEnd() - return 'meta' - } - if (stream.match('//')){ - stream.skipToEnd() - return 'comment' - } +CodeMirror.defineMode('nomnoml', () => ({ + startState() { + return {} + }, + token(stream, state) { + if (stream.sol()) { + stream.eatSpace() + if (stream.peek() === '#') { + stream.skipToEnd() + return 'meta' } + if (stream.match('//')) { + stream.skipToEnd() + return 'comment' + } + } - var delimiters = '[]|'.split('') - var operator = '>+-:;'.split('') - var all = [].concat(delimiters, operator) + var delimiters = '[]|;'.split('') + var operator = '<>()+-:'.split('') + var all = [...delimiters, ...operator] - if (stream.peek() === '<'){ - stream.eat('<') - if (stream.skipTo('>')) { - stream.eat('>') - return 'keyword' - } - return null + if (stream.peek() === '<') { + stream.eat('<') + if (stream.skipTo('>')) { + stream.eat('>') + return 'keyword' } - - if (delimiters.some(function (c){ return stream.eat(c) })) - return 'bracket' - if (operator.some(function (c){ return stream.eat(c) })) - return 'operator' - stream.eatWhile(function (c){ return all.indexOf(c) === -1 }) - return null; + return null } - }; -}); + + if (delimiters.some((c) => stream.eat(c))) return 'bracket' + if (operator.some((c) => stream.eat(c))) return 'operator' + stream.eatWhile((c) => all.indexOf(c) === -1) + return null + }, +})) diff --git a/codemirror/solarized.nomnoml.css b/codemirror/solarized.nomnoml.css index 3b61b2e..817f1a1 100644 --- a/codemirror/solarized.nomnoml.css +++ b/codemirror/solarized.nomnoml.css @@ -37,7 +37,7 @@ http://ethanschoonover.com/solarized text-shadow: none; } -.cm-s-solarized .cm-keyword { color: #93a1a1; font-style: italic } +.cm-s-solarized .cm-keyword { color: #b58900; } .cm-s-solarized .cm-atom { color: #d33682; } .cm-s-solarized .cm-number { color: #d33682; } .cm-s-solarized .cm-def { color: #2aa198; } @@ -47,9 +47,9 @@ http://ethanschoonover.com/solarized .cm-s-solarized .cm-variable-3 { color: #6c71c4; } .cm-s-solarized .cm-property { color: #2aa198; } -.cm-s-solarized .cm-operator {color: #657b83; } +.cm-s-solarized .cm-operator {color: #b58900; } -.cm-s-solarized .cm-comment { opacity: 0.5; } +.cm-s-solarized .cm-comment { color: #859900; opacity: 0.5; font-style: italic } .cm-s-solarized .cm-string { color: #859900; } .cm-s-solarized .cm-string-2 { color: #b58900; } @@ -57,7 +57,7 @@ http://ethanschoonover.com/solarized .cm-s-solarized .cm-meta { color: #b58900; } .cm-s-solarized .cm-qualifier { color: #b58900; } .cm-s-solarized .cm-builtin { color: #d33682; } -.cm-s-solarized .cm-bracket { color: #657b83; } +.cm-s-solarized .cm-bracket { color: #268bd2; } .cm-s-solarized .CodeMirror-matchingbracket { font-weight: bold; color: #000 !important } .cm-s-solarized .CodeMirror-nonmatchingbracket { color: #dc322f; } .cm-s-solarized .cm-tag { color: #93a1a1 } diff --git a/dist/nomnoml.js b/dist/nomnoml.js index 82e8d1f..6d46f81 100644 --- a/dist/nomnoml.js +++ b/dist/nomnoml.js @@ -673,7 +673,7 @@ let lineStartIndex = 0; let index = 0; const directives = extractDirectives(source); - source = source.replace(/\/\/[^\n]*/g, '').replace(/^#[^\n]*/g, ''); + source = source.replace(/^[ \t]*\/\/[^\n]*/gm, '').replace(/^#[^\n]*/gm, ''); if (source.trim() === '') return { root: { nodes: [], assocs: [], lines: [] }, diff --git a/index.html b/index.html index 224f1a8..e382a37 100644 --- a/index.html +++ b/index.html @@ -81,6 +81,11 @@

Miscalleneous types

[<pipe> pipe]
[<table> table| a | 5 || b | 7]
+

Comments

+ Comments are supported at the start of a line +
//[commented]
+[not //commented]
+

Directives

#import: filename
#arrowSize: 1
diff --git a/src/linearParse.ts b/src/linearParse.ts index 076076a..4aeb035 100644 --- a/src/linearParse.ts +++ b/src/linearParse.ts @@ -20,7 +20,7 @@ export function linearParse(source: string): Ast { const directives = extractDirectives(source) - source = source.replace(/\/\/[^\n]*/g, '').replace(/^#[^\n]*/g, '') + source = source.replace(/^[ \t]*\/\/[^\n]*/gm, '').replace(/^#[^\n]*/gm, '') if (source.trim() === '') return { diff --git a/test/test.comments.js b/test/test.comments.js index 40d1d58..e1392fd 100644 --- a/test/test.comments.js +++ b/test/test.comments.js @@ -1,7 +1,7 @@ var nomnoml = require('../dist/nomnoml.js') var { test } = require('node:test') var { deepEqual } = require('./assert.js') -var { part, node } = require('./utils.js') +var { part, node, assoc } = require('./utils.js') test('commented code', () => { const input = `[a] @@ -22,9 +22,13 @@ test('multiple comments code', () => { deepEqual(nomnoml.parse(input).root, expected) }) -test('trailing comments', () => { +test('trailing comments are not supported', () => { const input = `[a] -[b] // [foo]` - const expected = part({ nodes: [node('a'), node('b')] }) - deepEqual(nomnoml.parse(input).root, expected) +[b] // -> [foo]` + const expected = part({ + nodes: [node('a'), node('b'), node('foo')], + assocs: [assoc('b', '->', 'foo', { startLabel: { text: '//' } })], + }) + const actual = nomnoml.parse(input).root + deepEqual(actual, expected) }) diff --git a/test/test.parse-directives.js b/test/test.parse-directives.js index d8c1efe..15d3163 100644 --- a/test/test.parse-directives.js +++ b/test/test.parse-directives.js @@ -1,6 +1,6 @@ var nomnoml = require('../dist/nomnoml.js') var { test } = require('node:test') -var { assert, deepEqual } = require('./assert.js') +var { deepEqual } = require('./assert.js') var { part, node, dir } = require('./utils.js') test('single directive', () => {