gpp 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 768357e29236ee25c7bdfad581346d092c16ffea
4
+ data.tar.gz: a17f96873ebdf6f6e8db3bd8ae108e909f4c7f36
5
+ SHA512:
6
+ metadata.gz: 85c4310438cd96db69747dca9c31c1de02dccbc385d691bacfd044722281fabff8c39210f2caa1719bfb3f755fa20015309257b1c178d1fd79df9796602ccfd9
7
+ data.tar.gz: 6140d1bc380ede98ca30cc49fcc2450c8799b0bc1430a6b2becea8a01bd71cf61117feeac3c96cd25dc5d471affd9419028462007265986ee1617fc518d6a527
data/bin/gpp ADDED
@@ -0,0 +1,17 @@
1
+ #!/usr/bin/env ruby
2
+ require 'gpp'
3
+ require 'pathname'
4
+ require 'optparse'
5
+
6
+ OptionParser.new do |opts|
7
+ opts.version = GPP::VERSION
8
+ opts.banner = "Usage: #{opts.program_name} [-hv] [files]"
9
+ end.order!
10
+
11
+ args = ARGV.empty? ? ["-"] : ARGV
12
+
13
+ args.each do |arg|
14
+ file = arg == "-" ? STDIN.read : File.read(arg)
15
+ rpp = GPP::Processor.new(file, STDOUT)
16
+ rpp.scan_all
17
+ end
@@ -0,0 +1,2 @@
1
+ require 'gpp/processor'
2
+ require 'gpp/version'
@@ -0,0 +1,135 @@
1
+ require 'strscan'
2
+
3
+ module GPP
4
+ class Processor < StringScanner
5
+
6
+ def initialize (in_, out, defs = {})
7
+ super(in_)
8
+ @out = out
9
+ @defs = defs.to_h
10
+ end
11
+
12
+ def loopcat (into = "")
13
+ loop do
14
+ val = yield
15
+ break unless val
16
+ into << val
17
+ end
18
+ into
19
+ end
20
+
21
+ def scan_block
22
+ loopcat do
23
+ if scan(/{/)
24
+ "{" + scan_block + "}"
25
+ elsif scan(/}/)
26
+ nil
27
+ else
28
+ scan(/[^{}]+/)
29
+ end
30
+ end
31
+ end
32
+
33
+ def scan_string
34
+ loopcat do
35
+ if scan(/"/)
36
+ nil
37
+ else
38
+ scan(/\\?.|[^"\\]/)
39
+ end
40
+ end
41
+ end
42
+
43
+ def scan_word (bare = /\S+/)
44
+ if scan(/{\n?/)
45
+ scan_block()
46
+ elsif scan(/"/)
47
+ scan_string()
48
+ else
49
+ scan(bare)
50
+ end
51
+ end
52
+
53
+ def scan_args
54
+ if scan(/\(/)
55
+ loopcat [] do
56
+ if scan(/\)/)
57
+ nil
58
+ else
59
+ scan_word(/[^,)]+/).tap do
60
+ scan(/,/)
61
+ end
62
+ end
63
+ end
64
+ else
65
+ loopcat [] do
66
+ if scan(/\n/)
67
+ nil
68
+ else
69
+ scan(/ +/)
70
+ scan_word
71
+ end
72
+ end
73
+ end
74
+ end
75
+
76
+ def run (string, args = nil)
77
+ if args == nil
78
+ self.class.new(string, s = "", @defs).scan_all
79
+ else
80
+ self.class.new(string, s = "", @defs.merge(args.to_h)).scan_all
81
+ end
82
+ s
83
+ end
84
+
85
+ def run_block (args, body)
86
+ theargs = scan_args
87
+ run(body, args.zip(theargs))
88
+ end
89
+
90
+ def run_macro (id)
91
+ if d = @defs[id]
92
+ if d.is_a?(String)
93
+ run(d)
94
+ else
95
+ theargs = scan_args
96
+ run(d[1], d[0].zip(theargs))
97
+ end
98
+ else
99
+ STDERR.puts "undefined macro: #{id}"
100
+ exit 1
101
+ end
102
+ end
103
+
104
+ def scan_define
105
+ name, *args, body = scan_args
106
+ @defs[name] = [args, body]
107
+ end
108
+
109
+ def scan_import ()
110
+ args = scan_args
111
+ args.each do |arg|
112
+ run File.read(arg)
113
+ end
114
+ end
115
+
116
+ def scan_all
117
+ scan(/\s+/)
118
+ while !eos?
119
+ @out << (scan(/[^#@]+/) || "")
120
+ if scan(/#define\b/)
121
+ scan_define
122
+ elsif scan(/#import\b/)
123
+ scan_import
124
+ elsif scan(/@@/)
125
+ @out << "@"
126
+ elsif scan(/@(\w+)/)
127
+ @out << (run_macro(self[1]) || "")
128
+ else
129
+ scan(/./)
130
+ end
131
+ end
132
+ end
133
+
134
+ end
135
+ end
@@ -0,0 +1,3 @@
1
+ module GPP
2
+ VERSION = "0.1.0"
3
+ end
metadata ADDED
@@ -0,0 +1,49 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: gpp
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Nathan Baum
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-12-11 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: A text pre-processor
14
+ email: n@p12a.org.uk
15
+ executables:
16
+ - gpp
17
+ extensions: []
18
+ extra_rdoc_files: []
19
+ files:
20
+ - bin/gpp
21
+ - lib/gpp.rb
22
+ - lib/gpp/processor.rb
23
+ - lib/gpp/version.rb
24
+ homepage: http://www.github.org/nbaum/rpp
25
+ licenses:
26
+ - MIT
27
+ metadata: {}
28
+ post_install_message:
29
+ rdoc_options: []
30
+ require_paths:
31
+ - lib
32
+ required_ruby_version: !ruby/object:Gem::Requirement
33
+ requirements:
34
+ - - ">="
35
+ - !ruby/object:Gem::Version
36
+ version: '0'
37
+ required_rubygems_version: !ruby/object:Gem::Requirement
38
+ requirements:
39
+ - - ">="
40
+ - !ruby/object:Gem::Version
41
+ version: '0'
42
+ requirements: []
43
+ rubyforge_project:
44
+ rubygems_version: 2.4.5.1
45
+ signing_key:
46
+ specification_version: 4
47
+ summary: GPP
48
+ test_files: []
49
+ has_rdoc: