Module: Chusaku::Parser
- Defined in:
- lib/chusaku/parser.rb
Overview
Handles parsing a file and groups its lines into categories.
Class Method Summary collapse
-
.call(path:, actions:) ⇒ Hash
Primary method to call.
-
.parse_line(line:, actions:) ⇒ Hash
Given a line and actions, returns the line’s type.
Class Method Details
.call(path:, actions:) ⇒ Hash
Primary method to call.
Example output:
{
content: <Original file content>,
groups: [
{
type: :code,
body: 'class Foo\n',
action: nil,
line_number: 1
},
{
type: :comment,
body: ' # Bar\n # Baz\n',
action: nil,
line_number: 2
},
{
type: :action,
body: ' def action_name; end\n',
action: 'action_name',
line_number: 4
}
{
type: :code,
body: 'end # vanilla is the best flavor\n',
action: nil,
line_number: 5
}
]
}
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 |
# File 'lib/chusaku/parser.rb', line 41 def self.call(path:, actions:) groups = [] group = {} content = IO.read(path) content.each_line.with_index do |line, index| parsed_line = parse_line(line: line, actions: actions) if group[:type] == parsed_line[:type] # Same group. Push the current line into the current group. group[:body] += line else # Now looking at a new group. Push the current group onto the array # and start a new one. groups.push(group) unless group.empty? group = parsed_line.merge(line_number: index + 1) end end # Push the last group onto the array and return. groups.push(group) {content: content, groups: groups} end |
.parse_line(line:, actions:) ⇒ Hash
Given a line and actions, returns the line’s type.
A type can be one of:
1. comment - A line that is entirely commented. Lines that have trailing
comments do not fall under this category.
2. action - A line that contains an action definition.
3. code - Anything else.
Returns a Hash in the form:
{ type: :action, body: 'def foo', action: 'foo' }
81 82 83 84 85 86 87 88 89 90 91 92 |
# File 'lib/chusaku/parser.rb', line 81 def self.parse_line(line:, actions:) comment_match = /^\s*#.*$/.match(line) def_match = /^\s*def\s+(\w*)\s*\w*.*$/.match(line) if !comment_match.nil? {type: :comment, body: line, action: nil} elsif !def_match.nil? && actions.include?(def_match[1]) {type: :action, body: line, action: def_match[1]} else {type: :code, body: line, action: nil} end end |