Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: parser state machine #4

Merged
merged 25 commits into from
Aug 7, 2024
Merged

feat: parser state machine #4

merged 25 commits into from
Aug 7, 2024

Conversation

Autoparallel
Copy link
Contributor

@Autoparallel Autoparallel commented Aug 5, 2024

  • Switch template
  • Parser state machine that parses the input data and lays down a list of "instructions" that label what state transitions should be taken by the machine at a given byte index of that data. E.g., if we hit an open brace when we are not inside of a value or inside of a key, we should increase tree depth.

Alright the above two tasks are pretty much done for simpler JSON. Where I am at with this now is I have created a few circuits. They are in circuits.json which circomkit uses to do some automations with them! I found this quite helpful.

What I was working with in particular was test_extract which digests the json_examples/test.json. To try this out, run:

npx circomkit compile test_extract
npx circomkit witness test_extract input

and you should see some logs appear (note, follow README instructions to get circomkit up and running). These logs track the state of the Parser component you'll find in circuits/extract.circom and circuits/parser.circom.

I left notes in circuits/parser.circom that walk through expected states we see given what we have just read at a given byte index. You can validate yourself by clicking through test.json.

Nonetheless, you should get code that verifies this witness meaning that, in some sense, it is valid JSON! Also, we have marked where the keys are by enabling the inside_key bit and marked where values are by enabling the inside_value bit.

Output would look like this 😄:

