edn-abnf 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 8e126887990e8896adb3bd44950656b51afc8d8f1302621c017ecbbc914361eb
4
+ data.tar.gz: 9766b5bc4ec7a00e40cea828776d737b2723b6f3ae9448d1fb3dcc10a45f336a
5
+ SHA512:
6
+ metadata.gz: 1b6886f7d40dec4c34f5cb39c9900c5123605b97b68fb620f0d4ff40eefc1bdb27f26b1132464afdcd2e94be46a1be7d8d9e708f25b34a4047899bfb875e4ce9
7
+ data.tar.gz: 051ca2fbe7cb43b569d0c376b4fc7a8b1d29f4a33fb0b6b8c73da78029e73042356203b6bdc3dbb4ce4fb98fd1c0e5bcfc19eb218c72fa9b190ec7dc86b0517c
data/bin/edn-abnf ADDED
@@ -0,0 +1,57 @@
1
+ #!/usr/bin/env ruby
2
+ require 'pp'
3
+ require 'yaml'
4
+ require 'treetop'
5
+ require 'json'
6
+
7
+ require_relative '../lib/edn-abnf.rb'
8
+
9
+ def snaky(name)
10
+ name.gsub(/-/, "_")
11
+ end
12
+
13
+ Encoding.default_external = Encoding::UTF_8
14
+ require 'optparse'
15
+ require 'ostruct'
16
+
17
+ $options = OpenStruct.new
18
+ begin
19
+ op = OptionParser.new do |opts|
20
+ opts.banner = "Usage: edn-abnf.rb [options] [file.edn... | -]"
21
+
22
+ opts.on("-v", "--[no-]verbose", "Run verbosely") do |v|
23
+ $options.verbose = v
24
+ end
25
+ opts.on("-tFMT", "--to=FMT", [:basic, :neat, :json, :yaml, :edn, :diag], "Target format") do |v|
26
+ $options.target = v
27
+ end
28
+ end
29
+ op.parse!
30
+ rescue Exception => e
31
+ warn e
32
+ exit 1
33
+ end
34
+
35
+ if ARGV == []
36
+ puts op
37
+ exit 1
38
+ end
39
+
40
+ edn_file = ARGF.read
41
+
42
+ edn = EDN.from_edn(edn_file)
43
+ result = edn.tree # XXX .tree?
44
+
45
+ case $options.target
46
+ when :basic, nil
47
+ pp result
48
+ when :neat, :json
49
+ require 'neatjson'
50
+ puts JSON.neat_generate(result, after_comma: 1, after_colon: 1)
51
+ when :yaml
52
+ puts result.to_yaml
53
+ when :edn, :diag
54
+ puts result.cbor_diagnostic
55
+ else
56
+ warn ["Unknown target format: ", $options.target].inspect
57
+ end
data/edn-abnf.gemspec ADDED
@@ -0,0 +1,20 @@
1
+ Gem::Specification.new do |s|
2
+ s.name = "edn-abnf"
3
+ s.version = "0.0.1"
4
+ s.summary = "CDDL (Concise Data Definition Language) converters and miscellaneous tools"
5
+ s.description = %q{edn-abnf implements converters and miscellaneous tools for CBOR EDN's ABNF}
6
+ s.author = "Carsten Bormann"
7
+ s.email = "cabo@tzi.org"
8
+ s.license = "MIT"
9
+ s.homepage = "http://github.com/cabo/edn-abnf"
10
+ s.files = Dir['lib/**/*.rb'] + %w(edn-abnf.gemspec) + Dir['data/*.cddl'] + Dir['bin/**/*.rb']
11
+ s.executables = Dir['bin/*'].map {|x| File.basename(x)}
12
+ s.required_ruby_version = '>= 1.9.2'
13
+
14
+ s.require_paths = ["lib"]
15
+
16
+ s.add_development_dependency 'bundler', '~>1'
17
+ s.add_dependency 'treetop', '~>1'
18
+ s.add_dependency 'json', '~>2'
19
+ s.add_dependency 'neatjson', '~>0.10'
20
+ end
data/lib/edn-abnf.rb ADDED
@@ -0,0 +1,41 @@
1
+ require_relative "parser/edn-util.rb"
2
+
3
+ class EDN
4
+ @@parser = EDNGRAMMARParser.new
5
+
6
+ def self.reason(parser, s)
7
+ reason = [parser.failure_reason]
8
+ parser.failure_reason =~ /^(Expected .+) after/m
9
+ reason << "#{$1.gsub("\n", '<<<NEWLINE>>>')}:" if $1
10
+ if line = s.lines.to_a[parser.failure_line - 1]
11
+ reason << line
12
+ reason << "#{'~' * (parser.failure_column - 1)}^"
13
+ end
14
+ reason.join("\n")
15
+ end
16
+
17
+ def self.from_edn(s)
18
+ ast = @@parser.parse s
19
+ if !ast
20
+ fail self.reason(@@parser, s)
21
+ end
22
+ ret = EDN.new(ast)
23
+ ret
24
+ end
25
+
26
+ attr_accessor :ast, :tree
27
+ def initialize(ast_)
28
+ @ast = ast_
29
+ @tree = ast.ast
30
+ end
31
+
32
+ def warn_error(s)
33
+ warn s
34
+ @error = true
35
+ end
36
+ def deep_clone
37
+ Marshal.load(Marshal.dump(self))
38
+ end
39
+
40
+
41
+ end
@@ -0,0 +1,19 @@
1
+ require 'cbor-diagnostic'
2
+ module CBOR_DIAG
3
+ end
4
+ require 'cbor-diagnostic-app/dt'
5
+
6
+ require 'treetop'
7
+ require_relative './edngrammar'
8
+
9
+ class Treetop::Runtime::SyntaxNode
10
+ def ast
11
+ fail "undefined_ast #{inspect}"
12
+ end
13
+ def ast1 # devhack
14
+ "#{inspect[10..20]}--#{text_value[0..15]}"
15
+ end
16
+ def hex_value
17
+ text_value.to_i(16)
18
+ end
19
+ end