Skip to content

Commit

Permalink
Move Opal::Fragment into opal/fragment.rb
Browse files Browse the repository at this point in the history
  • Loading branch information
adambeynon committed Oct 6, 2013
1 parent 9f21010 commit 2de3af2
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 94 deletions.
36 changes: 36 additions & 0 deletions lib/opal/fragment.rb
@@ -0,0 +1,36 @@
module Opal
class Parser
# A fragment holds a string of generated javascript that will be written
# to the destination. It also keeps hold of the original sexp from which
# it was generated. Using this sexp, when writing fragments in order, a
# mapping can be created of the original location => target location,
# aka, source-maps!
class Fragment
# String of javascript this fragment holds
attr_reader :code

def initialize(code, sexp = nil)
@code = code.to_s
@sexp = sexp
end

# In debug mode we may wish to include the original line as a comment
def to_code
if @sexp
"/*:#{@sexp.line}*/#{@code}"
else
@code
end
end

# inspect the contents of this fragment, f("fooo")
def inspect
"f(#{@code.inspect})"
end

def line
@sexp.line if @sexp
end
end
end
end
53 changes: 1 addition & 52 deletions lib/opal/parser.rb
Expand Up @@ -2,42 +2,10 @@
require 'opal/grammar'
require 'opal/target_scope'
require 'opal/version'
require 'opal/fragment'

module Opal
class Parser
# A fragment holds a string of generated javascript that will be written
# to the destination. It also keeps hold of the original sexp from which
# it was generated. Using this sexp, when writing fragments in order, a
# mapping can be created of the original location => target location,
# aka, source-maps!
class Fragment
# String of javascript this fragment holds
attr_reader :code

def initialize(code, sexp = nil)
@code = code.to_s
@sexp = sexp
end

# In debug mode we may wish to include the original line as a comment
def to_code
if @sexp
"/*:#{@sexp.line}*/#{@code}"
else
@code
end
end

# inspect the contents of this fragment, f("fooo")
def inspect
"f(#{@code.inspect})"
end

def line
@sexp.line if @sexp
end
end

# Generated code gets indented with two spaces on each scope
INDENT = ' '

Expand Down Expand Up @@ -108,25 +76,6 @@ def source_map
Opal::SourceMap.new(@fragments, '(file)')
end

def extract_parser_options(content)
result = {}

if /^#\ opal\:(.*)/ =~ content
$~[1].split(',').map(&:strip).each do |opt|
next if opt == ""
opt = opt.gsub('-', '_')

if opt =~ /no_/
result[opt.sub(/no_/, '').to_sym] = false
else
result[opt.to_sym] = true
end
end
end

result
end

# This is called when a parsing/processing error occurs. This
# method simply appends the filename and curent line number onto
# the message and raises it.
Expand Down
41 changes: 0 additions & 41 deletions spec/parser/parser_spec.rb

This file was deleted.

2 changes: 1 addition & 1 deletion stdlib/opal-parser.js.erb
Expand Up @@ -2,7 +2,7 @@
<% require_asset 'strscan' %>

// We need (some) of the libs from our real ruby parser (not in sprockets load path)
<% %w(opal/version opal/grammar opal/lexer opal/parser opal/target_scope opal/core_ext opal/lexer_scope opal/grammar_helpers opal/parser opal).each do |f| %>
<% %w(opal/version opal/grammar opal/lexer opal/parser opal/target_scope opal/core_ext opal/lexer_scope opal/grammar_helpers opal/parser opal/fragment opal).each do |f| %>
<%= Opal::RequireParser.parse File.read(File.join Opal.core_dir, '..', 'lib', "#{f}.rb") %>
<% end %>

Expand Down

0 comments on commit 2de3af2

Please sign in to comment.