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

Handrolled Parser #729

Merged
merged 2 commits into from
Jan 16, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion .gitattributes
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
ext/json/ext/parser/parser.c linguist-generated=true
java/src/json/ext/Parser.java linguist-generated=true
52 changes: 2 additions & 50 deletions Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ end rescue nil
EXT_ROOT_DIR = 'ext/json/ext'
EXT_PARSER_DIR = "#{EXT_ROOT_DIR}/parser"
EXT_PARSER_DL = "#{EXT_PARSER_DIR}/parser.#{CONFIG['DLEXT']}"
RAGEL_PATH = "#{EXT_PARSER_DIR}/parser.rl"
EXT_PARSER_SRC = "#{EXT_PARSER_DIR}/parser.c"
EXT_GENERATOR_DIR = "#{EXT_ROOT_DIR}/generator"
EXT_GENERATOR_DL = "#{EXT_GENERATOR_DIR}/generator.#{CONFIG['DLEXT']}"
Expand Down Expand Up @@ -204,9 +203,9 @@ if defined?(RUBY_ENGINE) and RUBY_ENGINE == 'jruby'
else
desc "Compiling extension"
if RUBY_ENGINE == 'truffleruby'
task :compile => [ :ragel, EXT_PARSER_DL ]
task :compile => [ EXT_PARSER_DL ]
else
task :compile => [ :ragel, EXT_PARSER_DL, EXT_GENERATOR_DL ]
task :compile => [ EXT_PARSER_DL, EXT_GENERATOR_DL ]
end

UndocumentedTestTask.new do |t|
Expand Down Expand Up @@ -234,53 +233,6 @@ else
system 'ctags', *Dir['**/*.{rb,c,h,java}']
end

file EXT_PARSER_SRC => RAGEL_PATH do
cd EXT_PARSER_DIR do
if RAGEL_CODEGEN == 'ragel'
sh "ragel parser.rl -G2 -o parser.c"
else
sh "ragel -x parser.rl | #{RAGEL_CODEGEN} -G2"
end
src = File.read("parser.c").gsub(/[ \t]+$/, '')
src.gsub!(/^static const int (JSON_.*=.*);$/, 'enum {\1};')
src.gsub!(/^(static const char) (_JSON(?:_\w+)?_nfa_\w+)(?=\[\] =)/, '\1 MAYBE_UNUSED(\2)')
src.gsub!(/0 <= ([\( ]+\*[\( ]*p\)+) && \1 <= 31/, "0 <= (signed char)(*(p)) && (*(p)) <= 31")
src[0, 0] = "/* This file is automatically generated from parser.rl by using ragel */"
File.open("parser.c", "w") {|f| f.print src}
end
end

desc "Generate diagrams of ragel parser (ps)"
task :ragel_dot_ps do
root = 'diagrams'
specs = []
File.new(RAGEL_PATH).grep(/^\s*machine\s*(\S+);\s*$/) { specs << $1 }
for s in specs
if RAGEL_DOTGEN == 'ragel'
sh "ragel #{RAGEL_PATH} -S#{s} -p -V | dot -Tps -o#{root}/#{s}.ps"
else
sh "ragel -x #{RAGEL_PATH} -S#{s} | #{RAGEL_DOTGEN} -p|dot -Tps -o#{root}/#{s}.ps"
end
end
end

desc "Generate diagrams of ragel parser (png)"
task :ragel_dot_png do
root = 'diagrams'
specs = []
File.new(RAGEL_PATH).grep(/^\s*machine\s*(\S+);\s*$/) { specs << $1 }
for s in specs
if RAGEL_DOTGEN == 'ragel'
sh "ragel #{RAGEL_PATH} -S#{s} -p -V | dot -Tpng -o#{root}/#{s}.png"
else
sh "ragel -x #{RAGEL_PATH} -S#{s} | #{RAGEL_DOTGEN} -p|dot -Tpng -o#{root}/#{s}.png"
end
end
end

desc "Generate diagrams of ragel parser"
task :ragel_dot => [ :ragel_dot_png, :ragel_dot_ps ]

desc "Create the gem packages"
task :package do
sh "gem build json.gemspec"
Expand Down
Loading
Loading