Class: Elisp::Parser

Inherits:
Object
  • Object
show all
Defined in:
lib/elisp/parser.rb

Overview

Copyright (C) 2025 gemmaro

This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along with this program. If not, see http://www.gnu.org/licenses/.

Constant Summary collapse

HEADER_LINE =
%r{
  ;;;[ ]
  (?<title>[a-z]+[.]el)[ ]---[ ](?<desc>.*?)
  (?:[ ]+-[*]-[ ]*(?<vars>.*?)[ ]*-[*]-)?
  \n+
}x
COMMENT =
/ *;; (?<comment>.*)\n/
CODE =
%r{
  (?<content>
    (?:
      [A-Za-z0-9&,.:=?_#'`<>()\[\]\t +*/-]
      | [?]\\.
      | "(?:[^\\"]|\\(?:.|\n))*"
    )+?
    (?:[ ]*;.*)?
    | ;;;[#][#][#]autoload.*
  )
  \n
}x
BLANKLINES =
/\n+/
CODE_OR_BLANKLINES =
Regexp.union(CODE, BLANKLINES)
DEFAULT_CSS =
"<style>#{css}</style>"
HEADING =
%r{
  ;;;(?<level>;*)[ ](?<title>.+)\n
  (?:(?:;;)?\n)*
}x

Instance Method Summary collapse

Constructor Details

#initialize(input) ⇒ Parser

Returns a new instance of Parser.



47
48
49
50
51
# File 'lib/elisp/parser.rb', line 47

def initialize(input)
  @scanner = StringScanner.new(input.read)
  @state = :start
  @nodes = []
end

Instance Method Details

#make_major_paraObject



131
132
133
134
135
136
137
138
139
140
141
142
# File 'lib/elisp/parser.rb', line 131

def make_major_para
  paras = [@nodes.pop.to_minor_para]
  while (node = @nodes.pop)
    if node.is_a?(Elisp::MinorPara)
      paras.unshift(node)
    else
      @nodes.push(node)
      break
    end
  end
  @nodes << Elisp::MajorPara.new(paras)
end

#parseObject



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
# File 'lib/elisp/parser.rb', line 53

def parse
  until @scanner.eos?

    if @state == :start && @scanner.skip(HEADER_LINE)
      @nodes << Elisp::Heading.new(@scanner[:title], level: 1)
      @nodes << Elisp::Desc.new(@scanner[:desc])
      @nodes << Elisp::Variables.new(@scanner[:vars])
      @state = :after_header_line

    elsif [:after_header_line,
           :after_major_para,
           :after_minor_para,
           :after_code,
           :after_page_delimiter,
           :after_heading].include?(@state) &&
          @scanner.skip(HEADING)
      level = @scanner[:level].size
      title = @scanner[:title]
      if level.zero?
        if ["Commentary:", "Code:"].include?(title)
          title.chop!
        elsif title.match?(/[a-z]+[.]el ends here/)
          next  # validate file name?
        end
      end
      @nodes << Elisp::Heading.new(title, level: level + 2)
      @state = :after_heading

    elsif [:after_heading,
           :after_major_para,
           :after_minor_para,
           :after_code,
           :after_header_line].include?(@state) &&
          @scanner.skip(COMMENT)
      @nodes << Elisp::Comment.new(@scanner[:comment])
      @state = :after_comment

    elsif @state == :after_comment && @scanner.skip(";;\n")
      @nodes[-1] = @nodes.last.to_minor_para
      @state = :after_minor_para

    elsif @state == :after_comment && @scanner.skip(COMMENT)
      @nodes.last << @scanner[:comment]

    elsif @state == :after_comment && @scanner.skip(BLANKLINES)
      make_major_para
      @state = :after_major_para

    elsif [:after_major_para, :after_page_delimiter, :after_heading].include?(@state) &&
          @scanner.skip(CODE)
      @nodes << Elisp::Code.new(@scanner[:content])
      @state = :after_code

    elsif @state == :after_comment && @scanner.skip(CODE)
      make_major_para
      @nodes << Elisp::Code.new(@scanner[:content])
      @state = :after_code

    elsif @state == :after_code && @scanner.skip(CODE_OR_BLANKLINES)
      @nodes.last << @scanner[:content]

    elsif @state == :after_code && @scanner.skip(/\f\n+/)
      @nodes << Elisp::PageDelimiter.new
      @state = :after_page_delimiter

    else
      raise Elisp::Error, <<~MESSAGE
        parse failed
        --- rest line (#{@state}) ---
        #{@scanner.rest.lines.first.inspect}
        --- scanner ---
        #{@scanner.inspect}
      MESSAGE
    end
  end
  self
end

#write(output, css: nil) ⇒ Object



144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
# File 'lib/elisp/parser.rb', line 144

def write(output, css: nil)
  css = css ? %(<link rel="stylesheet" href="#{CGI.escape(css)}">) : DEFAULT_CSS
  body = @nodes.map(&:html).join("\n")
  output.write(<<~END_HTML)
    <!doctype html>
    <html>
      <head>
        #{css}
        <meta charset="utf8">
        <meta name="viewport" content="width=device-width, initial-scale=1">
      </head>
      <body>
        <main>#{body}</main>
      </body>
    </html>
  END_HTML
end