Initial Revision
[ohcount] / ext / ohcount_native / glots / d_monoglot.rb
1 require 'monoglot'
2
3 module Ohcount
4         # D is very similar to C, but not quite.
5         #    It allows ` as a string delimiter.
6         #    It allows nested block comments using /+ +/.
7         class DMonoglot < Monoglot
8
9                 def initialize(language)
10                         @name = language
11
12                         # spit out states
13                         @states = [
14                                 State.new(language, :code, :code),
15                                 State.new(language, :dquote_string, :code),
16                                 State.new(language, :squote_string, :code),
17                                 State.new(language, :backtick_string, :code),
18                                 State.new(language, :line_comment, :comment),
19                                 State.new(language, :block_comment, :comment),
20                                 State.new(language, :nested_comment, :comment)
21                         ]
22
23                         @transitions = []
24
25                         # line comments
26                         @transitions << Transition.new(language, '//', :code, :line_comment, :to, false)
27                         @transitions << Transition.new(language, '\n', :line_comment, :return, :from, false)
28
29                         # C-like block comments
30                         @transitions << Transition.new(language, e('/*'), :code, :block_comment, :to, false)
31                         @transitions << Transition.new(language, e('/*'), :nested_comment, :block_comment, :to, false)
32                         @transitions << Transition.new(language, e('*/'), :block_comment, :return, :from, false)
33
34                         # Nested block comments
35                         @transitions << Transition.new(language, e('/+'), :code, :nested_comment, :to, false)
36                         @transitions << Transition.new(language, e('/+'), :nested_comment, :nested_comment, :to, false)
37                         @transitions << Transition.new(language, e('+/'), :nested_comment, :return, :from, false)
38
39                         # single_quote
40                         @transitions << Transition.new(language, "'", :code, :squote_string, :to, false)
41                         @transitions << Transition.new(language, e("\\\\"), :squote_string, :squote_string, :from, true, "ESC_SLASH")
42                         @transitions << Transition.new(language, e("\\'"), :squote_string, :squote_string, :from, true, "ESC")
43                         @transitions << Transition.new(language, "'", :squote_string, :return, :from, false)
44
45                         # backtick
46                         @transitions << Transition.new(language, "`", :code, :backtick_string, :to, false)
47                         @transitions << Transition.new(language, e("\\\\"), :backtick_string, :backtick_string, :from, true, "ESC_SLASH")
48                         @transitions << Transition.new(language, e("\\`"), :backtick_string, :backtick_string, :from, true, "ESC")
49                         @transitions << Transition.new(language, "`", :backtick_string, :return, :from, false)
50
51                         # double_quote
52                         @transitions << Transition.new(language, e('"'), :code, :dquote_string, :to, false)
53                         @transitions << Transition.new(language, e('\\\\'), :dquote_string, :dquote_string, :to, true, "ESC_SLASH")
54                         @transitions << Transition.new(language, e('\\"'), :dquote_string, :dquote_string, :to, true, "ESC")
55                         @transitions << Transition.new(language, e('"'), :dquote_string, :return, :from, false)
56                 end
57         end
58 end