Initial Revision
[ohcount] / ext / ohcount_native / state.rb
1 module Ohcount
2         # The source code parser is implemented as a state machine.
3         #
4         # Each state is associated with a particular language and semantic (:code, :comment, or :blank).
5         class State
6                 # The state name -- any helpful mnemonic, but must be unique within this language.
7                 attr_reader :name
8
9                 # When in this state, we are considered to be reading this language.
10                 attr_reader :language
11
12                 # One of :code, :comment, or :blank.
13                 attr_reader     :semantic
14
15                 def initialize(language, name, semantic)
16                         raise ArgumentError.new("unknown semantic '#{ semantic }'") unless [:code, :comment, :blank].include?(semantic)
17
18                         @language = language
19                         @name = name.to_s.downcase
20                         @semantic = semantic
21                 end
22
23                 def full_name
24                 "#{ language }_#{ name.to_s }".upcase
25                 end
26
27                 def definition
28                         full_name.upcase
29                 end
30
31                 # Emit the state definition as C statements into the generated file.
32                 def print(io)
33                         io.puts %(State #{ definition } = { "#{ full_name }", "#{ @language }", semantic_#{ semantic.to_s } };)
34                 end
35         end
36 end