Skip to content

Instantly share code, notes, and snippets.

View kfsone's full-sized avatar

Oliver Smith kfsone

View GitHub Profile
// This is a relatively common pattern:
while (true)
{
if (predicate())
continue;
...
break;
}
// so is this
> cmd
Microsoft Windows [Version 10.0.20241.1005]
(c) 2020 Microsoft Corporation. All rights reserved.
C:\Users\oliver\AppData\Local\Temp>mkdir d1 d2
C:\Users\oliver\AppData\Local\Temp>echo @echo BAT >d1\a.bat
C:\Users\oliver\AppData\Local\Temp>echo @echo BAT >d2\a.bat
C:\Users\oliver\AppData\Local\Temp>echo print('PY') >d1\a.py
C:\Users\oliver\AppData\Local\Temp>echo print('PY') >d2\a.py
C:\Users\oliver\AppData\Local\Temp>set PATHEXT=.COM;.EXE;.BAT;.PY
C:\Users\oliver\AppData\Local\Temp>set PATH=d1;d2;%PATH%
@kfsone
kfsone / main.go
Created February 26, 2021 00:17
andlabs/ui combobox not rendering in a tab on windows
package main
import (
"fmt"
"github.com/andlabs/ui"
_ "github.com/andlabs/ui/winmanifest"
)
func makePage1() ui.Control {
return ui.NewLabel("Page 1")
func (e *Error) Error() string {
w := new(strings.Builder)
fmt.Fprintf(w, "%d:%d: error: ", e.ErrorToken.Pos.Line, e.ErrorToken.Pos.Column)
if e.Err != nil {
fmt.Fprintf(w, "%w: %q", e.Err, e.ErrorToken.Lit)
} else {
if len(e.ExpectedTokens) == 1 {
// if there's only one expectation, don't describe it in list terms.
fmt.Fprint(w, "expected: ", e.ExpectedTokens[0])
func (e *Error) Error() string {
w := new(strings.Builder)
fmt.Fprintf(w, "%d:%d: error: ", e.ErrorToken.Pos.Line, e.ErrorToken.Pos.Column)
if e.Err != nil {
fmt.Fprint(w, e.Err)
} else {
if len(e.ExpectedTokens) == 1 {
// if there's only one expectation, don't describe it in list terms.
fmt.Fprint(w, "expected ", e.ExpectedTokens[0])
@kfsone
kfsone / parsing.md
Last active March 16, 2021 17:37
Thoughts on a parser generator

The thinking

I'm thinking I might just write a typical ebnf generator, but then I'd be going from reinventing the wheel to reinventing the wheel, tire and hubcap.

EBNF generators I've used are very hands-offy because providing for the most common paths might somehow exclude some use cases.

So I think I want something that is EBNF+, and which is focused on describing c/json/lua/python type DSLs in a top-down way that allows accumulation of guidance through context, allows the user to validate in-situ rather than forcing them to write the code to build the parse tree, code to walk it and validate it and then code to do anything with it.

Start from EBNF and extend sideways, if you will.

@kfsone
kfsone / structuredrules.md
Last active March 16, 2021 18:50
structured rule example

Structured Rules

These are mostly syntactic sugar for deeper code generation and integration with the user's AST.

They are also tied to a top-down concept that helps guide parsing by supporting context.

E.g: How do you validate the type of a function parameter before the end of the body?

StructuredRules grammar is a superset of BNF.

make install
make[1]: Entering directory '/mnt/c/Users/oliver/go/src/github.com/kfsone/gocc'
go install .
make[1]: Leaving directory '/mnt/c/Users/oliver/go/src/github.com/kfsone/gocc'
make -C example regenerate
make[1]: Entering directory '/mnt/c/Users/oliver/go/src/github.com/kfsone/gocc/example'
make -C astx regenerate
make[2]: Entering directory '/mnt/c/Users/oliver/go/src/github.com/kfsone/gocc/example/astx'
gocc ast.bnf
make[2]: Leaving directory '/mnt/c/Users/oliver/go/src/github.com/kfsone/gocc/example/astx'
go vet -methods=false .
go get github.com/kisielk/errcheck@v1.2.0
go: finding github.com v1.2.0
go: finding github.com/kisielk v1.2.0
errcheck -exclude .errcheck-ignore ./...
make install
make[1]: Entering directory '/mnt/c/Users/oliver/go/src/github.com/kfsone/gocc'
go install .
make[1]: Leaving directory '/mnt/c/Users/oliver/go/src/github.com/kfsone/gocc'
make -C example regenerate
make[1]: Entering directory '/mnt/c/Users/oliver/go/src/github.com/kfsone/gocc/example'
make -C astx regenerate
make[2]: Entering directory '/mnt/c/Users/oliver/go/src/github.com/kfsone/gocc/example/astx'
gocc ast.bnf
make[2]: Leaving directory '/mnt/c/Users/oliver/go/src/github.com/kfsone/gocc/example/astx'