Skip to content

Commit

Permalink
comments only supported at the start of a line
Browse files Browse the repository at this point in the history
  • Loading branch information
skanaar committed May 21, 2023
1 parent a35c43d commit 1dc59ba
Show file tree
Hide file tree
Showing 8 changed files with 53 additions and 46 deletions.
2 changes: 1 addition & 1 deletion .prettierignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
nomnoml.css
*.css
webapp/
64 changes: 31 additions & 33 deletions codemirror/nomnoml.codemirror-mode.js
Original file line number Diff line number Diff line change
@@ -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
},
}))
8 changes: 4 additions & 4 deletions codemirror/solarized.nomnoml.css
Original file line number Diff line number Diff line change
Expand Up @@ -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; }
Expand All @@ -47,17 +47,17 @@ 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; }

.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 }
Expand Down
2 changes: 1 addition & 1 deletion dist/nomnoml.js
Original file line number Diff line number Diff line change
Expand Up @@ -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: [] },
Expand Down
5 changes: 5 additions & 0 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,11 @@ <h2>Miscalleneous types</h2>
<pre append-nomnoml-preview>[&lt;pipe&gt; pipe]</pre>
<pre append-nomnoml-preview>[&lt;table&gt; table| a | 5 || b | 7]</pre>

<h2>Comments</h2>
Comments are supported at the start of a line
<pre append-nomnoml-preview>//[commented]
[not //commented]</pre>

<h2>Directives</h2>
#import: filename<br>
#arrowSize: 1<br>
Expand Down
2 changes: 1 addition & 1 deletion src/linearParse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
14 changes: 9 additions & 5 deletions test/test.comments.js
Original file line number Diff line number Diff line change
@@ -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]
Expand All @@ -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)
})
2 changes: 1 addition & 1 deletion test/test.parse-directives.js
Original file line number Diff line number Diff line change
@@ -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', () => {
Expand Down

0 comments on commit 1dc59ba

Please sign in to comment.