-
Notifications
You must be signed in to change notification settings - Fork 6
Add JSON parser example #391
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
Merged
Merged
Changes from 14 commits
Commits
Show all changes
19 commits
Select commit
Hold shift + click to select a range
17c2e24
Handle string escapes properly; handle exponents
jimbxb 912b11d
Refined JSON example with documentation and name improvements
jimbxb f6aef33
Refined the defintion of space
jimbxb f0da735
uplift PR with new changes
jimbxb 399b679
Use a resource in JSON parser sample after various bug fixes
jimbxb ec6b059
Naming things
jimbxb 6ea7cc5
Clean up IO in sample
jimbxb 876a82f
handle aliases of globals properly; remove sample exe
jimbxb ec2f54d
Organise json sample; address comments
jimbxb 2ac93e4
Fix string prepend; name digits_ more clearly
jimbxb 4e58e54
Require ! for rsourceful HO calls
jimbxb 923adf3
Refactor digits into a loop
jimbxb 36e8f27
Newline EOF; casing
jimbxb a4f35cb
remove imports
jimbxb 1f08449
Better document samples/json; add more tests to samples/json
jimbxb eb1d8df
Align formatting of brainfuck with json
jimbxb e7df473
$ for consistency
jimbxb 199360d
Remove unnecessary type constraint; grammar
jimbxb 9a5f3e2
Make pair type generic; naming things
jimbxb File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,282 @@ | ||
pub constructors jobject(list(pair)) | ||
| jstring(string) | ||
| jbool(bool) | ||
| jnumber(float) | ||
| jlist(list(_)) | ||
| jnull | ||
|
||
pub def {test} (a:_ = b:_) { | ||
foreign lpvm cast(a):int = foreign lpvm cast(b):int | ||
} | ||
|
||
pub type pair { | ||
## pair type | ||
# A pair contianing a key and value. | ||
pub pair(key:string, value:json) | ||
} | ||
|
||
resource tokens:string | ||
|
||
## parse/2 | ||
# Parse a string into a JSON value. | ||
# Fails if the string cannot be parsed. | ||
pub def {test} parse(tokens:string, ?json:_) { | ||
use tokens in { | ||
!space | ||
!json(?json) | ||
} | ||
} | ||
|
||
## char/2 | ||
# Parse a single character from the input string. | ||
def {test} char(?c:char) use !tokens { | ||
tokens = [?c | ?tokens] | ||
} | ||
|
||
## literal/2 | ||
# Parse a character literal followed by whitespace. | ||
def {test} literal(c:char) use !tokens { | ||
!char(c) | ||
!space | ||
} | ||
|
||
## literal/2 | ||
# Parse a string literal followed by whitespace. | ||
def {test} literal(str:string) use !tokens { | ||
for ?c in str { | ||
!char(c) | ||
} | ||
!space | ||
} | ||
|
||
## space/1 | ||
# Parse zero or more whitespace characters from the input. | ||
def space() use !tokens { | ||
!char(?c) | ||
(c = ' ' | c = '\n' | c = '\r' | c = '\t') | ||
!space | ||
| pass | ||
} | ||
|
||
## json/1 | ||
# Parse a JSON object followed by whitespace. | ||
def {test} json(?j:_) use !tokens { | ||
!object(?j) | ||
| !list(?j) | ||
| !string(?j) | ||
| !bool(?j) | ||
| !number(?j) | ||
| !null(?j) | ||
} | ||
|
||
## object/1 | ||
# Parse a JSON object, followed by whitespace. | ||
def {test} object(?obj:_) use !tokens { | ||
!sequence('{', {test,resource}{ | ||
!string(?key) | ||
!literal(':') | ||
!json(?value) | ||
?@ = pair(key, value) | ||
}, '}', ?pairs) | ||
?obj = jobject(pairs) | ||
} | ||
|
||
## list/1 | ||
# Parse a JSON list, followed by whitespace. | ||
def {test} list(?list:_) use !tokens { | ||
!sequence('[', json, ']', ?jsons) | ||
?list = jlist(jsons) | ||
} | ||
|
||
## string/1 | ||
# Parse a JSON string, followed by whitespace. | ||
def {test} string(?string:_) use !tokens { | ||
!string(?str) | ||
?string = jstring(str) | ||
} | ||
|
||
## number/1 | ||
# Parse a JSON number, followed by whitespace. | ||
def {test} number(?number:_) use !tokens { | ||
!digits(?n, _) | ||
if { !char('.') :: | ||
!digits(?f, ?len) | ||
!n += f / power(10.0, len) | ||
} | ||
if { { !char('e') | !char('E') } :: | ||
!digits(?e, _) | ||
!n *= power(10.0, e) | ||
} | ||
!space | ||
?number = jnumber(n) | ||
} | ||
|
||
## bool/1 | ||
# Parse a JSON Boolean, followed by whitespace. | ||
def {test} bool(?bool:_) use !tokens { | ||
!literal("false") | ||
?bool = jbool(false) | ||
| !literal("true") | ||
?bool = jbool(true) | ||
} | ||
|
||
## null/1 | ||
# Parse a JSON null, followed by whitespace. | ||
def {test} null(?null:_) use !tokens { | ||
!literal("null") | ||
?null = jnull | ||
} | ||
jimbxb marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
## sequence/4 | ||
# Parse a comma-separated sequence of things, with a preceding and | ||
# following character, followed by whitespace. | ||
def {test} sequence(before:char, parse_item:{resource,test}(?X), after:char, ?xs:list(X)) use !tokens { | ||
!literal(before) | ||
if { !parse_item(?x) :: | ||
!sequence_tail(parse_item, ?xs) | ||
?xs = [x | xs] | ||
| else :: | ||
?xs = [] | ||
} | ||
!literal(after) | ||
} | ||
jimbxb marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
## sequence_tail/2 | ||
# Parse the tail of a sequnece. Helper for sequence/4. | ||
def {test} sequence_tail(parse_item:{resource,test}(?X), ?xs:list(X)) use !tokens { | ||
if { !literal(',') :: | ||
!parse_item(?x) | ||
!sequence_tail(parse_item, ?xs) | ||
?xs = [x | xs] | ||
| else :: | ||
?xs = [] | ||
} | ||
} | ||
|
||
## string/1 | ||
# Parse a string, followed by whitespace. | ||
def {test} string(?str:string) use !tokens { | ||
!char('\"') | ||
!string_tail(?str) | ||
?str = string(c_string(str)) | ||
} | ||
|
||
## string_tail/1 | ||
# Parse the tail of a string. Helper for string/1. | ||
def {test} string_tail(?str:string) use !tokens { | ||
!literal('\"') | ||
?str = "" | ||
| !char(?c) | ||
if { c:char = '\\' :: | ||
!char(?c) | ||
if { c = '"' :: pass | ||
| c = '\\' :: pass | ||
| c = '/' :: pass | ||
| c = 'b' :: ?c = '\b' | ||
| c = 'f' :: ?c = '\f' | ||
| c = 'n' :: ?c = '\n' | ||
| c = 'r' :: ?c = '\r' | ||
| c = 't' :: ?c = '\t' | ||
| else :: fail | ||
} | ||
} | ||
!string_tail(?str) | ||
?str = c ,, str | ||
} | ||
|
||
## digits/2 | ||
# Parse a sequence of one or more digits | ||
def {test} digits(?n:float, ?len:float) use !tokens { | ||
!digit(?n) | ||
?len = 1.0 | ||
do { | ||
!digit(?d) | ||
!len += 1.0 | ||
?n = n * 10.0 + d | ||
| break | ||
} | ||
} | ||
|
||
## digits/1 | ||
# Parse a single digit | ||
def {test} digit(?d:float) use !tokens { | ||
!char(?c) | ||
'0' <= c & c <= '9' | ||
?d = foreign llvm sitofp(ord(c) - ord('0'):int) | ||
} | ||
|
||
pub def print(x:_) use !io { | ||
!print(0, x) | ||
!nl | ||
} | ||
|
||
def print(ind:int, x:_) use !io { | ||
case x in { | ||
jobject(?pairs) :: | ||
!print_list('{', {resource}{ | ||
!indent(@1) | ||
!escape(@2^key) | ||
!print(": ") | ||
!print(@1, @2^value) | ||
}, ind, pairs, '}') | ||
| jlist(?list) :: | ||
!print_list('[', {resource}{ | ||
!indent(@1) | ||
!print(@1, @2) | ||
}, ind, list, ']') | ||
| jstring(?s) :: !escape(s) | ||
| jbool(?b) :: !print(b) | ||
| jnumber(?n) :: !print(n) | ||
| else :: !print("null") | ||
} | ||
} | ||
|
||
def indent(ind:int) use !io { | ||
ind <= 0 | ||
| !print(" ") | ||
!indent(ind - 1) | ||
} | ||
|
||
def escape(s:string) use !io { | ||
!print('"') | ||
for ?c in s { | ||
if { c = '"' :: !print("\\\"") | ||
| c = '\\' :: !print("\\\\") | ||
| c = '\b' :: !print("\\b") | ||
| c = '\f' :: !print("\\f") | ||
| c = '\n' :: !print("\\n") | ||
| c = '\r' :: !print("\\r") | ||
| c = '\t' :: !print("\\t") | ||
| else :: !print(c) | ||
} | ||
} | ||
!print('"') | ||
} | ||
|
||
def print_list(start:char, printer:{resource}(int, X), ind:int, xs:list(X), end:char) use !io { | ||
!print(start) | ||
if { [?x | ?xs] = xs :: | ||
!nl | ||
!printer(ind + 1, x) | ||
for ?x in xs { | ||
!println(',') | ||
!printer(ind + 1, x) | ||
} | ||
!nl | ||
!indent(ind) | ||
} | ||
!print(end) | ||
} | ||
|
||
?str = " {\"a\": [1, 1.020, 1e2, 01.2E3, false, { \"key\": null}],\n \"abc\\ndef\": true } " | ||
|
||
!print("Attempting to parse ") | ||
!escape(str) | ||
!nl | ||
|
||
if { parse(str, ?json) :: | ||
!println("Successfully parsed!") | ||
!print(json) | ||
| else :: | ||
!print("Failed to parse :(") | ||
} |
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
Oops, something went wrong.
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.