From 9a64b665ec75b651cfec2a12084c7dede5a21b9a Mon Sep 17 00:00:00 2001 From: Steven Kalt Date: Sun, 18 Oct 2020 11:54:10 -0400 Subject: [PATCH] feat(parser): Add parser combinators A bot pf a WIP. Draft parser combinators to handle all parts of the CC standard. --- .gitignore | 3 + README.md | 34 +++-- go.mod | 7 ++ go.sum | 216 ++++++++++++++++++++++++++++++++ pkg/parser/combinators.go | 256 ++++++++++++++++++++++++++++++++++++++ pkg/parser/parser.go | 214 +++++-------------------------- pkg/parser/parser_test.go | 40 ++++-- 7 files changed, 568 insertions(+), 202 deletions(-) create mode 100644 .gitignore create mode 100644 go.sum create mode 100644 pkg/parser/combinators.go diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..c2dd9a5 --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +node_modules +dist +.vscode \ No newline at end of file diff --git a/README.md b/README.md index 33da696..d6b28b4 100644 --- a/README.md +++ b/README.md @@ -1,32 +1,46 @@ # git-cc > a git extension to help write [conventional commits][cc-standard] -## quickstart +## install ```sh go install github.com/skalt/git-cc -git cc -init -y # need to configure git-cc within this repo. Use default types. -git add . git cc feat added conventional commits -# equivalent to `git commit -m "feat: added conventional commits"` - +# --> "feat: added conventional commits" git cc fix something: an error -# equivalent to `git commit -m "fix(something): an error"` +# --> "fix(something): an error" git cc feat -# interactively write a conventional commit +# interactively write a conventional commit from the description onward +git cc +# interactively write a conventional commit! ``` + + +## Why An Interactive Cli? +Figuring out what to write for an informative commit can be difficult. +The convential commits standard helps figure out what to write. +An interactive prompts helps with following the standard. + +
Some parts of the conventional commit standard require quoting to work on the command-line. -## Why? +- `git commit -m fix(something): ...` fails since `()` would be a syntactically-invalid subshell +- `git commit -m feat!: ...` fails since `!` expands to a bash history command +
You want the same interface in any project, on any machine. You want to use your git log as your changelog. Prior art: -[`committizen`][commitizen]. +- [`committizen`][commitizen] +- [`commitsar`][commitsar] [cc-standard]: https://www.conventionalcommits.org/en/v1.0.0/ [commitizen]: https://github.com/commitizen/cz-cli -[commitlint]: https://github.com/conventional-changelog/commitlint/tree/master/%40commitlint/config-conventional \ No newline at end of file +[commitlint]: https://github.com/conventional-changelog/commitlint/tree/master/%40commitlint/config-conventional +[commitsar]: https://github.com/commitsar-app/commitsar \ No newline at end of file diff --git a/go.mod b/go.mod index 90046e1..d5f9b90 100644 --- a/go.mod +++ b/go.mod @@ -1,3 +1,10 @@ module github.com/skalt/git-cc go 1.14 + +require ( + github.com/charmbracelet/bubbletea v0.11.1 // indirect + github.com/go-git/go-git/v5 v5.0.0 // indirect + github.com/manifoldco/promptui v0.7.0 + github.com/spf13/cobra v1.0.0 // indirect +) diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..73b47b9 --- /dev/null +++ b/go.sum @@ -0,0 +1,216 @@ +cloud.google.com/go v0.26.0/go.mod h1:aQUYkXzVsufM+DwF1aE+0xfcU+56JwCaLick0ClmMTw= +github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU= +github.com/OneOfOne/xxhash v1.2.2/go.mod h1:HSdplMjZKSmBqAxg5vPj2TmRDmfkzw+cTzAElWljhcU= +github.com/alcortesm/tgz v0.0.0-20161220082320-9c5fe88206d7/go.mod h1:6zEj6s6u/ghQa61ZWa/C2Aw3RkjiTBOix7dkqa1VLIs= +github.com/alecthomas/template v0.0.0-20160405071501-a0175ee3bccc/go.mod h1:LOuyumcjzFXgccqObfd/Ljyb9UuFJ6TxHnclSeseNhc= +github.com/alecthomas/units v0.0.0-20151022065526-2efee857e7cf/go.mod h1:ybxpYRFXyAe+OPACYpWeL0wqObRcbAqCMya13uyzqw0= +github.com/anmitsu/go-shlex v0.0.0-20161002113705-648efa622239/go.mod h1:2FmKhYUyUczH0OGQWaF5ceTx0UBShxjsH6f8oGKYe2c= +github.com/armon/consul-api v0.0.0-20180202201655-eb2c6b5be1b6/go.mod h1:grANhF5doyWs3UAsr3K4I6qtAmlQcZDesFNEHPZAzj8= +github.com/armon/go-socks5 v0.0.0-20160902184237-e75332964ef5/go.mod h1:wHh0iHkYZB8zMSxRWpUBQtwG5a7fFgvEO+odwuTv2gs= +github.com/beorn7/perks v0.0.0-20180321164747-3a771d992973/go.mod h1:Dwedo/Wpr24TaqPxmxbtue+5NUziq4I4S80YR8gNf3Q= +github.com/beorn7/perks v1.0.0/go.mod h1:KWe93zE9D1o94FZ5RNwFwVgaQK1VOXiVxmqh+CedLV8= +github.com/cespare/xxhash v1.1.0/go.mod h1:XrSqR1VqqWfGrhpAt58auRo0WTKS1nRRg3ghfAqPWnc= +github.com/charmbracelet/bubbletea v0.11.1 h1:A+1+YqYN0j/ghKfGZ27LJo+Vg80fGDP+MTudHO56t2o= +github.com/charmbracelet/bubbletea v0.11.1/go.mod h1:3gZkYELUOiEUOp0bTInkxguucy/xRbGSOcbMs1geLxg= +github.com/chzyer/logex v1.1.10 h1:Swpa1K6QvQznwJRcfTfQJmTE72DqScAa40E+fbHEXEE= +github.com/chzyer/logex v1.1.10/go.mod h1:+Ywpsq7O8HXn0nuIou7OrIPyXbp3wmkHB+jjWRnGsAI= +github.com/chzyer/readline v0.0.0-20180603132655-2972be24d48e h1:fY5BOSpyZCqRo5OhCuC+XN+r/bBCmeuuJtjz+bCNIf8= +github.com/chzyer/readline v0.0.0-20180603132655-2972be24d48e/go.mod h1:nSuG5e5PlCu98SY8svDHJxuZscDgtXS6KTTbou5AhLI= +github.com/chzyer/test v0.0.0-20180213035817-a1ea475d72b1 h1:q763qf9huN11kDQavWsoZXJNW3xEE4JJyHa5Q25/sd8= +github.com/chzyer/test v0.0.0-20180213035817-a1ea475d72b1/go.mod h1:Q3SI9o4m/ZMnBNeIyt5eFwwo7qiLfzFZmjNmxjkiQlU= +github.com/client9/misspell v0.3.4/go.mod h1:qj6jICC3Q7zFZvVWo7KLAzC3yx5G7kyvSDkc90ppPyw= +github.com/containerd/console v1.0.1 h1:u7SFAJyRqWcG6ogaMAx3KjSTy1e3hT9QxqX7Jco7dRc= +github.com/containerd/console v1.0.1/go.mod h1:XUsP6YE/mKtz6bxc+I8UiKKTP04qjQL4qcS3XoQ5xkw= +github.com/coreos/bbolt v1.3.2/go.mod h1:iRUV2dpdMOn7Bo10OQBFzIJO9kkE559Wcmn+qkEiiKk= +github.com/coreos/etcd v3.3.10+incompatible/go.mod h1:uF7uidLiAD3TWHmW31ZFd/JWoc32PjwdhPthX9715RE= +github.com/coreos/go-semver v0.2.0/go.mod h1:nnelYz7RCh+5ahJtPPxZlU+153eP4D4r3EedlOD2RNk= +github.com/coreos/go-systemd v0.0.0-20190321100706-95778dfbb74e/go.mod h1:F5haX7vjVVG0kc13fIWeqUViNPyEJxv/OmvnBo0Yme4= +github.com/coreos/pkg v0.0.0-20180928190104-399ea9e2e55f/go.mod h1:E3G3o1h8I7cfcXa63jLwjI0eiQQMgzzUDFVpN/nH/eA= +github.com/cpuguy83/go-md2man/v2 v2.0.0/go.mod h1:maD7wRr/U5Z6m/iR4s+kqSMx2CaBsrgA7czyZG/E6dU= +github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E= +github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/dgrijalva/jwt-go v3.2.0+incompatible/go.mod h1:E3ru+11k8xSBh+hMPgOLZmtrrCbhqsmaPHjLKYnJCaQ= +github.com/dgryski/go-sip13 v0.0.0-20181026042036-e10d5fee7954/go.mod h1:vAd38F8PWV+bWy6jNmig1y/TA+kYO4g3RSRF0IAv0no= +github.com/emirpasic/gods v1.12.0 h1:QAUIPSaCu4G+POclxeqb3F+WPpdKqFGlw36+yOzGlrg= +github.com/emirpasic/gods v1.12.0/go.mod h1:YfzfFFoVP/catgzJb4IKIqXjX78Ha8FMSDh3ymbK86o= +github.com/flynn/go-shlex v0.0.0-20150515145356-3f9db97f8568/go.mod h1:xEzjJPgXI435gkrCt3MPfRiAkVrwSbHsst4LCFVfpJc= +github.com/fsnotify/fsnotify v1.4.7/go.mod h1:jwhsz4b93w/PPRr/qN1Yymfu8t87LnFCMoQvtojpjFo= +github.com/ghodss/yaml v1.0.0/go.mod h1:4dBDuWmgqj2HViK6kFavaiC9ZROes6MMH2rRYeMEF04= +github.com/gliderlabs/ssh v0.2.2/go.mod h1:U7qILu1NlMHj9FlMhZLlkCdDnU1DBEAqr0aevW3Awn0= +github.com/go-git/gcfg v1.5.0 h1:Q5ViNfGF8zFgyJWPqYwA7qGFoMTEiBmdlkcfRmpIMa4= +github.com/go-git/gcfg v1.5.0/go.mod h1:5m20vg6GwYabIxaOonVkTdrILxQMpEShl1xiMF4ua+E= +github.com/go-git/go-billy/v5 v5.0.0 h1:7NQHvd9FVid8VL4qVUMm8XifBK+2xCoZ2lSk0agRrHM= +github.com/go-git/go-billy/v5 v5.0.0/go.mod h1:pmpqyWchKfYfrkb/UVH4otLvyi/5gJlGI4Hb3ZqZ3W0= +github.com/go-git/go-git v4.7.0+incompatible h1:+W9rgGY4DOKKdX2x6HxSR7HNeTxqiKrOvKnuittYVdA= +github.com/go-git/go-git-fixtures/v4 v4.0.1/go.mod h1:m+ICp2rF3jDhFgEZ/8yziagdT1C+ZpZcrJjappBCDSw= +github.com/go-git/go-git/v5 v5.0.0 h1:k5RWPm4iJwYtfWoxIJy4wJX9ON7ihPeZZYC1fLYDnpg= +github.com/go-git/go-git/v5 v5.0.0/go.mod h1:oYD8y9kWsGINPFJoLdaScGCN6dlKg23blmClfZwtUVA= +github.com/go-kit/kit v0.8.0/go.mod h1:xBxKIO96dXMWWy0MnWVtmwkA9/13aqxPnvrjFYMA2as= +github.com/go-logfmt/logfmt v0.3.0/go.mod h1:Qt1PoO58o5twSAckw1HlFXLmHsOX5/0LbT9GBnD5lWE= +github.com/go-logfmt/logfmt v0.4.0/go.mod h1:3RMwSq7FuexP4Kalkev3ejPJsZTpXXBr9+V4qmtdjCk= +github.com/go-stack/stack v1.8.0/go.mod h1:v0f6uXyyMGvRgIKkXu+yp6POWl0qKG85gN/melR3HDY= +github.com/gogo/protobuf v1.1.1/go.mod h1:r8qH/GZQm5c6nD/R0oafs1akxWv10x8SbQlK7atdtwQ= +github.com/gogo/protobuf v1.2.1/go.mod h1:hp+jE20tsWTFYpLwKvXlhS1hjn+gTNwPg2I6zVXpSg4= +github.com/golang/glog v0.0.0-20160126235308-23def4e6c14b/go.mod h1:SBH7ygxi8pfUlaOkMMuAQtPIUF8ecWP5IEl/CR7VP2Q= +github.com/golang/groupcache v0.0.0-20190129154638-5b532d6fd5ef/go.mod h1:cIg4eruTrX1D+g88fzRXU5OdNfaM+9IcxsU14FzY7Hc= +github.com/golang/mock v1.1.1/go.mod h1:oTYuIxOrZwtPieC+H1uAHpcLFnEyAGVDL/k47Jfbm0A= +github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U= +github.com/golang/protobuf v1.3.1/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U= +github.com/google/btree v1.0.0/go.mod h1:lNA+9X1NB3Zf8V7Ke586lFgjr2dZNuvo3lPJSGZ5JPQ= +github.com/google/go-cmp v0.2.0/go.mod h1:oXzfMopK8JAjlY9xF4vHSVASa0yLyX7SntLO5aqRK0M= +github.com/google/go-cmp v0.3.0/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU= +github.com/google/goterm v0.0.0-20190703233501-fc88cf888a3f/go.mod h1:nOFQdrUlIlx6M6ODdSpBj1NVA+VgLC6kmw60mkw34H4= +github.com/gorilla/websocket v1.4.0/go.mod h1:E7qHFY5m1UJ88s3WnNqhKjPHQ0heANvMoAMk2YaljkQ= +github.com/grpc-ecosystem/go-grpc-middleware v1.0.0/go.mod h1:FiyG127CGDf3tlThmgyCl78X/SZQqEOJBCDaAfeWzPs= +github.com/grpc-ecosystem/go-grpc-prometheus v1.2.0/go.mod h1:8NvIoxWQoOIhqOTXgfV/d3M/q6VIi02HzZEHgUlZvzk= +github.com/grpc-ecosystem/grpc-gateway v1.9.0/go.mod h1:vNeuVxBJEsws4ogUvrchl83t/GYV9WGTSLVdBhOQFDY= +github.com/hashicorp/hcl v1.0.0/go.mod h1:E5yfLk+7swimpb2L/Alb/PJmXilQ/rhwaUYs4T20WEQ= +github.com/inconshreveable/mousetrap v1.0.0/go.mod h1:PxqpIevigyE2G7u3NXJIT2ANytuPF1OarO4DADm73n8= +github.com/jbenet/go-context v0.0.0-20150711004518-d14ea06fba99 h1:BQSFePA1RWJOlocH6Fxy8MmwDt+yVQYULKfN0RoTN8A= +github.com/jbenet/go-context v0.0.0-20150711004518-d14ea06fba99/go.mod h1:1lJo3i6rXxKeerYnT8Nvf0QmHCRC1n8sfWVwXF2Frvo= +github.com/jessevdk/go-flags v1.4.0/go.mod h1:4FA24M0QyGHXBuZZK/XkWh8h0e1EYbRYJSGM75WSRxI= +github.com/jonboulle/clockwork v0.1.0/go.mod h1:Ii8DK3G1RaLaWxj9trq07+26W01tbo22gdxWY5EU2bo= +github.com/juju/ansiterm v0.0.0-20180109212912-720a0952cc2a h1:FaWFmfWdAUKbSCtOU2QjDaorUexogfaMgbipgYATUMU= +github.com/juju/ansiterm v0.0.0-20180109212912-720a0952cc2a/go.mod h1:UJSiEoRfvx3hP73CvoARgeLjaIOjybY9vj8PUPPFGeU= +github.com/julienschmidt/httprouter v1.2.0/go.mod h1:SYymIcj16QtmaHHD7aYtjjsJG7VTCxuUUipMqKk8s4w= +github.com/kevinburke/ssh_config v0.0.0-20190725054713-01f96b0aa0cd h1:Coekwdh0v2wtGp9Gmz1Ze3eVRAWJMLokvN3QjdzCHLY= +github.com/kevinburke/ssh_config v0.0.0-20190725054713-01f96b0aa0cd/go.mod h1:CT57kijsi8u/K/BOFA39wgDQJ9CxiF4nAY/ojJ6r6mM= +github.com/kisielk/errcheck v1.1.0/go.mod h1:EZBBE59ingxPouuu3KfxchcWSUPOHkagtvWXihfKN4Q= +github.com/kisielk/gotool v1.0.0/go.mod h1:XhKaO+MFFWcvkIS/tQcRk01m1F5IRFswLeQ+oQHNcck= +github.com/konsorten/go-windows-terminal-sequences v1.0.1/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ= +github.com/kr/logfmt v0.0.0-20140226030751-b84e30acd515/go.mod h1:+0opPa2QZZtGFBFZlji/RkVcI2GknAs/DXo4wKdlNEc= +github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo= +github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ= +github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI= +github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY= +github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE= +github.com/lucasb-eyer/go-colorful v1.0.3 h1:QIbQXiugsb+q10B+MI+7DI1oQLdmnep86tWFlaaUAac= +github.com/lucasb-eyer/go-colorful v1.0.3/go.mod h1:R4dSotOR9KMtayYi1e77YzuveK+i7ruzyGqttikkLy0= +github.com/lunixbochs/vtclean v0.0.0-20180621232353-2d01aacdc34a h1:weJVJJRzAJBFRlAiJQROKQs8oC9vOxvm4rZmBBk0ONw= +github.com/lunixbochs/vtclean v0.0.0-20180621232353-2d01aacdc34a/go.mod h1:pHhQNgMf3btfWnGBVipUOjRYhoOsdGqdm/+2c2E2WMI= +github.com/magiconair/properties v1.8.0/go.mod h1:PppfXfuXeibc/6YijjN8zIbojt8czPbwD3XqdrwzmxQ= +github.com/manifoldco/promptui v0.7.0 h1:3l11YT8tm9MnwGFQ4kETwkzpAwY2Jt9lCrumCUW4+z4= +github.com/manifoldco/promptui v0.7.0/go.mod h1:n4zTdgP0vr0S3w7/O/g98U+e0gwLScEXGwov2nIKuGQ= +github.com/mattn/go-colorable v0.0.9 h1:UVL0vNpWh04HeJXV0KLcaT7r06gOH2l4OW6ddYRUIY4= +github.com/mattn/go-colorable v0.0.9/go.mod h1:9vuHe8Xs5qXnSaW/c/ABM9alt+Vo+STaOChaDxuIBZU= +github.com/mattn/go-isatty v0.0.4 h1:bnP0vzxcAdeI1zdubAl5PjU6zsERjGZb7raWodagDYs= +github.com/mattn/go-isatty v0.0.4/go.mod h1:M+lRXTBqGeGNdLjl/ufCoiOlB5xdOkqRJdNxMWT7Zi4= +github.com/mattn/go-isatty v0.0.12 h1:wuysRhFDzyxgEmMf5xjvJ2M9dZoWAXNNr5LSBS7uHXY= +github.com/mattn/go-isatty v0.0.12/go.mod h1:cbi8OIDigv2wuxKPP5vlRcQ1OAZbq2CE4Kysco4FUpU= +github.com/mattn/go-runewidth v0.0.9 h1:Lm995f3rfxdpd6TSmuVCHVb/QhupuXlYr8sCI/QdE+0= +github.com/mattn/go-runewidth v0.0.9/go.mod h1:H031xJmbD/WCDINGzjvQ9THkh0rPKHF+m2gUSrubnMI= +github.com/matttproud/golang_protobuf_extensions v1.0.1/go.mod h1:D8He9yQNgCq6Z5Ld7szi9bcBfOoFv/3dc6xSMkL2PC0= +github.com/mitchellh/go-homedir v1.1.0 h1:lukF9ziXFxDFPkA1vsr5zpc1XuPDn/wFntq5mG+4E0Y= +github.com/mitchellh/go-homedir v1.1.0/go.mod h1:SfyaCUpYCn1Vlf4IUYiD9fPX4A5wJrkLzIz1N1q0pr0= +github.com/mitchellh/mapstructure v1.1.2/go.mod h1:FVVH3fgwuzCH5S8UJGiWEs2h04kUh9fWfEaFds41c1Y= +github.com/muesli/termenv v0.7.2 h1:r1raklL3uKE7rOvWgSenmEm2px+dnc33OTisZ8YR1fw= +github.com/muesli/termenv v0.7.2/go.mod h1:ct2L5N2lmix82RaY3bMWwVu/jUFc9Ule0KGDCiKYPh8= +github.com/mwitkow/go-conntrack v0.0.0-20161129095857-cc309e4a2223/go.mod h1:qRWi+5nqEBWmkhHvq77mSJWrCKwh8bxhgT7d/eI7P4U= +github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e h1:fD57ERR4JtEqsWbfPhv4DMiApHyliiK5xCTNVSPiaAs= +github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e/go.mod h1:zD1mROLANZcx1PVRCS0qkT7pwLkGfwJo4zjcN/Tysno= +github.com/oklog/ulid v1.3.1/go.mod h1:CirwcVhetQ6Lv90oh/F+FBtV6XMibvdAFo93nm5qn4U= +github.com/pelletier/go-toml v1.2.0/go.mod h1:5z9KED0ma1S8pY6P1sdut58dfprrGBbd/94hg7ilaic= +github.com/pkg/errors v0.8.0/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= +github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= +github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= +github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= +github.com/prometheus/client_golang v0.9.1/go.mod h1:7SWBe2y4D6OKWSNQJUaRYU/AaXPKyh/dDVn+NZz0KFw= +github.com/prometheus/client_golang v0.9.3/go.mod h1:/TN21ttK/J9q6uSwhBd54HahCDft0ttaMvbicHlPoso= +github.com/prometheus/client_model v0.0.0-20180712105110-5c3871d89910/go.mod h1:MbSGuTsp3dbXC40dX6PRTWyKYBIrTGTE9sqQNg2J8bo= +github.com/prometheus/client_model v0.0.0-20190129233127-fd36f4220a90/go.mod h1:xMI15A0UPsDsEKsMN9yxemIoYk6Tm2C1GtYGdfGttqA= +github.com/prometheus/common v0.0.0-20181113130724-41aa239b4cce/go.mod h1:daVV7qP5qjZbuso7PdcryaAu0sAZbrN9i7WWcTMWvro= +github.com/prometheus/common v0.4.0/go.mod h1:TNfzLD0ON7rHzMJeJkieUDPYmFC7Snx/y86RQel1bk4= +github.com/prometheus/procfs v0.0.0-20181005140218-185b4288413d/go.mod h1:c3At6R/oaqEKCNdg8wHV1ftS6bRYblBhIjjI8uT2IGk= +github.com/prometheus/procfs v0.0.0-20190507164030-5867b95ac084/go.mod h1:TjEm7ze935MbeOT/UhFTIMYKhuLP4wbCsTZCD3I8kEA= +github.com/prometheus/tsdb v0.7.1/go.mod h1:qhTCs0VvXwvX/y3TZrWD7rabWM+ijKTux40TwIPHuXU= +github.com/rogpeppe/fastuuid v0.0.0-20150106093220-6724a57986af/go.mod h1:XWv6SoW27p1b0cqNHllgS5HIMJraePCO15w5zCzIWYg= +github.com/russross/blackfriday/v2 v2.0.1/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM= +github.com/sergi/go-diff v1.1.0 h1:we8PVUC3FE2uYfodKH/nBHMSetSfHDR6scGdBi+erh0= +github.com/sergi/go-diff v1.1.0/go.mod h1:STckp+ISIX8hZLjrqAeVduY0gWCT9IjLuqbuNXdaHfM= +github.com/shurcooL/sanitized_anchor_name v1.0.0/go.mod h1:1NzhyTcUVG4SuEtjjoZeVRXNmyL/1OwPU0+IJeTBvfc= +github.com/sirupsen/logrus v1.2.0/go.mod h1:LxeOpSwHxABJmUn/MG1IvRgCAasNZTLOkJPxbbu5VWo= +github.com/soheilhy/cmux v0.1.4/go.mod h1:IM3LyeVVIOuxMH7sFAkER9+bJ4dT7Ms6E4xg4kGIyLM= +github.com/spaolacci/murmur3 v0.0.0-20180118202830-f09979ecbc72/go.mod h1:JwIasOWyU6f++ZhiEuf87xNszmSA2myDM2Kzu9HwQUA= +github.com/spf13/afero v1.1.2/go.mod h1:j4pytiNVoe2o6bmDsKpLACNPDBIoEAkihy7loJ1B0CQ= +github.com/spf13/cast v1.3.0/go.mod h1:Qx5cxh0v+4UWYiBimWS+eyWzqEqokIECu5etghLkUJE= +github.com/spf13/cobra v1.0.0 h1:6m/oheQuQ13N9ks4hubMG6BnvwOeaJrqSPLahSnczz8= +github.com/spf13/cobra v1.0.0/go.mod h1:/6GTrnGXV9HjY+aR4k0oJ5tcvakLuG6EuKReYlHNrgE= +github.com/spf13/jwalterweatherman v1.0.0/go.mod h1:cQK4TGJAtQXfYWX+Ddv3mKDzgVb68N+wFjFa4jdeBTo= +github.com/spf13/pflag v1.0.3 h1:zPAT6CGy6wXeQ7NtTnaTerfKOsV6V6F8agHXFiazDkg= +github.com/spf13/pflag v1.0.3/go.mod h1:DYY7MBk1bdzusC3SYhjObp+wFpr4gzcvqqNjLnInEg4= +github.com/spf13/viper v1.4.0/go.mod h1:PTJ7Z/lr49W6bUbkmS1V3by4uWynFiR9p7+dSq/yZzE= +github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= +github.com/stretchr/objx v0.1.1/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= +github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs= +github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4= +github.com/tmc/grpc-websocket-proxy v0.0.0-20190109142713-0ad062ec5ee5/go.mod h1:ncp9v5uamzpCO7NfCPTXjqaC+bZgJeR0sMTm6dMHP7U= +github.com/ugorji/go v1.1.4/go.mod h1:uQMGLiO92mf5W77hV/PUCpI3pbzQx3CRekS0kk+RGrc= +github.com/xanzy/ssh-agent v0.2.1 h1:TCbipTQL2JiiCprBWx9frJ2eJlCYT00NmctrHxVAr70= +github.com/xanzy/ssh-agent v0.2.1/go.mod h1:mLlQY/MoOhWBj+gOGMQkOeiEvkx+8pJSI+0Bx9h2kr4= +github.com/xiang90/probing v0.0.0-20190116061207-43a291ad63a2/go.mod h1:UETIi67q53MR2AWcXfiuqkDkRtnGDLqkBTpCHuJHxtU= +github.com/xordataexchange/crypt v0.0.3-0.20170626215501-b2862e3d0a77/go.mod h1:aYKd//L2LvnjZzWKhF00oedf4jCCReLcmhLdhm1A27Q= +go.etcd.io/bbolt v1.3.2/go.mod h1:IbVyRI1SCnLcuJnV2u8VeU0CEYM7e686BmAb1XKL+uU= +go.uber.org/atomic v1.4.0/go.mod h1:gD2HeocX3+yG+ygLZcrzQJaqmWj9AIm7n08wl/qW/PE= +go.uber.org/multierr v1.1.0/go.mod h1:wR5kodmAFQ0UK8QlbwjlSNy0Z68gJhDJUG5sjR94q/0= +go.uber.org/zap v1.10.0/go.mod h1:vwi/ZaCAaUcBkycHslxD9B2zi4UTXhF60s6SWpuDF0Q= +golang.org/x/crypto v0.0.0-20180904163835-0709b304e793/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4= +golang.org/x/crypto v0.0.0-20190219172222-a4c6cb3142f2/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4= +golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= +golang.org/x/crypto v0.0.0-20200302210943-78000ba7a073 h1:xMPOj6Pz6UipU1wXLkrtqpHbR0AVFnyPEQq/wRWz9lM= +golang.org/x/crypto v0.0.0-20200302210943-78000ba7a073/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto= +golang.org/x/crypto v0.0.0-20201012173705-84dcc777aaee h1:4yd7jl+vXjalO5ztz6Vc1VADv+S/80LGJmyl1ROJ2AI= +golang.org/x/crypto v0.0.0-20201012173705-84dcc777aaee/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto= +golang.org/x/lint v0.0.0-20181026193005-c67002cb31c3/go.mod h1:UVdnD1Gm6xHRNCYTkRU2/jEulfH38KcIWyp/GAMgvoE= +golang.org/x/lint v0.0.0-20190313153728-d0100b6bd8b3/go.mod h1:6SW0HCj/g11FgYtHlgUYUwCkIfeOF89ocIRzGO/8vkc= +golang.org/x/net v0.0.0-20180826012351-8a410e7b638d/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= +golang.org/x/net v0.0.0-20181114220301-adae6a3d119a/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= +golang.org/x/net v0.0.0-20181220203305-927f97764cc3/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= +golang.org/x/net v0.0.0-20190311183353-d8887717615a/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= +golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= +golang.org/x/net v0.0.0-20190522155817-f3200d17e092/go.mod h1:HSz+uSET+XFnRR8LxR5pz3Of3rY3CfYBVs4xY44aLks= +golang.org/x/net v0.0.0-20200301022130-244492dfa37a h1:GuSPYbZzB5/dcLNCwLQLsg3obCJtX9IJhpXkvY7kzk0= +golang.org/x/net v0.0.0-20200301022130-244492dfa37a/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= +golang.org/x/oauth2 v0.0.0-20180821212333-d2e6202438be/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U= +golang.org/x/sync v0.0.0-20180314180146-1d60e4601c6f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/sync v0.0.0-20181108010431-42b317875d0f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/sync v0.0.0-20181221193216-37e7f081c4d4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/sys v0.0.0-20180830151530-49385e6e1522/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= +golang.org/x/sys v0.0.0-20180905080454-ebe1bf3edb33/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= +golang.org/x/sys v0.0.0-20181107165924-66b7b1311ac8/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= +golang.org/x/sys v0.0.0-20181116152217-5ac8a444bdc5/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= +golang.org/x/sys v0.0.0-20181122145206-62eef0e2fa9b/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= +golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= +golang.org/x/sys v0.0.0-20190221075227-b4e8571b14e0/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= +golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20200116001909-b77594299b42/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20200302150141-5c8b2ff67527 h1:uYVVQ9WP/Ds2ROhcaGPeIdVq0RIXVLwsHlnvJ+cT1So= +golang.org/x/sys v0.0.0-20200302150141-5c8b2ff67527/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20200916030750-2334cc1a136f/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20201009025420-dfb3f7c4e634 h1:bNEHhJCnrwMKNMmOx3yAynp5vs5/gRy+XWFtZFu7NBM= +golang.org/x/sys v0.0.0-20201009025420-dfb3f7c4e634/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= +golang.org/x/text v0.3.2/go.mod h1:bEr9sfX3Q8Zfm5fL9x+3itogRgK3+ptLWKqgva+5dAk= +golang.org/x/time v0.0.0-20190308202827-9d24e82272b4/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ= +golang.org/x/tools v0.0.0-20180221164845-07fd8470d635/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= +golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= +golang.org/x/tools v0.0.0-20190114222345-bf090417da8b/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= +golang.org/x/tools v0.0.0-20190311212946-11955173bddd/go.mod h1:LCzVGOaR6xXOjkQ3onu1FJEFr0SW1gC7cKk1uF8kGRs= +google.golang.org/appengine v1.1.0/go.mod h1:EbEs0AVv82hx2wNQdGPgUI5lhzA/G0D9YwlJXL52JkM= +google.golang.org/genproto v0.0.0-20180817151627-c66870c02cf8/go.mod h1:JiN7NxoALGmiZfu7CAH4rXhgtRTLTxftemlI0sWmxmc= +google.golang.org/grpc v1.19.0/go.mod h1:mqu4LbDTu4XGKhr4mRzUsmM4RtVoemTSY81AxZiDr8c= +google.golang.org/grpc v1.21.0/go.mod h1:oYelfM1adQP15Ek0mdvEgi9Df8B9CZIaU1084ijfRaM= +gopkg.in/alecthomas/kingpin.v2 v2.2.6/go.mod h1:FMv+mEhP44yOT+4EoQTLFTRgOQ1FBLkstjWtayDeSgw= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/check.v1 v1.0.0-20200227125254-8fa46927fb4f h1:BLraFXnmrev5lT+xlilqcH8XK9/i0At2xKjWk4p6zsU= +gopkg.in/check.v1 v1.0.0-20200227125254-8fa46927fb4f/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/resty.v1 v1.12.0/go.mod h1:mDo4pnntr5jdWRML875a/NmxYqAlA73dVijT2AXvQQo= +gopkg.in/warnings.v0 v0.1.2 h1:wFXVbFY8DY5/xOe1ECiWdKCzZlxgshcYVNkBHstARME= +gopkg.in/warnings.v0 v0.1.2/go.mod h1:jksf8JmL6Qr/oQM2OXTHunEvvTAsrWBLb6OOjuVWRNI= +gopkg.in/yaml.v2 v2.0.0-20170812160011-eb3733d160e7/go.mod h1:JAlM8MvJe8wmxCU4Bli9HhUf9+ttbYbLASfIpnQbh74= +gopkg.in/yaml.v2 v2.2.1/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= +gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= +gopkg.in/yaml.v2 v2.2.4/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= +honnef.co/go/tools v0.0.0-20190102054323-c2f93a96b099/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4= diff --git a/pkg/parser/combinators.go b/pkg/parser/combinators.go new file mode 100644 index 0000000..43029ac --- /dev/null +++ b/pkg/parser/combinators.go @@ -0,0 +1,256 @@ +package parser + +import ( + "fmt" + "regexp" +) + +// see https://medium.com/@armin.heller/using-parser-combinators-in-go-e63b3ad69c94, +// https://github.com/Geal/nom (v5+), and https://bodil.lol/parser-combinators/ + +// TODO: have parsers output a Concrete Syntax Tree (CST). +// IDEA: have parserresult interface be tree-ish: +// type ResultIdea struct { +// Children []ResultIdea +// Kind string // should be Token or symbol, might be empty in case of ignored chars +// Range [2]int // start, end. Empty if start == end. +// Original *string // pointer to the original input +// //? Parent *ResultIdea +// } + +// func (r *ResultIdea) OriginalString() string { +// return (*r.Original)[r.Range[0]:r.Range[1]] +// } +// func (r *ResultIdea) Remaining() []rune { +// return []rune((*r.Original)[r.Range[1]:]) +// } + +// type ParserIdea func( /*input*/ *string, int /*rune start index*/) (ResultIdea, error) + +type ParserResult struct { + Output interface{} + Remaining []rune +} +type ParserCallback func([]rune) interface{} +type Parser func([]rune) (ParserResult, error) +type void struct{} + +func ident(input []rune) interface{} { + return []rune(input) +} + +func LastRuneOf(parser Parser) Parser { + return func(input []rune) (ParserResult, error) { + maxIndex := len(input) - 1 + if maxIndex < 0 { + return parser([]rune{}) + } else { + lastRune := input[maxIndex-1:] + return parser(lastRune) + } + } +} + +func TakeUntil(parser Parser, callback ParserCallback) Parser { + return func(input []rune) (ParserResult, error) { + for i, char := range input { + _, err := parser([]rune{char}) + if err == nil { + return ParserResult{callback(input[:i]), input[i:]}, nil + } + } + return ParserResult{nil, input}, fmt.Errorf("didn't match parser") + } +} + +func Opt(parser Parser) Parser { + return func(input []rune) (ParserResult, error) { + result, _ := parser(input) + return result, nil + } +} + +func LiteralRune(match rune, callback ParserCallback) Parser { + return func(input []rune) (ParserResult, error) { + if len(input) > 0 { + if input[0] == match { + return ParserResult{callback([]rune{match}), input[1:]}, nil + } else { + return ParserResult{nil, input}, fmt.Errorf("%v not matched", match) + } + } else { + return ParserResult{nil, input}, fmt.Errorf("no input") + } + } +} + +func Not(parser Parser) Parser { + return func(input []rune) (ParserResult, error) { + result, err := parser(input) + if err == nil { + return result, nil + } else { + return ParserResult{nil, input}, fmt.Errorf("wasn't expecting to match %v", parser) + } + } +} + +func toRunes(i interface{}) []rune { + switch i.(type) { + case string: + str, _ := i.(string) + return []rune(str) + case rune: + r, _ := i.(rune) + return []rune{r} + case []rune: + runes, _ := i.([]rune) + return runes + default: + panic(fmt.Errorf("%v is not string or []rune", i)) + } +} + +func Tag(tag interface{}) Parser { + toMatch := toRunes(tag) + return func(input []rune) (ParserResult, error) { + if len(toMatch) > len(input) { + return ParserResult{nil, input}, fmt.Errorf("input longer than tag") + } + for i, matching := range toMatch { + if input[i] != matching { + err := fmt.Errorf( + "\"%v\" does not match \"%v\"", + string(input[:i+1]), + string(toMatch), + ) + return ParserResult{nil, input}, err + } + } + return ParserResult{string(toMatch), input[len(toMatch):]}, nil + } +} + +func Any(parsers ...Parser) Parser { + return func(input []rune) (ParserResult, error) { + for _, parser := range parsers { + result, err := parser(input) + if err == nil { + return result, err + } + } + return ParserResult{nil, input}, fmt.Errorf("expected a parser to match") + } +} +func Empty(input []rune) (ParserResult, error) { + if len(input) == 0 { + return ParserResult{nil, input}, nil + } else { + return ParserResult{nil, input}, fmt.Errorf("Not the end") + } +} +func OneOfTheseRunes(str string) Parser { + set := make(map[rune]void) + var present void + for _, char := range []rune(str) { + set[char] = present + } + parsers := make([]Parser, len(set)) + for char := range set { + parsers = append(parsers, LiteralRune(char, ident)) + } + return Any(parsers...) +} + +func asString(result []rune) interface{} { + return string(result) +} +func clone(input []rune) []rune { + cloned := make([]rune, len(input)) + copy(cloned, input) + return cloned +} + +func Sequence(parsers ...Parser) Parser { + return func(input []rune) (ParserResult, error) { + var currentInput = []rune{} + results := make([]interface{}, len(parsers)) + copy(currentInput, input) + for _, parser := range parsers { + result, err := parser(currentInput) + if err != nil { + return ParserResult{nil, input}, err + } else { + currentInput = result.Remaining + results = append(results, result.Output) + } + } + return ParserResult{results, currentInput}, nil + } +} +func Delimeted(start Parser, middle Parser, end Parser) Parser { + return func(input []rune) (ParserResult, error) { + result, err := Sequence(start, middle, end)(input) + if err != nil { + return ParserResult{nil, input}, err + } + results, _ := result.Output.([]string) + return ParserResult{results[1], result.Remaining}, nil + } +} +func Many0(parser Parser) Parser { + return func(input []rune) (ParserResult, error) { + i := 0 + results := make([]interface{}, len(input)) + for i < len(input) { // the highest possible # of times callable + result, err := parser(input[i:]) + if err != nil { + break + } + results = append(results, result.Output) + i += len(result.Remaining) + } + return ParserResult{results, input[i:]}, nil + } +} + +func Many1(parser Parser) Parser { + return func(input []rune) (ParserResult, error) { + result, _ := Many0(parser)(input) + resultArr := result.Output.([]interface{}) + if len(resultArr) == 0 { + return result, fmt.Errorf("no results") + } else { + return result, nil + } + } +} + +func Regex(pattern string) Parser { + re := regexp.MustCompile(`^` + pattern) // should be from the start of the bytes + return func(input []rune) (ParserResult, error) { + b := []byte(string(input)) + result := re.FindIndex(b) //Match(b) + if result == nil { // no match found + return ParserResult{nil, input}, fmt.Errorf("no match for /%s/", pattern) + } else { + // a rune can be multiple bytes, so convert the reult back to runes + startByte := result[0] + endByte := result[1] + endRune := len([]rune(string(b[startByte:endByte]))) + return ParserResult{input[:endRune], input[endRune:]}, nil + } + } +} + +// func Range(start rune, end rune) Parser { +// return Not(Empty)( +// func(input []rune) (ParserResult, error) { +// if input[0] >= start && input[0] <= end { +// // ok +// } else { +// return +// } +// }, +// ) +// } diff --git a/pkg/parser/parser.go b/pkg/parser/parser.go index 07418c6..dcac604 100644 --- a/pkg/parser/parser.go +++ b/pkg/parser/parser.go @@ -1,199 +1,43 @@ package parser -import ( - "fmt" -) - -// see https://medium.com/@armin.heller/using-parser-combinators-in-go-e63b3ad69c94 -// and https://github.com/Geal/nom - -type ParserResult struct { - Output interface{} - Remaining []rune -} -type ParserCallback func([]rune) interface{} -type Parser func([]rune) (ParserResult, error) -type void struct{} - -func ident(input []rune) interface{} { - return []rune(input) -} -func TakeUntil(character rune, callback ParserCallback) Parser { - return func(input []rune) (ParserResult, error) { - for i, nextChar := range input { - if nextChar == character { - output := callback(input[:i]) - return ParserResult{output, input[i:]}, nil - } - } - return ParserResult{nil, input}, fmt.Errorf("didn't match '%v'", character) - } -} - -func Opt(parser Parser) Parser { - return func(input []rune) (ParserResult, error) { - result, _ := parser(input) - return result, nil - } -} - -func LiteralRune(match rune, callback ParserCallback) Parser { - return func(input []rune) (ParserResult, error) { - if len(input) > 0 { - if input[0] == match { - return ParserResult{callback([]rune{match}), input[1:]}, nil - } else { - return ParserResult{nil, input}, fmt.Errorf("%v not matched", match) - } - } else { - return ParserResult{nil, input}, fmt.Errorf("no input") - } - } -} +// import contsants? +// https://www.conventionalcommits.org/en/v1.0.0/#specification -func NotMatching(parser Parser) Parser { - return func(input []rune) (ParserResult, error) { - result, err := parser(input) - if err == nil { - return result, nil - } else { - return ParserResult{nil, input}, fmt.Errorf("wasn't expecting to match %v", parser) - } - } -} - -func toRunes(i interface{}) []rune { - switch i.(type) { - case string: - str, _ := i.(string) - return []rune(str) - case rune: - r, _ := i.(rune) - return []rune{r} - case []rune: - runes, _ := i.([]rune) - return runes - default: - panic(fmt.Errorf("%v is not string or []rune", i)) - } -} +var Newline = Any(LiteralRune('\n', ident), Tag("\r\n")) +var DoubleNewline = Sequence(Newline, Newline) +var ColonSep = Tag(": ") -func Tag(tag interface{}) Parser { - toMatch := toRunes(tag) - return func(input []rune) (ParserResult, error) { - if len(toMatch) > len(input) { - return ParserResult{nil, input}, fmt.Errorf("input longer than tag") - } - for i, matching := range toMatch { - if input[i] != matching { - err := fmt.Errorf( - "\"%v\" does not match \"%v\"", - string(input[:i+1]), - string(toMatch), - ) - return ParserResult{nil, input}, err - } - } - return ParserResult{string(toMatch), input[len(toMatch):]}, nil - } -} +// The key words “MUST”, “MUST NOT”, “REQUIRED”, “SHALL”, “SHALL NOT”, “SHOULD”, “SHOULD NOT”, “RECOMMENDED”, “MAY”, and “OPTIONAL” in this document are to be interpreted as described in RFC 2119. -func Any(parsers ...Parser) Parser { - return func(input []rune) (ParserResult, error) { - for _, parser := range parsers { - result, err := parser(input) - if err == nil { - return result, err - } - } - return ParserResult{nil, input}, fmt.Errorf("expected a parser to match") - } -} +// Commits MUST be prefixed with a type, which consists of a noun, feat, fix, etc., followed by the OPTIONAL scope, OPTIONAL !, and REQUIRED terminal colon and space. +// The type feat MUST be used when a commit adds a new feature to your application or library. +// The type fix MUST be used when a commit represents a bug fix for your application. -func OneOfTheseRunes(str string) Parser { - set := make(map[rune]void) - var present void - for _, char := range []rune(str) { - set[char] = present - } - parsers := make([]Parser, len(set)) - for char := range set { - parsers = append(parsers, LiteralRune(char, ident)) - } - return Any(parsers...) -} +// A description MUST immediately follow the colon and space after the type/scope prefix. The description is a short summary of the code changes, e.g., fix: array parsing issue when multiple spaces were contained in string. -func asString(result []rune) interface{} { - return string(result) -} -func clone(input []rune) []rune { - cloned := []rune{} - copy(cloned, input) - return cloned -} -func Sequence(parsers ...Parser) Parser { - return func(input []rune) (ParserResult, error) { - var currentInput = []rune{} - results := make([]interface{}, len(parsers)) - copy(currentInput, input) - for _, parser := range parsers { - result, err := parser(currentInput) - if err != nil { - return ParserResult{nil, input}, err - } else { - currentInput = result.Remaining - results = append(results, result.Output) - } - } - return ParserResult{results, currentInput}, nil +func CommitType(extraTypes ...string) Parser { + commitTypes := []Parser{Tag("feat"), Tag("fix")} + for _, commitType := range extraTypes { + commitTypes = append(commitTypes, Tag(commitType)) } + return Any(commitTypes...) } -func Delimeted(start Parser, middle Parser, end Parser) Parser { - return func(input []rune) (ParserResult, error) { - result, err := Sequence(start, middle, end)(input) - if err != nil { - return ParserResult{nil, input}, err - } - results, _ := result.Output.([]string) - return ParserResult{results[1], result.Remaining}, nil - } -} +// A scope MAY be provided after a type. A scope MUST consist of a noun describing a section of the codebase surrounded by parenthesis, e.g., fix(parser): +var Scope = Delimeted(Tag('('), TakeUntil(Tag(')'), ident), Tag(')')) +var BreakingChangeBang = Tag("!") -func Many0(parser Parser) Parser { - return func(input []rune) (ParserResult, error) { - i := 0 - results := make([]interface{}, len(input)) - for i < len(input) { // the highest possible # of times callable - result, err := parser(input[i:]) - if err != nil { - break - } - results = append(results, result.Output) - i += len(result.Remaining) - } - return ParserResult{results, input[i:]}, nil - } -} -func Many1(parser Parser) Parser { - return func(input []rune) (ParserResult, error) { - result, _ := Many0(parser)(input) - resultArr := result.Output.([]interface{}) - if len(resultArr) == 0 { - return result, fmt.Errorf("no results") - } else { - return result, nil - } - } -} +// TODO: -> fn vVv -- pass in configured commit types +var CommitTypeContext = Sequence(CommitType( /**/ ), Opt(Scope), Opt(BreakingChangeBang), ColonSep) +var BreakingChange = Tag("BREAKING CHANGE") +var ShortDescription = TakeUntil(Any(Newline, Empty), ident) -var CommitType = Any(Tag("feat"), Tag("fix")) -var Scope = Delimeted( - Tag('('), - NotMatching(Any(OneOfTheseRunes(")\n\r"))), - Tag(')'), +var KebabWord = Regex(`[\w-]+`) +var FooterPrefix = Any( + Sequence(BreakingChange, ColonSep), + Sequence(KebabWord, Any(ColonSep, Tag(" #"))), ) -var BreakingChangeBang = Tag("!") -var CommitTypeContext = Sequence(CommitType, Opt(Scope), Opt(BreakingChangeBang)) -func main() {} +var Body = TakeUntil(Any(Empty, FooterPrefix), ident) +var Footer = TakeUntil(Any(Empty, FooterPrefix), ident) +var Footers = Many0(Footer) diff --git a/pkg/parser/parser_test.go b/pkg/parser/parser_test.go index b6f5440..ea3085b 100644 --- a/pkg/parser/parser_test.go +++ b/pkg/parser/parser_test.go @@ -11,7 +11,7 @@ func TestParsingCommitTypes(t *testing.T) { expected string, remainder string, ) func(t *testing.T) { - result, err := CommitType([]rune(input)) + result, err := CommitType()([]rune(input)) return func(t *testing.T) { if err != nil { fmt.Printf("%v", err) @@ -47,7 +47,7 @@ func TestParsingCommitTypes(t *testing.T) { testExpectedMatch("fixing", "fix", "ing"), ) t.Run("rejects invalid commit types", func(t *testing.T) { - _, err := CommitType([]rune("foo")) + _, err := CommitType()([]rune("foo")) if err == nil { t.Fail() } @@ -58,11 +58,10 @@ func TestTakeUntil(t *testing.T) { var callback = func(chars []rune) interface{} { return string(chars) } - var test = func(input string, until rune, output string, remaining string) func(t *testing.T) { + var test = func(input string, until Parser, output string, remaining string) func(t *testing.T) { return func(t *testing.T) { result, err := TakeUntil(until, callback)([]rune(input)) if err != nil { - fmt.Println("err != nil") t.Fail() } if result.Output != output { @@ -77,14 +76,41 @@ func TestTakeUntil(t *testing.T) { } t.Run( "matching in the middle of the input works", - test("abcdef", 'c' /* output: */, "ab" /* remaining: */, "cdef"), + test("abcdef", LiteralRune('c', callback) /* output: */, "ab" /* remaining: */, "cdef"), ) t.Run( "matching at the start of the input works", - test("abcdef", 'a' /* output: */, "" /* remaining: */, "abcdef"), + test("abcdef", LiteralRune('a', callback) /* output: */, "" /* remaining: */, "abcdef"), ) t.Run( "matching at the end of the input works", - test("abcdef", 'f' /* output: */, "abcde" /* remaining: */, "f"), + test("abcdef", LiteralRune('f', callback) /* output: */, "abcde" /* remaining: */, "f"), ) } + +// these test cases are copied from https://www.conventionalcommits.org/en/v1.0.0/, which is licensed under +// CC-BY 3.0 (https://creativecommons.org/licenses/by/3.0/). I've added escape sequences to record then as go multiline string literals. +var validCCwithBreakingChangeFooter = `feat: allow provided config object to extend other configs + +BREAKING CHANGE: ` + "`extends`" + ` key in config file is now used for extending other config files +` +var validCCWithBreakingChangeBang = `refactor!: drop support for Node 6` +var validCCwithBothBreakingChangeBangAndFooter = `refactor!: drop support for Node 6 + +BREAKING CHANGE: refactor to use JavaScript features not available in Node 6. + +` +var validCCWithOnlyDescription = `docs: correct spelling of CHANGELOG` +var validCCWithScope = `feat(lang): add polish language` +var validCCWithFooters = `fix: correct minor typos in code + +see the issue for details + +on typos fixed. + +Reviewed-by: Z +Refs #133` + +var validCCreversion = `revert: let us never again speak of the noodle incident + +Refs: 676104e, a215868`