riml 0.1.3 → 0.1.5
Sign up to get free protection for your applications and to get access to all the features.
- data/bin/riml +22 -15
- data/lib/ast_rewriter.rb +6 -5
- data/lib/compiler.rb +85 -39
- data/lib/constants.rb +4 -0
- data/{config → lib}/environment.rb +3 -2
- data/lib/errors.rb +7 -2
- data/lib/grammar.y +143 -125
- data/lib/header.vim +4 -0
- data/lib/lexer.rb +6 -3
- data/lib/nodes.rb +49 -9
- data/lib/parser.rb +1269 -1349
- data/lib/riml.rb +84 -0
- data/version.rb +2 -2
- metadata +7 -6
- data/lib/helper.rb +0 -45
data/lib/riml.rb
ADDED
@@ -0,0 +1,84 @@
|
|
1
|
+
require File.expand_path('../environment', __FILE__)
|
2
|
+
require 'nodes'
|
3
|
+
require 'lexer'
|
4
|
+
require 'parser'
|
5
|
+
require 'compiler'
|
6
|
+
|
7
|
+
module Riml
|
8
|
+
# lex code into tokens
|
9
|
+
def self.lex(code)
|
10
|
+
Lexer.new(code).tokenize
|
11
|
+
end
|
12
|
+
|
13
|
+
# parse code (or tokens) into nodes
|
14
|
+
def self.parse(input, ast_rewriter = AST_Rewriter.new)
|
15
|
+
unless input.is_a?(Array) || input.is_a?(String)
|
16
|
+
raise ArgumentError, "input must be tokens or code, is #{input.class}"
|
17
|
+
end
|
18
|
+
Parser.new.parse(input, ast_rewriter)
|
19
|
+
end
|
20
|
+
|
21
|
+
# compile nodes (or tokens or code or file) into output code
|
22
|
+
def self.compile(input, parser = Parser.new, compiler = Compiler.new)
|
23
|
+
if input.is_a?(Nodes)
|
24
|
+
nodes = input
|
25
|
+
elsif input.is_a?(String) || input.is_a?(Array)
|
26
|
+
nodes = parser.parse(input)
|
27
|
+
elsif input.is_a?(File)
|
28
|
+
source = input.read
|
29
|
+
nodes = parser.parse(source)
|
30
|
+
else
|
31
|
+
raise ArgumentError, "input must be nodes, tokens or code, is #{input.class}"
|
32
|
+
end
|
33
|
+
output = compiler.compile(nodes)
|
34
|
+
return output unless input.is_a?(File)
|
35
|
+
write_file(output, input.path)
|
36
|
+
ensure
|
37
|
+
input.close if input.is_a?(File)
|
38
|
+
process_compile_queue!(parser, compiler)
|
39
|
+
end
|
40
|
+
|
41
|
+
# expects `file_names` to be readable files
|
42
|
+
def self.compile_files(*file_names)
|
43
|
+
file_names.each do |file_name|
|
44
|
+
f = File.open(file_name)
|
45
|
+
# `compile` will close file handle
|
46
|
+
compile(f)
|
47
|
+
end
|
48
|
+
end
|
49
|
+
|
50
|
+
def self.source_path
|
51
|
+
@source_path ||= Dir.getwd
|
52
|
+
end
|
53
|
+
def self.source_path=(path)
|
54
|
+
@source_path = path
|
55
|
+
end
|
56
|
+
|
57
|
+
private
|
58
|
+
|
59
|
+
# This is for when another file is sourced within a file we're compiling.
|
60
|
+
# We have to share the same `ClassMap`, thus we have a queue for the compiler,
|
61
|
+
# and we process this queue after each source we compile. We pass the same
|
62
|
+
# parser instance to share Class state, as this state belongs to the
|
63
|
+
# AST_Rewriter's `ClassMap`.
|
64
|
+
def self.process_compile_queue!(parser, compiler)
|
65
|
+
return true if compiler.compile_queue.empty?
|
66
|
+
|
67
|
+
file_name = compiler.compile_queue.shift
|
68
|
+
compile(File.open(File.join(Riml.source_path, file_name)), parser, Compiler.new)
|
69
|
+
process_compile_queue!(parser, compiler)
|
70
|
+
end
|
71
|
+
|
72
|
+
FILE_HEADER = File.read(File.expand_path("../header.vim", __FILE__)) % VERSION.join('.')
|
73
|
+
|
74
|
+
def self.write_file(output, file_name)
|
75
|
+
file_basename = File.basename(file_name)
|
76
|
+
unless File.extname(file_basename).empty?
|
77
|
+
file_basename = file_basename.split(".").tap {|parts| parts.pop}.join(".")
|
78
|
+
end
|
79
|
+
File.open("#{file_basename}.vim", 'w') do |f|
|
80
|
+
f.write FILE_HEADER + output
|
81
|
+
end
|
82
|
+
end
|
83
|
+
|
84
|
+
end
|
data/version.rb
CHANGED
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.1.
|
4
|
+
version: 0.1.5
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,11 +9,11 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2012-11-
|
12
|
+
date: 2012-11-25 00:00:00.000000000Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: racc
|
16
|
-
requirement: &
|
16
|
+
requirement: &74018570 !ruby/object:Gem::Requirement
|
17
17
|
none: false
|
18
18
|
requirements:
|
19
19
|
- - ! '>='
|
@@ -21,7 +21,7 @@ dependencies:
|
|
21
21
|
version: '0'
|
22
22
|
type: :development
|
23
23
|
prerelease: false
|
24
|
-
version_requirements: *
|
24
|
+
version_requirements: *74018570
|
25
25
|
description: ! " Riml is a superset of VimL that includes some nice features:\n classes,
|
26
26
|
string interpolation, heredocs, default case-sensitive string\n comparison and
|
27
27
|
other things most programmers take for granted.\n"
|
@@ -34,16 +34,17 @@ files:
|
|
34
34
|
- README.md
|
35
35
|
- LICENSE
|
36
36
|
- version.rb
|
37
|
-
- config/environment.rb
|
38
37
|
- lib/parser.rb
|
39
38
|
- lib/class_map.rb
|
40
39
|
- lib/errors.rb
|
41
40
|
- lib/walker.rb
|
41
|
+
- lib/header.vim
|
42
42
|
- lib/ast_rewriter.rb
|
43
|
+
- lib/riml.rb
|
43
44
|
- lib/compiler.rb
|
44
45
|
- lib/nodes.rb
|
46
|
+
- lib/environment.rb
|
45
47
|
- lib/grammar.y
|
46
|
-
- lib/helper.rb
|
47
48
|
- lib/lexer.rb
|
48
49
|
- lib/constants.rb
|
49
50
|
- bin/riml
|
data/lib/helper.rb
DELETED
@@ -1,45 +0,0 @@
|
|
1
|
-
require File.expand_path('../../config/environment', __FILE__)
|
2
|
-
require 'nodes'
|
3
|
-
require 'lexer'
|
4
|
-
require 'parser'
|
5
|
-
require 'compiler'
|
6
|
-
|
7
|
-
module Riml
|
8
|
-
# lex code into tokens
|
9
|
-
def self.lex(code)
|
10
|
-
Lexer.new(code).tokenize
|
11
|
-
end
|
12
|
-
|
13
|
-
# parse code (or tokens) into nodes
|
14
|
-
def self.parse(input, rewrite_ast = true)
|
15
|
-
unless input.is_a?(Array) || input.is_a?(String)
|
16
|
-
raise ArgumentError, "input must be tokens or code, is #{input.class}"
|
17
|
-
end
|
18
|
-
Parser.new.parse(input, rewrite_ast)
|
19
|
-
end
|
20
|
-
|
21
|
-
# compile nodes (or tokens or code) into output code
|
22
|
-
def self.compile(input)
|
23
|
-
if input.is_a?(Nodes)
|
24
|
-
nodes = input
|
25
|
-
elsif input.is_a?(String) || input.is_a?(Array)
|
26
|
-
nodes = parse(input)
|
27
|
-
else
|
28
|
-
raise ArgumentError, "input must be nodes, tokens or code, is #{input.class}"
|
29
|
-
end
|
30
|
-
Compiler.new.compile(nodes)
|
31
|
-
end
|
32
|
-
|
33
|
-
# expects `file_name` to be readable file
|
34
|
-
def self.compile_file(file_name)
|
35
|
-
input = File.read(file_name)
|
36
|
-
output = compile(input)
|
37
|
-
file_basename = File.basename(file_name)
|
38
|
-
unless File.extname(file_basename).empty?
|
39
|
-
file_basename = file_basename.split(".").tap {|parts| parts.pop}.join(".")
|
40
|
-
end
|
41
|
-
File.open("#{file_basename}.vim", 'w') do |f|
|
42
|
-
f.write output
|
43
|
-
end
|
44
|
-
end
|
45
|
-
end
|