Skip to content

Commit

Permalink
added debugging and tests, added value types, fixed bugs
Browse files Browse the repository at this point in the history
  • Loading branch information
forgodtosave committed Jul 1, 2023
1 parent b23f0dc commit bda214c
Show file tree
Hide file tree
Showing 55 changed files with 6,692 additions and 6,871 deletions.
4 changes: 2 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,5 @@ cypress/screenshots/
cypress/videos/
components.d.ts

src/plugins/languages/dist
src/plugins/languages/KlipperConfigLanguage/*.d.ts
src/plugins/Codemirror/KlipperCfgLang/parser/klipperCfgParser.js
src/plugins/Codemirror/KlipperCfgLang/parser/klipperCfgParser.terms.js
2 changes: 0 additions & 2 deletions .prettierignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,3 @@ dist/
public/
components.d.ts
CHANGELOG.md
src/plugins/CodemirrorLanguages/KlipperConfigLanguage/parser/klipperConfigParser.terms.js
src/plugins/CodemirrorLanguages/KlipperConfigLanguage/parser/klipperConfigParser.js
1 change: 1 addition & 0 deletions dev-dist/registerSW.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7,748 changes: 971 additions & 6,777 deletions package-lock.json

Large diffs are not rendered by default.

10 changes: 9 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,12 @@
"scripts": {
"serve": "vite serve",
"build": "vite build && npm run build.zip",
"build:lang": "npm run build:parser:klipperCfg && npm run build:lang:klipperCfg",
"build:lang:klipperCfg": "rollup --config src/plugins/Codemirror/KlipperCfgLang/rollup.config.js",
"build:parser:klipperCfg": "lezer-generator src/plugins/Codemirror/KlipperCfgLang/parser/klipperCfg.grammar -o src/plugins/Codemirror/KlipperCfgLang/parser/klipperCfgParser.js",
"format": "npm run format:base -- --write",
"format:base": "prettier .",
"format:check": "npm run format:base -- --check",
"build-parser": "npx @lezer/generator src/plugins/CodemirrorLanguages/KlipperConfigLanguage/parser/klipperConfig.grammar -o src/plugins/CodemirrorLanguages/KlipperConfigLanguage/parser/klipperConfigParser.js",
"lint": "eslint --ignore-path .gitignore src",
"lint:fix": "npm run lint -- --fix",
"build.zip": "cd ./dist && zip -r mainsail.zip ./ -x '**.DS_Store' ./ && cd ..",
Expand All @@ -21,6 +23,7 @@
"start": "vite build && vite preview",
"test": "start-server-and-test preview http://127.0.0.1:4173/ 'cypress run'",
"test:ui": "cypress open",
"test:parser:klipperCfg": "mocha --config src/plugins/Codemirror/.mocharc.json src/plugins/Codemirror/KlipperCfgLang/test/test-klipperCfg.js",
"changelog": "git cliff v0.0.4..$(git describe --tags $(git rev-list --tags --max-count=1)) --output CHANGELOG.md"
},
"dependencies": {
Expand Down Expand Up @@ -72,7 +75,9 @@
},
"devDependencies": {
"@intlify/vite-plugin-vue-i18n": "^2.5.0",
"@lezer/generator": "^1.3.0",
"@mdi/js": "^7.0.0",
"@rollup/plugin-node-resolve": "^15.1.0",
"@types/file-saver": "^2.0.5",
"@types/jmuxer": "^2.0.3",
"@types/lodash.kebabcase": "^4.1.6",
Expand All @@ -88,9 +93,12 @@
"eslint-config-prettier": "^8.3.0",
"eslint-plugin-jsonc": "^2.2.1",
"eslint-plugin-vue": "^9.0.0",
"mocha": "^10.2.0",
"prettier": "^2.5.1",
"rollup": "^2.79.1",
"sass": "~1.32",
"start-server-and-test": "^1.14.0",
"ts-node": "^10.9.1",
"typescript": "^4.5.5",
"unplugin-vue-components": "^0.22.0",
"vite": "^3.0.0",
Expand Down
10 changes: 10 additions & 0 deletions src/components/inputs/Codemirror.vue
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,11 @@ import { klipperCfg } from '../../plugins/Codemirror/KlipperCfgLang/lang/klipper
import { parseErrorLint } from '../../plugins/Codemirror/parseErrorLint'
import { indentUnit } from '@codemirror/language'
// for lezer grammar debugging
/* import { logTree } from '../../plugins/Codemirror/printLezerTree'
import { syntaxTree } from '@codemirror/language'
import { parser } from '../../plugins/Codemirror/KlipperCfgLang/dist/klipperCfgParser.es.js' */
@Component
export default class Codemirror extends Mixins(BaseMixin) {
private content = ''
Expand Down Expand Up @@ -50,6 +55,11 @@ export default class Codemirror extends Mixins(BaseMixin) {
if (newVal !== cm_value) {
this.setCmValue(newVal)
}
// for lezer grammar debugging
/* const state = this.cminstance?.state ?? EditorState.create({})
logTree(syntaxTree(state), state.doc.toString())
const text = state.doc.toString()
console.log(parser.parse(text) + '') */
}
mounted(): void {
Expand Down
6 changes: 6 additions & 0 deletions src/plugins/Codemirror/.mocharc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"extensions": ["ts"],
"node-option": [
"loader=ts-node/esm"
]
}
2 changes: 1 addition & 1 deletion src/plugins/Codemirror/KlipperCfgLang/lang/klipperCfg.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { parser } from '../parser/klipperCfgParser.js'
import { parser } from '../dist/klipperCfgParser.es.js'
import { LRLanguage, LanguageSupport, StreamLanguage, foldNodeProp } from '@codemirror/language'
import { parseMixed } from '@lezer/common'
import { klipper_config } from '../../../StreamParserKlipperConfig.js'
Expand Down
4 changes: 4 additions & 0 deletions src/plugins/Codemirror/KlipperCfgLang/parser/highlight.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,15 @@ export const klipperConfigHighlighting = styleTags({
Boolean: t.bool,
Number: t.number,
Cords: t.number,
Resolution: t.number,
Ratio: t.number,
Pin: t.namespace,
VirtualPin: t.namespace,
Path: t.string,
File: t.string,
FilePath: t.string,
Ipv4: t.number,
Ipv6: t.number,

AutoGenerated: t.regexp,
Comment: t.lineComment,
Expand Down
75 changes: 41 additions & 34 deletions src/plugins/Codemirror/KlipperCfgLang/parser/klipperCfg.grammar
Original file line number Diff line number Diff line change
@@ -1,61 +1,68 @@
@precedence { str @cut }
@top Program { (Import | ConfigBlock )+ }

@top Program { (Import | ConfigBlock)+ }
@skip { AutoGenerated (newline | eof) | space | blankLine | Comment newline? }

@skip { Comment newline? | AutoGenerated newline | space | blankLine }

valueBlock<content> { indent (content newline | valueBlock<content>)+ (dedent | eof) }
valueBlock<content> { indent (content (newline | eof) | valueBlock<content>)+ (dedent | eof) }
sep<content, seperator> { content (seperator content)+ }


Import { "[" ImportKeyword FilePath "]" (newline | eof) }
ConfigBlock {"[" BlockType Identifier* "]" ( newline Body? | newline? eof ) }

Import { "[" ImportKeyword FilePath "]" newline }
ConfigBlock {"[" BlockType Identifier? "]" newline Body? }

Body { Option+ }
Option { Parameter ":" Value | GcodeKeyword ":" Jinja2 }
Body { Option+ eof? }
Option { Parameter ":" Value | GcodeKeyword Jinja2 }

Value { value (newline | eof) | newline valueBlock<value> }
Jinja2 { jinja2 (newline | eof) | newline valueBlock<jinja2> }
Value { value (newline | eof) | value? newline valueBlock<value> }
Jinja2 { jinja2 (newline | eof) | jinja2? newline valueBlock<jinja2> }

value { Pin | pins | VirtualPin | VirtualPin | Cords | Number | String | Boolean | Path | FilePath }
pins { sep<Pin, ","> }
VirtualPin { string ":" string }
value { Pin | pins | VirtualPin | Cords | Number | String | Boolean | Path | FilePath | Resolution | Ratio | Ipv4 | Ipv6 }
pins { sep<(Pin | VirtualPin), ","> }
Cords { sep<number, ","> }
Number { number }
String { string }
Path { ("/"|"~/")? !str string ("/" string)* }
FilePath { Path "." string}



@context trackIndent from "./tokens.js"
@external tokens indentation from "./tokens.js" { indent, dedent }
@external tokens newlines from "./tokens.js" { newline, blankLine, eof }

@tokens {
extAscii { $[a-zA-Z0-9_\-.] }
unixPath { ![/<>|:&{}\t\f #;\[\]\n\r] }

ImportKeyword{ "include" }
GcodeKeyword{ $[a-zA-Z0-9_.\-]* "gcode" }
BlockType { $[a-zA-Z0-9_]+ }
Identifier { $[a-zA-Z0-9_.\-]+ }
Parameter { $[a-zA-Z0-9_]+ }

string { ($[ a-zA-Z0-9_\-!:]+ | "//") }
jinja2 { $[ \ta-zA-Z0-9_.\-"'{}%=]+ }
GcodeKeyword{ extAscii* "gcode:" }
BlockType { extAscii+ }
Identifier { extAscii+ }
Parameter { extAscii+ }
Path { ("/"|"~/") unixPath+ ("/" unixPath+)* }
FilePath { (Path | unixPath+) "." unixPath+ }
Resolution { $[0-9]+ "x" $[0-9]+ }
Ratio { $[0-9]+ ":" $[0-9]+ }
Ipv4 { $[0-9]+ "." $[0-9]+ "." $[0-9]+ "." $[0-9]+ ((":"|"/") $[0-9]+)? }
hex { $[0-9a-fA-F] }
Ipv6 { (("::" (hex+ ":")* hex+)
| (hex+ (":" hex+)* "::")
| ((hex+ ":")+ (":" hex+)+)
| ((hex+ ":" hex+ ":" hex+ ":" hex+ ":" hex+ ":" hex+)))
("/" $[0-9]+)?}

String { ![#;\n\r]+ }
jinja2 { ![#;\n\r]+ }
number { "-"? $[0-9]+ ("." $[0-9]*)? }
Boolean { "True" | "False" }
Pin { ("^" | "~")? "!"? "P" $[A-Z] $[0-9]+ }
Boolean { "True" | "False" | "true" | "false" }
Pin { ("^" | "~")? "!"? "P" $[A-Z]? $[0-9.]+ }
VirtualPin { ("^" | "~")? "!"? extAscii+ ":" extAscii+ }

AutoGenerated { "#*#" ![\n\r]* }
Comment { "#" ![\n\r]* }
Comment { ("#"|";") ![\n\r]* }

space { $[ \t\f]+ }

@precedence { space, jinja2, string }
@precedence { AutoGenerated, Comment }
@precedence { number, Pin, Boolean, ImportKeyword, string, "/" }
@precedence { ImportKeyword, BlockType }
@precedence { GcodeKeyword, Parameter }
@precedence { space, jinja2, String } // because spaces are allowed in string/jinja2
@precedence { AutoGenerated, Comment } // AutoGenerated also starts with a #
@precedence { Resolution, Ipv4, Ipv6, Ratio, number, Pin, VirtualPin, Boolean, ImportKeyword, FilePath, Path, String }
@precedence { ImportKeyword, BlockType }// because the ImportKeyword can be canerated with extAscii
@precedence { GcodeKeyword, Parameter } // because the GcodeKeyword can be canerated with extAscii
}

@external propSource klipperConfigHighlighting from "./highlight"
Expand Down

This file was deleted.

20 changes: 0 additions & 20 deletions src/plugins/Codemirror/KlipperCfgLang/parser/klipperCfgParser.js

This file was deleted.

This file was deleted.

4 changes: 2 additions & 2 deletions src/plugins/Codemirror/KlipperCfgLang/parser/tokens.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ export const newlines = new ExternalTokenizer(
while (input.next == space || input.next == tab) {
input.advance()
}
if (input.next == newline || input.next == carriageReturn) input.acceptToken(blankLine, 1)
if (isLineBreak(input.next)) input.acceptToken(blankLine, 1)
} else if (isLineBreak(input.next)) {
input.acceptToken(newlineToken, 1)
}
Expand Down Expand Up @@ -72,7 +72,7 @@ export const trackIndent = new ContextTracker({
return context.depth < 0 ? context.parent : context
},
shift(context, term, stack, input) {
if (term == indent) return new IndentLevel(context, countIndent(input.read(input.pos, stack.pos)))
if (term == indent) return new IndentLevel(context, countIndent(input.read(input.pos, stack.pos)) >= 1 ? 1 : 0)
if (term == dedent) return context.parent
return context
},
Expand Down
19 changes: 19 additions & 0 deletions src/plugins/Codemirror/KlipperCfgLang/rollup.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { nodeResolve } from '@rollup/plugin-node-resolve'

export default {
input: 'src/plugins/Codemirror/KlipperCfgLang/parser/klipperCfgParser.js',
output: [
{
format: 'cjs',
file: 'src/plugins/Codemirror/KlipperCfgLang/dist/klipperCfgParser.cjs',
},
{
format: 'es',
file: 'src/plugins/Codemirror/KlipperCfgLang/dist/klipperCfgParser.es.js',
},
],
external(id) {
return !/^[\.\/]/.test(id)
},
plugins: [nodeResolve()],
}
27 changes: 27 additions & 0 deletions src/plugins/Codemirror/KlipperCfgLang/test/test-klipperCfg.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { parser } from '../dist/klipperCfgParser.cjs'
import { fileTests } from '../../mochaFileTests.js'
import * as fs from 'fs'
import * as path from 'path'

const caseDir = 'src/plugins/Codemirror/KlipperCfgLang/test/testCases'
const testConfigsDir = 'src/plugins/Codemirror/KlipperCfgLang/test/testConfigs'

for (let file of fs.readdirSync(caseDir)) {
if (!/\.txt$/.test(file)) continue

let result = /^[^.]+/.exec(file)
let name = result ? result[0] : 'default-name'
describe(name, () => {
for (let { name, run } of fileTests(fs.readFileSync(path.join(caseDir, file), 'utf8'), file))
it(name, () => run(parser))
})
}

for (let file of fs.readdirSync(testConfigsDir)) {
let result = /^[^.]+/.exec(file)
let name = result ? result[0] : 'default-name'
describe(name, () => {
for (let { name, run } of fileTests(fs.readFileSync(path.join(testConfigsDir, file), 'utf8'), file, true))
it(name, () => run(parser))
})
}
Loading

0 comments on commit bda214c

Please sign in to comment.