Class: Codnar::Scanner
- Inherits:
-
Object
- Object
- Codnar::Scanner
- Defined in:
- lib/codnar/scanner.rb
Overview
Scan a file into classified lines.
Instance Method Summary collapse
-
#initialize(errors, syntax) ⇒ Scanner
constructor
Construct a scanner based on a syntax in the following structure:.
-
#lines(path) ⇒ Object
Scan a disk file into classified lines in the following format (where the groups contain the text extracted by the matching pattern):.
Constructor Details
#initialize(errors, syntax) ⇒ Scanner
Construct a scanner based on a syntax in the following structure:
patterns:
<name>:
name: <name>
kind: <kind>
regexp: <regexp>
groups:
- <name>
states:
<name>:
name: <name>
transitions:
- pattern: <pattern>
kind: <kind>
next_state: <state>
start_state: <state>
To allow for cleaner YAML files to specify the syntax, the following shorthands are supported:
-
A pattern or state reference can be presented by the string name of the pattern or state.
-
The name field of a state or pattern can be ommitted. If specified, it must be identical to the key in the states or patterns mapping.
-
The kind field of a pattern can be ommitted; by default it is assumed to be identical to the pattern name.
-
A pattern regexp can be presented by a plain string.
-
The pattern groups field can be ommitted or contain
nilif it is equal to [ “indentation”, “payload” ]. -
The kind field of a transition can be ommitted; by default it is assumed to be identical to the pattern kind. If it ends up
nil, this indicates that there’s no kind assigned by the pattern, and the current line should be classified again by the next state. -
The next state of a transition can be ommitted; by default it is assumed to be identical to the containing state.
-
The start state can be ommitted; by default it is assumed to be named
start.
When the Scanner is constructed, a deep clone of the syntax object is created and modified to expand all the above shorthands. Any problems detected during this process are pushed into the errors.
48 49 50 51 52 53 54 |
# File 'lib/codnar/scanner.rb', line 48 def initialize(errors, syntax) @errors = errors @syntax = syntax.deep_clone @syntax.patterns.each { |name, pattern| (name, pattern) } @syntax.states.each { |name, state| (name, state) } @syntax.start_state = resolve_start_state end |
Instance Method Details
#lines(path) ⇒ Object
Scan a disk file into classified lines in the following format (where the groups contain the text extracted by the matching pattern):
- kind: <kind>
line: <text>
<group>: <text>
By convention, each classified line has a “payload” group that contains the “main” content of the line (chunk name for begin/end/nested chunk lines, clean comment text for comment lines, etc.). In addition, most classified lines have an “indentation” group that contains the leading white space (which is not included in the payload).
If at some state, a file line does not match any pattern, the scanner will push a message into the errors. In addition it will classify the line as follows:
- kind: error
state: <name>
line: <text>
indentation: <leading white space>
payload: <line text following the indentation>
78 79 80 81 82 83 84 |
# File 'lib/codnar/scanner.rb', line 78 def lines(path) @path = path @lines = [] @state = @syntax.start_state @errors.in_file_lines(path) { |line| scan_line(line.chomp) } return @lines end |