❯ npx circomkit witness test_extract input
State[ 1 ].tree_depth =  1
State[ 1 ].parsing_to_key =  1
State[ 1 ].inside_key =  0
State[ 1 ].parsing_to_value =  0
State[ 1 ].inside_value =  0
State[ 1 ].end_of_kv =  0
---
State[ 2 ].tree_depth =  1
State[ 2 ].parsing_to_key =  1
State[ 2 ].inside_key =  0
State[ 2 ].parsing_to_value =  0
State[ 2 ].inside_value =  0
State[ 2 ].end_of_kv =  0
---
State[ 3 ].tree_depth =  1
State[ 3 ].parsing_to_key =  1
State[ 3 ].inside_key =  0
State[ 3 ].parsing_to_value =  0
State[ 3 ].inside_value =  0
State[ 3 ].end_of_kv =  0
---
State[ 4 ].tree_depth =  1
State[ 4 ].parsing_to_key =  1
State[ 4 ].inside_key =  0
State[ 4 ].parsing_to_value =  0
State[ 4 ].inside_value =  0
State[ 4 ].end_of_kv =  0
---
State[ 5 ].tree_depth =  1
State[ 5 ].parsing_to_key =  1
State[ 5 ].inside_key =  0
State[ 5 ].parsing_to_value =  0
State[ 5 ].inside_value =  0
State[ 5 ].end_of_kv =  0
---
State[ 6 ].tree_depth =  1
State[ 6 ].parsing_to_key =  1
State[ 6 ].inside_key =  0
State[ 6 ].parsing_to_value =  0
State[ 6 ].inside_value =  0
State[ 6 ].end_of_kv =  0
---
State[ 7 ].tree_depth =  1
State[ 7 ].parsing_to_key =  0
State[ 7 ].inside_key =  1
State[ 7 ].parsing_to_value =  0
State[ 7 ].inside_value =  0
State[ 7 ].end_of_kv =  0
---
State[ 8 ].tree_depth =  1
State[ 8 ].parsing_to_key =  0
State[ 8 ].inside_key =  1
State[ 8 ].parsing_to_value =  0
State[ 8 ].inside_value =  0
State[ 8 ].end_of_kv =  0
---
State[ 9 ].tree_depth =  1
State[ 9 ].parsing_to_key =  0
State[ 9 ].inside_key =  1
State[ 9 ].parsing_to_value =  0
State[ 9 ].inside_value =  0
State[ 9 ].end_of_kv =  0
---
State[ 10 ].tree_depth =  1
State[ 10 ].parsing_to_key =  0
State[ 10 ].inside_key =  1
State[ 10 ].parsing_to_value =  0
State[ 10 ].inside_value =  0
State[ 10 ].end_of_kv =  0
---
State[ 11 ].tree_depth =  1
State[ 11 ].parsing_to_key =  0
State[ 11 ].inside_key =  1
State[ 11 ].parsing_to_value =  0
State[ 11 ].inside_value =  0
State[ 11 ].end_of_kv =  0
---
State[ 12 ].tree_depth =  1
State[ 12 ].parsing_to_key =  0
State[ 12 ].inside_key =  0
State[ 12 ].parsing_to_value =  0
State[ 12 ].inside_value =  0
State[ 12 ].end_of_kv =  0
---
State[ 13 ].tree_depth =  1
State[ 13 ].parsing_to_key =  0
State[ 13 ].inside_key =  0
State[ 13 ].parsing_to_value =  1
State[ 13 ].inside_value =  0
State[ 13 ].end_of_kv =  1
---
State[ 14 ].tree_depth =  1
State[ 14 ].parsing_to_key =  0
State[ 14 ].inside_key =  0
State[ 14 ].parsing_to_value =  1
State[ 14 ].inside_value =  0
State[ 14 ].end_of_kv =  0
---
State[ 15 ].tree_depth =  1
State[ 15 ].parsing_to_key =  0
State[ 15 ].inside_key =  0
State[ 15 ].parsing_to_value =  0
State[ 15 ].inside_value =  1
State[ 15 ].end_of_kv =  0
---
State[ 16 ].tree_depth =  1
State[ 16 ].parsing_to_key =  0
State[ 16 ].inside_key =  0
State[ 16 ].parsing_to_value =  0
State[ 16 ].inside_value =  1
State[ 16 ].end_of_kv =  0
---
State[ 17 ].tree_depth =  1
State[ 17 ].parsing_to_key =  0
State[ 17 ].inside_key =  0
State[ 17 ].parsing_to_value =  0
State[ 17 ].inside_value =  1
State[ 17 ].end_of_kv =  0
---
State[ 18 ].tree_depth =  1
State[ 18 ].parsing_to_key =  0
State[ 18 ].inside_key =  0
State[ 18 ].parsing_to_value =  0
State[ 18 ].inside_value =  1
State[ 18 ].end_of_kv =  0
---
State[ 19 ].tree_depth =  1
State[ 19 ].parsing_to_key =  0
State[ 19 ].inside_key =  0
State[ 19 ].parsing_to_value =  0
State[ 19 ].inside_value =  0
State[ 19 ].end_of_kv =  0
---
State[ 20 ].tree_depth =  1
State[ 20 ].parsing_to_key =  0
State[ 20 ].inside_key =  0
State[ 20 ].parsing_to_value =  0
State[ 20 ].inside_value =  0
State[ 20 ].end_of_kv =  1
---
Witness created: ./build/test_extract/input/witness.wtns

There are next steps. So I have other JSON there to test the limits of this. Also, I should write some tests on valid state transitions for the parser. I did not do this yet. Please hold me accountable and make sure I make issues for these things.

Signing off for now. Gn.

@Autoparallel Autoparallel marked this pull request as draft August 5, 2024 22:01
@Autoparallel Autoparallel changed the title (WIP) state machine feat: parser state machine Aug 6, 2024
@Autoparallel Autoparallel marked this pull request as ready for review August 6, 2024 22:58
Copy link

@thor314 thor314 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

appreciate the comments, looks like a good testin flow

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nice, looks like a good test flow

pragma circom 2.1.9;

include "operators.circom";
/*
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

good notes

@Autoparallel
Copy link
Contributor Author

@thor314 thank ser

@Autoparallel Autoparallel merged commit 274d15a into main Aug 7, 2024
1 check passed
@Autoparallel Autoparallel deleted the feat/parser branch August 7, 2024 22:00
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants