Skip to content

Commit

Permalink
Revise logging to use standard RDF log facility.
Browse files Browse the repository at this point in the history
  • Loading branch information
gkellogg committed Nov 27, 2024
1 parent 27dee06 commit bbc6fe2
Show file tree
Hide file tree
Showing 9 changed files with 46 additions and 44 deletions.
7 changes: 6 additions & 1 deletion bin/ebnf
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ $:.unshift(File.expand_path(File.join(File.dirname(__FILE__), "..", 'lib')))
require 'rubygems'
require 'getoptlong'
require 'ebnf'
require 'rdf/spec'

options = {
output_format: :sxp,
Expand Down Expand Up @@ -86,7 +87,11 @@ end

input = File.open(ARGV[0]) if ARGV[0]

ebnf = EBNF.parse(input || STDIN, **options)
logger = options[:live] ? Logger.new(STDERR) : RDF::Spec.logger
logger.level = options[:level] || Logger::ERROR
logger.formatter = lambda {|severity, datetime, progname, msg| "%5s %s\n" % [severity, msg]}

ebnf = EBNF.parse(input || STDIN, logger: logger, **options)
ebnf.make_bnf if options[:bnf] || options[:ll1]
ebnf.make_peg if options[:peg]
if options[:ll1]
Expand Down
8 changes: 4 additions & 4 deletions examples/abnf/parser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -238,10 +238,10 @@ class ABNFParser
# @return [EBNFParser]
def initialize(input, **options, &block)
# If the `level` option is set, instantiate a logger for collecting trace information.
if options.has_key?(:level)
options[:logger] = Logger.new(STDERR)
options[:logger].level = options[:level]
options[:logger].formatter = lambda {|severity, datetime, progname, msg| "#{severity} #{msg}\n"}
if options.key?(:level)
options[:logger] ||= Logger.new(STDERR).
tap {|x| x.level = options[:level]}.
tap {|x| x.formatter = lambda {|severity, datetime, progname, msg| "#{severity} #{msg}\n"}}
end

# Read input, if necessary, which will be used in a Scanner.
Expand Down
8 changes: 4 additions & 4 deletions examples/ebnf-ll1-parser/parser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -276,10 +276,10 @@ def initialize(input, **options, &block)
@input = input.respond_to?(:read) ? input.read : input.to_s

# If the `level` option is set, instantiate a logger for collecting trace information.
if options.has_key?(:level)
options[:logger] = Logger.new(STDERR)
options[:logger].level = options.fetch(:level, 2)
options[:logger].formatter = lambda {|severity, datetime, progname, msg| "#{severity} #{msg}\n"}
if options.key?(:level)
options[:logger] ||= Logger.new(STDERR).
tap {|x| x.level = options[:level]}.
tap {|x| x.formatter = lambda {|severity, datetime, progname, msg| "#{severity} #{msg}\n"}}
end

parsing_terminals = false
Expand Down
8 changes: 4 additions & 4 deletions examples/ebnf-peg-parser/parser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -272,10 +272,10 @@ class EBNFPegParser
# @return [EBNFParser]
def initialize(input, **options, &block)
# If the `level` option is set, instantiate a logger for collecting trace information.
if options.has_key?(:level)
options[:logger] = Logger.new(STDERR)
options[:logger].level = options[:level]
options[:logger].formatter = lambda {|severity, datetime, progname, msg| "#{severity} #{msg}\n"}
if options.key?(:level)
options[:logger] ||= Logger.new(STDERR).
tap {|x| x.level = options[:level]}.
tap {|x| x.formatter = lambda {|severity, datetime, progname, msg| "#{severity} #{msg}\n"}}
end

# Read input, if necessary, which will be used in a Scanner.
Expand Down
8 changes: 4 additions & 4 deletions examples/isoebnf/parser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -200,10 +200,10 @@ class ISOEBNFPegParser
# @return [EBNFParser]
def initialize(input, **options, &block)
# If the `level` option is set, instantiate a logger for collecting trace information.
if options.has_key?(:level)
options[:logger] = Logger.new(STDERR)
options[:logger].level = options[:level]
options[:logger].formatter = lambda {|severity, datetime, progname, msg| "#{severity} #{msg}\n"}
if options.key?(:level)
options[:logger] ||= Logger.new(STDERR).
tap {|x| x.level = options[:level]}.
tap {|x| x.formatter = lambda {|severity, datetime, progname, msg| "#{severity} #{msg}\n"}}
end

# Read input, if necessary, which will be used in a Scanner.
Expand Down
8 changes: 4 additions & 4 deletions lib/ebnf/abnf.rb
Original file line number Diff line number Diff line change
Expand Up @@ -234,10 +234,10 @@ class ABNF
# @return [EBNFParser]
def initialize(input, **options)
# If the `level` option is set, instantiate a logger for collecting trace information.
if options.has_key?(:level)
options[:logger] = Logger.new(STDERR)
options[:logger].level = options[:level]
options[:logger].formatter = lambda {|severity, datetime, progname, msg| "#{severity} #{msg}\n"}
if options.key?(:level)
options[:logger] ||= Logger.new(STDERR).
tap {|x| x.level = options[:level]}.
tap {|x| x.formatter = lambda {|severity, datetime, progname, msg| "#{severity} #{msg}\n"}}
end

# Read input, if necessary, which will be used in a Scanner.
Expand Down
27 changes: 12 additions & 15 deletions lib/ebnf/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -106,8 +106,8 @@ class Base
# Format of input, one of `:abnf`, `:ebnf`, `:isoebnf`, `:isoebnf`, `:native`, or `:sxp`.
# Use `:native` for the native EBNF parser, rather than the PEG parser.
# @param [Hash{Symbol => Object}] options
# @option options [Boolean, Array] :debug
# Output debug information to an array or $stdout.
# @option options [Boolean] :level
# Trace level. 0(debug), 1(info), 2(warn), 3(error).
# @option options [Boolean, Array] :validate
# Validate resulting grammar.
def initialize(input, format: :ebnf, **options)
Expand Down Expand Up @@ -311,24 +311,17 @@ def depth

# Progress output, less than debugging
def progress(*args, **options)
return unless @options[:progress] || @options[:debug]
depth = options[:depth] || @depth
args << yield if block_given?
message = "#{args.join(': ')}"
str = "[#{@lineno}]#{' ' * depth}#{message}"
@options[:debug] << str if @options[:debug].is_a?(Array)
$stderr.puts(str) if @options[:progress] || @options[:debug] == true
debug(*args, **options.merge(level: Logger::INFO))
end

# Error output
def error(*args, **options)
depth = options[:depth] || @depth
args << yield if block_given?
message = "#{args.join(': ')}"
debug(message, **options.merge(level: Logger::ERROR))
@errors << message
str = "[#{@lineno}]#{' ' * depth}#{message}"
@options[:debug] << str if @options[:debug].is_a?(Array)
$stderr.puts(str)
$stderr.puts(message)
end

##
Expand All @@ -343,13 +336,17 @@ def error(*args, **options)
#
# @yieldreturn [String] added to message
def debug(*args, **options)
return unless @options[:debug]
return unless @options.key?(:logger)
level = @options[:level] || Logger::DEBUG
depth = options[:depth] || @depth
args << yield if block_given?
message = "#{args.join(': ')}"
str = "[#{@lineno}]#{' ' * depth}#{message}"
@options[:debug] << str if @options[:debug].is_a?(Array)
$stderr.puts(str) if @options[:debug] == true
if @options[:logger].respond_to?(:add)
@options[:logger].add(level, str)
elsif @options[:logger].respond_to?(:<<)
@options[:logger] << "[#{lineno}] " + str
end
end
end
end
8 changes: 4 additions & 4 deletions lib/ebnf/isoebnf.rb
Original file line number Diff line number Diff line change
Expand Up @@ -196,10 +196,10 @@ class ISOEBNF
# @return [EBNFParser]
def initialize(input, **options, &block)
# If the `level` option is set, instantiate a logger for collecting trace information.
if options.has_key?(:level)
options[:logger] = Logger.new(STDERR)
options[:logger].level = options[:level]
options[:logger].formatter = lambda {|severity, datetime, progname, msg| "#{severity} #{msg}\n"}
if options.key?(:level)
options[:logger] ||= Logger.new(STDERR).
tap {|x| x.level = options[:level]}.
tap {|x| x.formatter = lambda {|severity, datetime, progname, msg| "#{severity} #{msg}\n"}}
end

# Read input, if necessary, which will be used in a Scanner.
Expand Down
8 changes: 4 additions & 4 deletions spec/base_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@
%{((rule Prolog "2" (seq (opt BaseDecl) (star PrefixDecl))))},
%{
@terminals
[3] terminal ::= [A-Z]+
terminal ::= [A-Z]+
} => %{((terminals _terminals (seq))
(terminal terminal "3" (plus (range "A-Z"))))},
(terminal terminal (plus (range "A-Z"))))},
%{
[9] primary ::= HEX
| RANGE
Expand Down Expand Up @@ -125,11 +125,11 @@
end

describe "#to_s" do
specify {expect(subject.to_s).to include("[1] ebnf")}
specify {expect(subject.to_s).to include("ebnf")}
end

describe "#to_html" do
specify {expect(subject.to_s).to include("[1] ebnf")}
specify {expect(subject.to_s).to include("ebnf")}
end

describe "#to_ruby" do
Expand Down

0 comments on commit bbc6fe2

Please sign in to comment.