This repository was archived by the owner on Sep 10, 2025. It is now read-only.
Added a lexer/parser/evaluator - rather than parsing via strings.Split#3
Merged
Added a lexer/parser/evaluator - rather than parsing via strings.Split#3
strings.Split#3Conversation
The lexer processes input from a string, and returns a series of
tokens:
* Single-line comments, beginning with "#" are skipped.
* So is any shebang line - this is redundent.
* Whitespace is swallowed and strings are handled appropriately.
* The following all work as expected:
* "\t"
* "\n"
* "Embedded quote: \"."
This covers: * The lexing. * The reason for why we can't just evaluate the series of tokens from the lexer directly. TODO: * Parse the damn tokens into statements, and then execute them.
This commit removes our implementation; at this point we have a lexer that appears to work - at least the test-cases pass - but nothing using it. TODO: * Add a some sub-command stub to test the lexer.
Updated `HACKING.md` to describe how I'm going to parse; specifically that we'll recycle the token-types in `token/token.go`. Should be easy, but first tea :)
I've added the sub-command framework I'm most familiar with, and
now I can successfully lex our example recipe:
frodo ~/go/src/github.com/skx/deployr $ ./deployr lex ./example.recipe
{Set Set}
{IDENT greeting}
{STRING Hello, world!}
{CopyTemplate CopyTemplate}
{IDENT example.recipe.template}
{IDENT /tmp/blah}
{Run Run}
{STRING cat /tmp/blah}
{IfChanged IfChanged}
{STRING sha1sum /tmp/blah}
{EOF }
TODO:
* Write the parser.
* Test the parser.
* Use the parser.
Random-testing of our lexer to dump broken-programs revealed
the fact that we silently closed open-strings at EOF. This is
friendly and helpful, but wrong.
Correctly return an error for this input:
Run "test
Error is "Unterminated string!" which is nice and descriptive.
The parser is driven by the new `parse` sub-command, which shows
parsing good/bad programs like so:
frodo ~/go/src/github.com/skx/deployr $ ./deployr parse ./example.recipe
{{Set Set} [{IDENT greeting} {STRING Hello, world!}]}
{{CopyTemplate CopyTemplate} [{IDENT example.recipe.template} {IDENT /tmp/blah}]}
{{Run Run} [{STRING cat /tmp/blah}]}
{{IfChanged IfChanged} [{STRING sha1sum /tmp/blah}]}
frodo ~/go/src/github.com/skx/deployr $ ./deployr parse ./t.in
Error parsing program: Error retrieceved from the lexer - Unterminated string!
As expected the parsed-results are just an array of "Statements" where
statements hold a single token as the thing to invoke, and an array
of (up to) two potential arguments.
This catches the case of:
echo '"Test"' > t.in
./deployr parse t.in
We can now run our example! First run shows both commands
work, as expected:
frodo ~/go/src/github.com/skx/deployr $ ./deployr run --target=master.steve.org.uk:2222 ./example.recipe
This file was deployed to master.steve.org.uk by deployr!
Here you see a variable being used which was defined in the recipe itself:
* Hello, world!
5649664271eb6d14e7073ca98cf1dffa4ec1e5d0 /tmp/blah
Second run results in no change, so the "IfChanged" execution
doens't run:
frodo ~/go/src/github.com/skx/deployr $ ./deployr run --target=master.steve.org.uk:2222 ./example.recipe
This file was deployed to master.steve.org.uk by deployr!
Here you see a variable being used which was defined in the recipe itself:
* Hello, world!
Expand "${foo}" to the content of the variable "foo", in all
our functions.
This should be "Run foo .. bar":
Run "foo \
bar";
If you want a newline you should add one. This might surprise
coders, but if we pretend we're bash-scripts it makes more sense.
Owner
Author
|
I believe this is ready now! Some epic-changes, which have really rewritten the application from scratch:
We have some tests, but need more, and my existing recipes work, however there are some breaking changes:
For example here's a diff: |
Closed
Owner
Author
|
TODO:
|
strings.Split
Thanks "go vet" :)
Also moved variable-discussion into its own section.
Now we can add a test for it.
This is a good thing :)
This will allow us to mock our lexer for tests.
Owner
Author
|
If I can get my 100% test coverage of the parser then I'll merge this. Spent the past few hours asleep, and I'm feeling optimistic :) |
A good day.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to subscribe to this conversation on GitHub.
Already have an account?
Sign in.
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This pull-request updates the codebase to make it more robust, although it is still work-in-progress.
The intention is:
We probably don't need an AST or full evaluation environment, because we've zero control-flow related primitives (
if,do/while,for,goto, etc). It might be that in the future we'll add some simple built-in functions for fetching github-release locations, etc, but I think that's not really required.To test the code I'll also move towards using sub-commands appropriately: