Skip to content

Commit

Permalink
refactoring, back to stream parser, autocomplete
Browse files Browse the repository at this point in the history
  • Loading branch information
drom committed Apr 2, 2023
1 parent 926db10 commit e74f14b
Show file tree
Hide file tree
Showing 26 changed files with 367 additions and 351 deletions.
2 changes: 1 addition & 1 deletion app/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<head>
<meta charset="UTF-8">
<link rel="icon" href="data:image/svg+xml,<svg xmlns=%22http://www.w3.org/2000/svg%22 viewBox=%220 0 100 100%22><text y=%22.9em%22 fill=%22grey%22 font-size=%2290%22>🎃</text></svg>">
<title>test</title>
<title>WaveQL</title>
<link rel="preload" as="font" href="iosevka-term-light.woff2" type="font/woff2" crossorigin>
<style>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ const {keymap, EditorView} = require('@codemirror/view');
const ediatableFlag = new Compartment;

const toggleEditability = (eView) => {
console.log('toggleEditability');
// console.log('toggleEditability');
const e = EditorView.editable;
eView.dispatch({
effects: [
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
17 changes: 6 additions & 11 deletions lib/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,14 @@ const getElement = divName => {

const initText = `\
tb
top
`
/* top .. top // comment
clock :clock
reset
top .. top // comment ..
@100ns
valid ready &
(DIZ (?<id>\\w+)_((?<go>go)|(?<pc>pc))
)
data %sH
addr
valid :valid
Expand All @@ -38,7 +37,6 @@ addr
addr
addr
`;
*/

// TODO

Expand All @@ -47,17 +45,14 @@ addr

global.MAIN = async (div) => {
const root = getElement(div);
// const p = parserWithMetadata();
// console.log(p);

const deso = {
waveql: initText,
wires: {body: [
{kind: 'scope', type: 'module', name: 'tb', body: [
{kind: 'var', type: 'wire', name: 'reset'},
{kind: 'var', type: 'reg', name: 'clock'},
{kind: 'var', type: 'wire', name: 'reset', size: 1},
{kind: 'var', type: 'reg', name: 'clock', size: 1},
{kind: 'scope', type: 'module', name: 'top', body: [
{kind: 'var', type: 'wire', name: 'data'}
{kind: 'var', type: 'wire', name: 'data', size: 32}
]}
]}
]}
Expand Down
146 changes: 146 additions & 0 deletions lib/dictionary.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,146 @@
'use strict';

const get = require('./get-sig.js');
const tracePath = require('./trace-path.js');

const dict = [

{m: '(DIZ', fn: (stream, stt, mat, wires) => {
stream.eatSpace();
const arg1 = stream.match(/^(\S+)(\s+|$)/);
if (arg1) {
let rex;
try {
rex = new RegExp(arg1[1]);
} catch (err) {
return 'comment';
}
const rowo = stt.rowo;
const clock = stt.labelo.clock;
if (clock === undefined) {
return 'comment';
}
rowo.kind = 'DIZ';
rowo.clock = stt.labelo.clock;

const levelo = get(wires, stt.path, false);
const othero0 = levelo.body.reduce((res, ero) => {
const m = ero.name.match(rex);
if (m && m.groups && m.groups.id) {
const {id, pc, go} = m.groups;
const stage = res[id] = (res[id] || {});
const desc = {name: ero.name, ref: ero.link};
if (pc) {
stage[pc] = desc;
} else
if (go) {
stage[go] = desc;
}
}
return res;
}, {});
// filter out singles
const othero = Object.keys(othero0).reduce((res, name) => {
const val = othero0[name];
if (val.pc && val.go) {
res[name] = val;
}
return res;
}, {});
rowo.othero = othero;
stt.rStack.push(rowo);
return 'macroName';
}
return 'comment';
}},

{m: '/', fn: (stream, stt) => {
stt.path.length = 0;
tracePath(stream, stt);
return 'punctuation';
}},

{m: '.', fn: (/* stream, stt */) => {
return 'punctuation';
}},

{m: '..', fn: (stream, stt) => {
stt.path.pop();
tracePath(stream, stt);
return 'punctuation';
}},

{m: '...', fn: (stream, stt) => {
stt.path.pop();
stt.path.pop();
tracePath(stream, stt);
return 'punctuation';
}},

{m: '//', fn: (stream /*, stt */) => {
stream.skipToEnd();
return 'comment';
}},

{m: ')', fn: (stream, stt) => {
const {rStack} = stt;
const rTop = rStack.pop();
if (rTop) {
rTop.len = stt.rowo.idx - rTop.idx + 1;
return 'macroName';
}
return 'comment';
}},

{m: /^@(?<value>\d+)(?<mult>[munpf]*s)(\.(?<style>\w+))?$/, fn: (stream, stt, mat) => {
const scale = {
's': 0,
'ms': -3,
'us': -6,
'ns': -9,
'ps': -12,
'fs': -15
};
const rowo = stt.rowo;
rowo.vlines = rowo.vlines || [];
rowo.vlines.push({
value: Number(mat.groups.value),
mult: scale[mat.groups.mult],
style: mat.groups.style
});
return 'unit';
}},

{m: /^%(?<align>[<>^])?(?<plus>[+-])?(?<sign>[su])?(?<radix>[bodhHac])$/, fn: (stream, stt, mat) => {
stt.rowo.format = mat.groups;
return 'attributeName';
}},

{m: /^:(?<name>\S+)(\s+|$)/, fn: (stream, stt, mat) => {
stt.labelo[mat.groups.name] = stt.rowo;
return 'labelName';
}},

{m: /^\{(?<body>[^}]+)\}$/, fn: (stream, stt, mat) => {
const a = mat.groups.body;
const b = a.split(',');
const body = b.reduce((res, e) => {
const ee = e.split(':');
const [key, val] = ee;
if (ee.length === 2) {
const sig = stt.labelo[val] || Number(val);
res[key] = sig;
} else
if (ee.length === 1) {
res[key] = stt.labelo[key] || {};
}
return res;
}, {});
stt.rowo.kind = 'brace';
stt.rowo.body = body;
return 'brace';
}}

];

module.exports = dict;
11 changes: 8 additions & 3 deletions lib/ext-change-with.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,20 @@

const {EditorView} = require('@codemirror/view');

const waveqlParserWith = require('./waveql-parser-with.js');
const langParser = require('./lang-parser.js');
const waveQlLang = require('./waveql-lang.js');
const dictionary = require('./dictionary.js');

const extChangeWith = (deso, pstate, render) => {
const parser = waveqlParserWith(deso.wires);
const lang = waveQlLang(deso.wires, dictionary);
return EditorView.updateListener.of((update) => {
if (update.docChanged) {
const str = update.state.doc.toString();
deso.view = parser(str);
const parser = langParser(lang);
const stt = parser(str);
deso.view = stt.rows;
pstate.numLanes = deso.view.length;
pstate.pathTrace = stt.pathTrace;
render();
}
});
Expand Down
2 changes: 1 addition & 1 deletion lib/ext-read-only-toggler-with.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ const toggleEditability = (eView) => {

const extReadOnlyTogglerWith = (key, initialValue) => [
roFlag.of(EditorState.readOnly.of(initialValue)),
roStyle.of(ro),
roStyle.of(initialValue ? ro : rw),
keymap.of([{key, run: toggleEditability, preventDefault: true}])
];

Expand Down
61 changes: 3 additions & 58 deletions lib/ext-waveql-lang-with.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,11 @@

const {StreamLanguage, syntaxHighlighting, HighlightStyle} = require('@codemirror/language');
const {tags} = require('@lezer/highlight');

const get = require('./get-sig.js');
const getScopeType = require('./get-scope-type.js');
const getVarType = require('./get-var-type.js');
const waveQlLang = require('./waveql-lang.js');

// waveql syntax
// https://codemirror.net/docs/ref/#language.StreamLanguage


// https://github.com/lezer-parser/highlight/blob/main/src/highlight.ts

const sidebarHighlightStyleGen = t => [
Expand Down Expand Up @@ -38,60 +34,9 @@ const sidebarHighlightStyleGen = t => [
// textUnderlinePosition: 'under'
// textDecoration: 'line-through'

const waveQlLang = wires => ({
startState: function() {
return {path: []};
},
token: function (stream, stt) {
let mat;

if (stream.eatSpace()) { return null; }

mat = stream.match(/^\/\//); if (mat) { stream.skipToEnd(); return 'comment'; }

mat = stream.match(/^@\d+[munpf]*s(\.\w+)*(\s+|$)/); if (mat) { return 'unit'; }

mat = stream.match(/^\.\.\.(\s+|$)/); if (mat) { stt.path.pop(); stt.path.pop(); return 'punctuation'; }
mat = stream.match(/^\.\.(\s+|$)/); if (mat) { stt.path.pop(); return 'punctuation'; }
mat = stream.match(/^\.(\s+|$)/); if (mat) { return 'punctuation'; }
mat = stream.match(/^\/(\s+|$)/); if (mat) { stt.path.length = 0; return 'punctuation'; }

mat = stream.match(/^:(\S+)(\s+|$)/); if (mat) { return 'labelName'; }

mat = stream.match(/^\{[^}]+\}(\s+|$)/); if (mat) { return 'brace'; }

mat = stream.match(/^%([<>^])?([+-])?([su])?([bodhHac])(\s+|$)/); if (mat) { return 'attributeName'; }

mat = stream.match(/\(\w+(\s+|$)/); if (mat) { return 'macro'; }

mat = stream.match(/^(\S+)(\s+|$)/); if (mat) {
const sigName = mat[1];
const newPath = stt.path.concat(sigName);
const ero = get(wires, newPath, false);
// console.log(newPath, ero);
if (ero) {
if (ero.kind === 'scope') {
stt.path = newPath;
return (getScopeType(ero.type)).tag;
}
if (ero.kind === 'var') {
// stt.varWidth = ero.size;
return (getVarType(ero.type)).tag;
}
}
return 'comment';
}
},
languageData: {
commentTokens: {line: '//'}
}
});


const extWaveqlLangWith = (wires) => {
// console.log(wires);
const extWaveqlLangWith = (wires, dict) => {
return [
StreamLanguage.define(waveQlLang(wires)),
StreamLanguage.define(waveQlLang(wires, dict)),
syntaxHighlighting(HighlightStyle.define(sidebarHighlightStyleGen(tags)))
];
};
Expand Down
2 changes: 1 addition & 1 deletion lib/get-scope-type.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

const getScopeType = (key) => {
const lut = {
module: {id: 0, tag: 'meta', icon: 'module', term: 'scopeModule'},
module: {id: 0, tag: 'meta', icon: 'module', term: 'scopeModule'}
// task: {id: 1, tag: 'namespace'},
// function: {id: 2, tag: 'namespace'},
// begin: {id: 3, tag: 'namespace'},
Expand Down
10 changes: 4 additions & 6 deletions lib/index.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
'use strict';

const pkg = require('../package.json');
const tokenizer = require('./tokenizer.js');
const lexer = require('./lexer.js');
const parse = require('./parse.js');
const langParser = require('./lang-parser.js');
const waveQlLang = require('./waveql-lang.js');
const mountCodeMirror6 = require('./mount-codemirror6.js');

exports.version = pkg.version;
exports.tokenizer = tokenizer;
exports.lexer = lexer;
exports.parse = parse;
exports.langParser = langParser;
exports.waveQlLang = waveQlLang;
exports.mountCodeMirror6 = mountCodeMirror6;
Loading

0 comments on commit e74f14b

Please sign in to comment.