Class: CodeModels::Parser
- Inherits:
-
Object
- Object
- CodeModels::Parser
- Defined in:
- lib/codemodels/parser.rb
Overview
A parser get code and produce an AST
Sub-classes should provide internal_parse_artifact
Constant Summary collapse
- DEFAULT_INTERNAL_ENCODING =
'UTF-8'
Instance Attribute Summary collapse
-
#internal_encoding ⇒ Object
readonly
Internal encoding.
Instance Method Summary collapse
-
#initialize(internal_encoding = DEFAULT_INTERNAL_ENCODING) ⇒ Parser
constructor
A new instance of Parser.
- #parse_artifact(artifact) ⇒ Object
-
#parse_file(path, file_encoding = nil) ⇒ Object
Parse the file by producing an artifact corresponding to the file.
-
#parse_string(code) ⇒ Object
Parse the file by producing an artifact corresponding to the string.
Constructor Details
#initialize(internal_encoding = DEFAULT_INTERNAL_ENCODING) ⇒ Parser
Returns a new instance of Parser.
18 19 20 21 22 |
# File 'lib/codemodels/parser.rb', line 18 def initialize(internal_encoding=DEFAULT_INTERNAL_ENCODING) @internal_encoding = internal_encoding puts "WARN using an internal encoding different from the local encoding..." if "".encoding.name!=internal_encoding raise "The method internal_parse_artifact should be implemented" unless self.respond_to?(:internal_parse_artifact) end |
Instance Attribute Details
#internal_encoding ⇒ Object (readonly)
Internal encoding. Code is internally converted to this encoding.
16 17 18 |
# File 'lib/codemodels/parser.rb', line 16 def internal_encoding @internal_encoding end |
Instance Method Details
#parse_artifact(artifact) ⇒ Object
24 25 26 |
# File 'lib/codemodels/parser.rb', line 24 def parse_artifact(artifact) internal_parse_artifact(artifact) end |
#parse_file(path, file_encoding = nil) ⇒ Object
Parse the file by producing an artifact corresponding to the file
29 30 31 32 33 34 35 |
# File 'lib/codemodels/parser.rb', line 29 def parse_file(path,file_encoding=nil) file_encoding = internal_encoding unless file_encoding code = IO.read(path,{ :encoding => file_encoding, :mode => 'rb'}) code = code.encode(internal_encoding) artifact = FileArtifact.new(path,code) parse_artifact(artifact) end |
#parse_string(code) ⇒ Object
Parse the file by producing an artifact corresponding to the string
38 39 40 41 42 |
# File 'lib/codemodels/parser.rb', line 38 def parse_string(code) raise "Wrong encoding: it is #{code.encoding.name}, internally expected #{internal_encoding}" unless code.encoding.name==internal_encoding artifact = StringArtifact.new(code) parse_artifact(artifact) end |