Skip to content

Commit

Permalink
Update examples.
Browse files Browse the repository at this point in the history
  • Loading branch information
I3oris committed Jun 10, 2024
1 parent 3443f4f commit 4665ded
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 43 deletions.
38 changes: 13 additions & 25 deletions examples/json.cr
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,13 @@ class JSONParser < TopDown::Parser
syntax :int do
capture do
# Sign:
maybe { parse('-') }
maybe('-')

# Entire part:
union do
parse('0')
parse(:digit1_9) do
repeat { parse(:digit) }
repeat(:digit)
end
end
end.to_i
Expand All @@ -38,15 +38,13 @@ class JSONParser < TopDown::Parser

# Decimal part:
parse!('.')
parse!(:digit)
repeat { parse :digit }
repeat_n(1.., :digit)

# Exponent:
maybe do
parse('e' | 'E')
parse!('-' | '+')
parse!(:digit)
repeat { parse(:digit) }
repeat_n(1.., :digit)
end
end
end.to_f
Expand Down Expand Up @@ -77,23 +75,18 @@ class JSONParser < TopDown::Parser
parse('t') { '\t' }
parse('u') do
code = capture do
4.times { parse! :hexdigit }
repeat_n(4, :hexdigit)
end
code.to_i(16).chr
end
raise_syntax_error "Invalid escape sequence: '\\#{peek_char}'"
end
end

syntax :object, '{' do
obj = {} of String => Value

repeat(separator: ',') do
key, value = parse(:key_value)
obj[key] = value
syntax :object, '{', end!: '}' do
repeat_to_h(Hash(String, Value), separator: ',') do
parse(:key_value)
end
parse!('}')
obj
end

syntax :key_value do
Expand All @@ -116,15 +109,10 @@ class JSONParser < TopDown::Parser
end
end

syntax :array, '[' do
values = [] of Value

repeat(separator: ',') do
values << parse(:value)
syntax :array, '[', end!: ']' do
repeat_to_a(Array(Value), separator: ',') do
parse(:value)
end
parse!(']')

values
end

# # Skip ##
Expand All @@ -135,11 +123,11 @@ class JSONParser < TopDown::Parser
parse('\t')
parse('\r')
# # Line comments:
# parse("//") { repeat { parse(not('\n')) } }
# parse("//") { repeat(not('\n')) }

# # Block comments:
# parse("/*") do
# repeat { parse(not("*/")) }
# repeat(not("*/"))
# parse("*/")
# end
end
Expand Down
25 changes: 7 additions & 18 deletions examples/json_with_token.cr
Original file line number Diff line number Diff line change
Expand Up @@ -97,22 +97,16 @@ class JSONParserWithToken < TopDown::Parser

# # Syntax ##

syntax :object, ["{"] do
obj = {} of String => Value

repeat separator: [","] do
key, value = parse(:key_value)
obj[key] = value
syntax :object, ["{"], end!: ["}"] do
repeat_to_h(Hash(String, Value), separator: [","]) do
parse(:key_value)
end

parse! ["}"]
obj
end

syntax :key_value do
key = parse ["string"]
parse! [":"]
value = parse! :value
value = parse!(:value)

{key, value}
end
Expand All @@ -129,15 +123,10 @@ class JSONParserWithToken < TopDown::Parser
end
end

syntax :array, ["["] do
values = [] of Value

repeat separator: [","] do
values << parse :value
syntax :array, ["["], end!: ["]"] do
repeat_to_a Array(Value), separator: [","] do
parse(:value)
end
parse! ["]"]

values
end

# # Skip ##
Expand Down

0 comments on commit 4665ded

Please sign in to comment.