Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up

A parser combinator library for Go.

License

NotificationsYou must be signed in to change notification settings

shellyln/takenoco

Repository files navigation

Parser Combinator Library for Go.
A framework for making easy use of parser combinators and production rules.

TestreleaseGo version

logo


🪄 Introduction

Introduction to takenoco, a parser combinator library for Go

🧭 Examples

🚀 Getting started

Define the parser:

package csvimport ("errors""strconv"    ."github.com/shellyln/takenoco/base"    ."github.com/shellyln/takenoco/string")var (// Comma and line break characterscellBreakCharacters []stringdocumentParserParserFn)funcinit() {cellBreakCharacters=make([]string,0,len(LineBreakCharacters)+1)cellBreakCharacters=append(cellBreakCharacters,",")cellBreakCharacters=append(cellBreakCharacters,LineBreakCharacters...)documentParser=document()}// Remove the resulting AST.funcerase(fnParserFn)ParserFn {returnTrans(fn,Erase)}// Whitespacesfuncsp()ParserFn {returnerase(ZeroOrMoreTimes(WhitespaceNoLineBreak()))}funcquotedCell()ParserFn {returnTrans(OneOrMoreTimes(FlatGroup(sp(),erase(Seq("\"")),ZeroOrMoreTimes(First(erase(Seq("\"\"")),CharClassN("\""),                    ),                ),First(erase(Seq("\"")),FlatGroup(End(),Error("Unexpected EOF")),                ),sp(),            ),        ),Concat,    )}funccell()ParserFn {returnTrans(ZeroOrMoreTimes(CharClassN(cellBreakCharacters...)),Trim,    )}// Convert AST to array data. (line)funclineTransform(_ParserContext,astsAstSlice) (AstSlice,error) {w:=make([]string,len(asts))length:=len(asts)fori:=0;i<length;i++ {w[i]=asts[i].Value.(string)    }returnAstSlice{{ClassName:"*Line",Type:AstType_Any,Value:w,    }},nil}funcline()ParserFn {returnTrans(FlatGroup(ZeroOrMoreTimes(First(quotedCell(),cell()),erase(Seq(",")),            ),First(quotedCell(),cell()),        ),lineTransform,    )}// Convert AST to array data. (Entire document)funcdocumentTransform(_ParserContext,astsAstSlice) (AstSlice,error) {length:=len(asts)w:=make([][]string,length)fori:=0;i<length;i++ {w[i]=asts[i].Value.([]string)    }fori:=length-1;i>=0;i-- {iflen(w[i])==0||len(w[i])==1&&w[i][0]=="" {w=w[:i]        }else {break        }    }returnAstSlice{{ClassName:"*Document",Type:AstType_Any,Value:w,    }},nil}funcdocument()ParserFn {returnTrans(FlatGroup(ZeroOrMoreTimes(line(),erase(OneOrMoreTimes(LineBreak())),            ),line(),End(),        ),documentTransform,    )}funcParse(sstring) ([][]string,error) {out,err:=documentParser(*NewStringParserContext(s))iferr!=nil {returnnil,err    }else {ifout.MatchStatus==MatchStatus_Matched {returnout.AstStack[0].Value.([][]string),nil        }else {returnnil,errors.New("Parse failed at "+strconv.Itoa(out.SourcePosition.Position))        }    }}

Use the parser:

package mainimport ("fmt""os"    csv"github.com/shellyln/takenoco/_examples/csv")funcmain() {x,err:=csv.Parse("0,1,2,3,4,5,6,7,8,9\n0,1,2,3,4,5,6,7,8,9")iferr!=nil {fmt.Fprintln(os.Stderr,err)os.Exit(-1)    }fmt.Println(x)y:=csv.ToCsv(x)fmt.Println(y)os.Exit(0)}

Run on go playground

📦 Build the example app

🪟 Windows prerequirements:

choco install make# or# scoop install make

🔹 Build to native executable

make

🔹 Build to WebAssembly (Go)

make wasm

🔹 Build to WebAssembly (TinyGo; experimental)

🪟 Windows prerequirements:

scoop install tinygoscoop install binaryen

🐧 Linux prerequirements:

🍎 Mac prerequirements:

Build:

make tinywasm

🧩 Contributing

Prerequirements:

go install golang.org/x/tools/go/analysis/passes/shadow/cmd/shadow@latestgo install honnef.co/go/tools/cmd/staticcheck@latest

⚖️ License

MIT
Copyright (c) 2021 Shellyl_N and Authors.


[8]ページ先頭

©2009-2025 Movatter.jp