riml 0.2.8 → 0.2.9

Sign up to get free protection for your applications and to get access to all the features.
data/lib/riml.rb CHANGED
@@ -1,4 +1,5 @@
1
1
  require 'pathname'
2
+ require 'fileutils'
2
3
 
3
4
  require File.expand_path('../environment', __FILE__)
4
5
  require 'nodes'
@@ -8,30 +9,35 @@ require 'compiler'
8
9
  require 'warning_buffer'
9
10
 
10
11
  module Riml
11
- # lex code into tokens
12
+ # lex code (String) into tokens (Array)
12
13
  def self.lex(code)
13
14
  Lexer.new(code).tokenize
14
15
  end
15
16
 
16
- # parse code (or tokens) into nodes
17
+ # parse tokens (Array) or code (String) into AST (Nodes)
17
18
  def self.parse(input, ast_rewriter = AST_Rewriter.new, filename = nil)
18
19
  unless input.is_a?(Array) || input.is_a?(String)
19
- raise ArgumentError, "input must be tokens or code, is #{input.class}"
20
+ raise ArgumentError, "input must be tokens (Array) or code (String), " \
21
+ "is #{input.inspect}"
20
22
  end
21
23
  Parser.new.parse(input, ast_rewriter, filename)
22
24
  end
23
25
 
24
- # compile nodes (or tokens or code or file) into output code
26
+ # compile AST (Nodes), tokens (Array), code (String) or object that returns
27
+ # String from :read to output code (String). Writes file(s) if `input` is a
28
+ # File.
25
29
  def self.compile(input, parser = Parser.new, compiler = Compiler.new)
26
30
  if input.is_a?(Nodes)
27
31
  nodes = input
28
32
  elsif input.is_a?(String) || input.is_a?(Array)
29
33
  nodes = parser.parse(input)
30
- elsif input.is_a?(File)
34
+ elsif input.respond_to?(:read)
31
35
  source = input.read
32
- nodes = parser.parse(source, AST_Rewriter.new, input.path)
36
+ path = input.respond_to?(:path) ? input.path : nil
37
+ nodes = parser.parse(source, AST_Rewriter.new, path)
33
38
  else
34
- raise ArgumentError, "input must be nodes, tokens, code or file, is #{input.class}"
39
+ raise ArgumentError, "input must be one of AST (Nodes), tokens (Array), " \
40
+ "code (String) or respond_to?(:read), is #{input.inspect}"
35
41
  end
36
42
 
37
43
  if compiler.parser == parser
@@ -44,7 +50,7 @@ module Riml
44
50
  output = compiler.compile(nodes)
45
51
 
46
52
  if input.is_a?(File)
47
- write_file(output, input.path, compiling_cmdline_file)
53
+ write_file(compiler, output, input.path, compiling_cmdline_file)
48
54
  else
49
55
  output
50
56
  end
@@ -53,15 +59,38 @@ module Riml
53
59
  process_compile_queue!(compiler)
54
60
  end
55
61
 
56
- # expects `file_names` to be readable files
62
+ # expects `filenames` (String) arguments, to be readable files. Optional options (Hash) as
63
+ # last argument.
57
64
  def self.compile_files(*filenames)
65
+ parser, compiler = Parser.new, Compiler.new
66
+
67
+ if filenames.last.is_a?(Hash)
68
+ opts = filenames.pop
69
+ if dir = opts[:output_dir]
70
+ compiler.output_dir = dir
71
+ end
72
+ end
73
+
58
74
  if filenames.size > 1
59
- threaded_compile_files(*filenames)
75
+ threads = []
76
+ filenames.each_with_index do |fname, i|
77
+ if i.zero?
78
+ _parser, _compiler = parser, compiler
79
+ else
80
+ _parser, _compiler = Parser.new, Compiler.new
81
+ _compiler.output_dir = compiler.output_dir
82
+ end
83
+ threads << Thread.new do
84
+ f = File.open(fname)
85
+ compile(f, _parser, _compiler)
86
+ end
87
+ end
88
+ threads.each {|t| t.join}
60
89
  elsif filenames.size == 1
61
90
  fname = filenames.first
62
91
  f = File.open(fname)
63
92
  # `compile` will close file handle
64
- compile(f)
93
+ compile(f, parser, compiler)
65
94
  else
66
95
  raise ArgumentError, "need filenames to compile"
67
96
  end
@@ -69,10 +98,11 @@ module Riml
69
98
  flush_warnings
70
99
  end
71
100
 
72
- # checks syntax of `input` (lexes + parses) without going through ast rewriting or compilation
101
+ # checks syntax of `input` (String).
102
+ # lexes + parses without going through AST rewriting or compilation
73
103
  def self.check_syntax(input)
74
104
  raise ArgumentError.new(input) unless input.is_a?(String)
75
- parse(input, false)
105
+ parse(input, nil)
76
106
  true
77
107
  end
78
108
 
@@ -148,17 +178,6 @@ module Riml
148
178
  set_path(name, val)
149
179
  end
150
180
 
151
- def self.threaded_compile_files(*filenames)
152
- threads = []
153
- filenames.each do |fname|
154
- threads << Thread.new do
155
- f = File.open(fname)
156
- compile(f)
157
- end
158
- end
159
- threads.each {|t| t.join}
160
- end
161
-
162
181
  # This is for when another file is sourced within a file we're compiling.
163
182
  def self.process_compile_queue!(compiler)
164
183
  while full_path = compiler.compile_queue.shift
@@ -172,21 +191,23 @@ module Riml
172
191
  FILE_HEADER = File.read(File.expand_path("../header.vim", __FILE__)) % VERSION.join('.')
173
192
  INCLUDE_COMMENT_FMT = File.read(File.expand_path("../included.vim", __FILE__))
174
193
 
175
- def self.write_file(output, fname, cmdline_file = true)
176
- # writing out a file that's compiled from cmdline, output into CWD
194
+ def self.write_file(compiler, output, fname, cmdline_file = true)
195
+ # writing out a file that's compiled from cmdline, output into output_dir
177
196
  output_dir = if cmdline_file
178
- Dir.getwd
197
+ compiler.output_dir || Dir.getwd
179
198
  # writing out a riml_source'd file
180
199
  else
181
- # absolute path
182
- if fname[0] == File::SEPARATOR
200
+ # absolute path for filename sent from cmdline or from riml_sourced files,
201
+ # output to that same directory if no --output-dir option is set
202
+ if fname[0] == File::SEPARATOR && !compiler.output_dir
183
203
  Pathname.new(fname).parent.to_s
184
204
  # relative path
185
205
  else
186
- File.join(Dir.getwd, Pathname.new(fname).parent.to_s)
206
+ File.join(compiler.output_dir || Dir.getwd, Pathname.new(fname).parent.to_s)
187
207
  end
188
208
  end
189
209
  basename_without_riml_ext = File.basename(fname).sub(/\.riml\Z/i, '')
210
+ FileUtils.mkdir_p(output_dir) unless File.directory?(output_dir)
190
211
  full_path = File.join(output_dir, "#{basename_without_riml_ext}.vim")
191
212
  File.open(full_path, 'w') do |f|
192
213
  f.write FILE_HEADER + output
data/version.rb CHANGED
@@ -1,4 +1,4 @@
1
1
  module Riml
2
- # last changed: July 20, 2013
3
- VERSION = [0,2,8]
2
+ # last changed: August 18, 2013
3
+ VERSION = [0,2,9]
4
4
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: riml
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.8
4
+ version: 0.2.9
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-07-20 00:00:00.000000000 Z
12
+ date: 2013-08-18 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: racc