oedipus_lex 2.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- checksums.yaml.gz.sig +0 -0
- data.tar.gz.sig +1 -0
- data/.autotest +26 -0
- data/.gemtest +0 -0
- data/History.rdoc +6 -0
- data/Manifest.txt +27 -0
- data/README.rdoc +468 -0
- data/Rakefile +72 -0
- data/lib/oedipus_lex.rake +16 -0
- data/lib/oedipus_lex.rb +274 -0
- data/lib/oedipus_lex.rex +51 -0
- data/lib/oedipus_lex.rex.rb +144 -0
- data/rex-mode.el +58 -0
- data/sample/calc3.racc +54 -0
- data/sample/calc3.rex +15 -0
- data/sample/error1.rex +15 -0
- data/sample/error1.txt +2 -0
- data/sample/error2.rex +15 -0
- data/sample/sample.html +32 -0
- data/sample/sample.rex +15 -0
- data/sample/sample.xhtml +32 -0
- data/sample/sample1.c +9 -0
- data/sample/sample1.rex +43 -0
- data/sample/sample2.bas +4 -0
- data/sample/sample2.rex +30 -0
- data/sample/xhtmlparser.html +7 -0
- data/sample/xhtmlparser.racc +66 -0
- data/sample/xhtmlparser.rex +70 -0
- data/sample/xhtmlparser.xhtml +10 -0
- data/test/test_oedipus_lex.rb +629 -0
- metadata +164 -0
- metadata.gz.sig +0 -0
data/rex-mode.el
ADDED
@@ -0,0 +1,58 @@
|
|
1
|
+
;;; rex-mode.el --- Generic mode for rex/rexical/oedipus_lex files
|
2
|
+
|
3
|
+
;; Copyright (c) Ryan Davis, seattle.rb
|
4
|
+
;;
|
5
|
+
;; Author: Ryan Davis <ryand-ruby@zenspider.com>
|
6
|
+
;; Keywords: languages
|
7
|
+
|
8
|
+
;; (The MIT License)
|
9
|
+
;;
|
10
|
+
;; Permission is hereby granted, free of charge, to any person obtaining
|
11
|
+
;; a copy of this software and associated documentation files (the
|
12
|
+
;; 'Software'), to deal in the Software without restriction, including
|
13
|
+
;; without limitation the rights to use, copy, modify, merge, publish,
|
14
|
+
;; distribute, sublicense, and/or sell copies of the Software, and to
|
15
|
+
;; permit persons to whom the Software is furnished to do so, subject to
|
16
|
+
;; the following conditions:
|
17
|
+
;;
|
18
|
+
;; The above copyright notice and this permission notice shall be
|
19
|
+
;; included in all copies or substantial portions of the Software.
|
20
|
+
;;
|
21
|
+
;; THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
|
22
|
+
;; EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
23
|
+
;; MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
|
24
|
+
;; IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
|
25
|
+
;; CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
|
26
|
+
;; TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
|
27
|
+
;; SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
28
|
+
|
29
|
+
;;; Commentary:
|
30
|
+
|
31
|
+
;; woot
|
32
|
+
|
33
|
+
;;; Code:
|
34
|
+
|
35
|
+
(define-generic-mode rex-mode
|
36
|
+
;; comments
|
37
|
+
'(?#)
|
38
|
+
|
39
|
+
;; keywords
|
40
|
+
nil
|
41
|
+
|
42
|
+
;; font-lock-faces
|
43
|
+
'(("/\\(\\\\.\\|[^/]\\)*/" . font-lock-string-face)
|
44
|
+
(":[a-zA-Z_][a-zA-Z0-9_]*" . font-lock-variable-name-face)
|
45
|
+
("^ *\\([A-Z][A-Z0-9_]*\\)" 1 font-lock-variable-name-face)
|
46
|
+
("^\\(?:end\\|inner\\|macro\\|option\\|rule\\)" . font-lock-keyword-face)
|
47
|
+
("class [A-Z][a-zA-Z_]+" . font-lock-keyword-face))
|
48
|
+
|
49
|
+
;; auto-mode
|
50
|
+
'("\\.rex$")
|
51
|
+
|
52
|
+
;; functions
|
53
|
+
nil
|
54
|
+
|
55
|
+
"Simple generic mode for rex/rexical/t-rex files")
|
56
|
+
|
57
|
+
(provide 'rex-mode)
|
58
|
+
;;; rex-mode.el ends here
|
data/sample/calc3.racc
ADDED
@@ -0,0 +1,54 @@
|
|
1
|
+
#
|
2
|
+
# A simple calculator, version 3.
|
3
|
+
#
|
4
|
+
|
5
|
+
class Calculator3
|
6
|
+
prechigh
|
7
|
+
nonassoc UMINUS
|
8
|
+
left '*' '/'
|
9
|
+
left '+' '-'
|
10
|
+
preclow
|
11
|
+
options no_result_var
|
12
|
+
rule
|
13
|
+
target : exp
|
14
|
+
| /* none */ { 0 }
|
15
|
+
|
16
|
+
exp : exp '+' exp { val[0] + val[2] }
|
17
|
+
| exp '-' exp { val[0] - val[2] }
|
18
|
+
| exp '*' exp { val[0] * val[2] }
|
19
|
+
| exp '/' exp { val[0] / val[2] }
|
20
|
+
| '(' exp ')' { val[1] }
|
21
|
+
| '-' NUMBER =UMINUS { -(val[1]) }
|
22
|
+
| NUMBER
|
23
|
+
end
|
24
|
+
|
25
|
+
---- header ----
|
26
|
+
#
|
27
|
+
# generated by racc
|
28
|
+
#
|
29
|
+
require 'calc3.rex'
|
30
|
+
|
31
|
+
---- inner ----
|
32
|
+
|
33
|
+
---- footer ----
|
34
|
+
|
35
|
+
if $stdin.tty? then
|
36
|
+
puts 'sample calc'
|
37
|
+
puts '"q" to quit.'
|
38
|
+
end
|
39
|
+
calc = Calculator3.new
|
40
|
+
|
41
|
+
while true
|
42
|
+
if $stdin.tty? then
|
43
|
+
print '>>> '; $stdout.flush
|
44
|
+
end
|
45
|
+
str = $stdin.gets.strip
|
46
|
+
break if /q/i === str
|
47
|
+
begin
|
48
|
+
p calc.parse str
|
49
|
+
rescue ParseError
|
50
|
+
puts 'parse error'
|
51
|
+
end
|
52
|
+
|
53
|
+
break unless $stdin.tty?
|
54
|
+
end
|
data/sample/calc3.rex
ADDED
data/sample/error1.rex
ADDED
data/sample/error1.txt
ADDED
data/sample/error2.rex
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
#
|
2
|
+
# error2.rex
|
3
|
+
# lexical definition sample for rex
|
4
|
+
#
|
5
|
+
|
6
|
+
class Error2
|
7
|
+
macro
|
8
|
+
BLANK /[\ \t]+/
|
9
|
+
rule
|
10
|
+
/{{BLANK}}/ # no action
|
11
|
+
/\d+/ { [:digit, text.to_i] }
|
12
|
+
/\w+/ { [:word, text] }
|
13
|
+
/\n/
|
14
|
+
/./ { self.state = :NONDEF ; [text, text] }
|
15
|
+
end
|
data/sample/sample.html
ADDED
@@ -0,0 +1,32 @@
|
|
1
|
+
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
|
2
|
+
"http://www.w3.org/TR/html4/loose.dtd">
|
3
|
+
<html>
|
4
|
+
<head>
|
5
|
+
<!-- Comment -->
|
6
|
+
<title> Title </title>
|
7
|
+
<meta http-equiv="Content-Type" content="text/html; charset=Shift_JIS">
|
8
|
+
<meta name="robots" content="noindex,nofollow">
|
9
|
+
<link rel="stylesheet" type="text/css" href="sample.css">
|
10
|
+
</head>
|
11
|
+
<script language="JavaScript">
|
12
|
+
<!--
|
13
|
+
function foo(msg) {
|
14
|
+
ndx = Math.floor(Math.random() * msg.length);
|
15
|
+
return msg[ndx];
|
16
|
+
}
|
17
|
+
//-->
|
18
|
+
</script>
|
19
|
+
<body>
|
20
|
+
<div id="div1" class="div">
|
21
|
+
<p id="p1" class="p">
|
22
|
+
HTML
|
23
|
+
4.01
|
24
|
+
</p>
|
25
|
+
</div>
|
26
|
+
<form action="sample.cgi" method="post">
|
27
|
+
<p>
|
28
|
+
<input type="text" name="t1" id="t1" value="TEXT1">
|
29
|
+
</p>
|
30
|
+
</form>
|
31
|
+
</body>
|
32
|
+
</html>
|
data/sample/sample.rex
ADDED
data/sample/sample.xhtml
ADDED
@@ -0,0 +1,32 @@
|
|
1
|
+
<?xml version="1.0" encoding="Shift_JIS"?>
|
2
|
+
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
|
3
|
+
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
|
4
|
+
<html xmlns="http://www.w3.org/1999/xhtml">
|
5
|
+
<head>
|
6
|
+
<!-- Comment -->
|
7
|
+
<title>Title</title>
|
8
|
+
<meta http-equiv="Content-Type" content="text/html; charset=Shift_JIS" />
|
9
|
+
<meta name="robots" content="noindex,nofollow" />
|
10
|
+
<link rel="stylesheet" type="text/css" href="sample.css" />
|
11
|
+
</head>
|
12
|
+
<script language="JavaScript">
|
13
|
+
<!--
|
14
|
+
function foo(msg) {
|
15
|
+
ndx = Math.floor(Math.random() * msg.length);
|
16
|
+
return msg[ndx];
|
17
|
+
}
|
18
|
+
//-->
|
19
|
+
</script>
|
20
|
+
<body>
|
21
|
+
<div id="div1" class="div">
|
22
|
+
<p id="p1" class="p">
|
23
|
+
XHTML 1.1
|
24
|
+
</p>
|
25
|
+
</div>
|
26
|
+
<form action="sample.cgi" method="post">
|
27
|
+
<p>
|
28
|
+
<input type="text" name="t1" id="t1" value="TEXT1" />
|
29
|
+
</p>
|
30
|
+
</form>
|
31
|
+
</body>
|
32
|
+
</html>
|
data/sample/sample1.c
ADDED
data/sample/sample1.rex
ADDED
@@ -0,0 +1,43 @@
|
|
1
|
+
#
|
2
|
+
# sample1.rex
|
3
|
+
# lexical definition sample for rex
|
4
|
+
#
|
5
|
+
# usage
|
6
|
+
# rex sample1.rex --stub
|
7
|
+
# ruby sample1.rex.rb sample1.c
|
8
|
+
#
|
9
|
+
|
10
|
+
class Sample1
|
11
|
+
macro
|
12
|
+
BLANK /\s+/
|
13
|
+
REM_IN /\/\*/
|
14
|
+
REM_OUT /\*\//
|
15
|
+
REM /\/\//
|
16
|
+
|
17
|
+
rule
|
18
|
+
|
19
|
+
# [:state] pattern [actions]
|
20
|
+
|
21
|
+
# remark
|
22
|
+
/{{REM_IN}}/ :REMS
|
23
|
+
:REMS /{{REM_OUT}}/ { [:state, nil] }
|
24
|
+
:REMS /.*(?={{REM_OUT}})/ { [:remark, text] }
|
25
|
+
/{{REM}}/ :REM
|
26
|
+
:REM /\n/ { [:state, nil] }
|
27
|
+
:REM /.*(?=$)/ { [:remark, text] }
|
28
|
+
|
29
|
+
# literal
|
30
|
+
/\"[^"]*\"/ { [:string, text] }
|
31
|
+
/\'[^']\'/ { [:character, text] }
|
32
|
+
|
33
|
+
# skip
|
34
|
+
/{{BLANK}}/
|
35
|
+
|
36
|
+
# numeric
|
37
|
+
/\d+/ { [:digit, text.to_i] }
|
38
|
+
|
39
|
+
# identifier
|
40
|
+
/\w+/ { [:word, text] }
|
41
|
+
/./ { [text, text] }
|
42
|
+
|
43
|
+
end
|
data/sample/sample2.bas
ADDED
data/sample/sample2.rex
ADDED
@@ -0,0 +1,30 @@
|
|
1
|
+
#
|
2
|
+
# sample2.rex
|
3
|
+
# lexical definition sample for rex
|
4
|
+
#
|
5
|
+
# usage
|
6
|
+
# rex sample2.rex --stub
|
7
|
+
# ruby sample2.rex.rb sample2.bas
|
8
|
+
#
|
9
|
+
|
10
|
+
class Sample2
|
11
|
+
macro
|
12
|
+
BLANK /\s+/
|
13
|
+
REMARK /\'/ # '
|
14
|
+
|
15
|
+
rule
|
16
|
+
/{{REMARK}}/ :REM
|
17
|
+
:REM /\n/ { [:state, nil] }
|
18
|
+
:REM /.*(?=$)/ { [:remark, text] }
|
19
|
+
|
20
|
+
/\"[^"]*\"/ { [:string, text] }
|
21
|
+
|
22
|
+
/{{BLANK}}/ # no action
|
23
|
+
|
24
|
+
/INPUT/i { [:input, text] }
|
25
|
+
/PRINT/i { [:print, text] }
|
26
|
+
|
27
|
+
/\d+/ { [:digit, text.to_i] }
|
28
|
+
/\w+/ { [:word, text] }
|
29
|
+
/./ { [text, text] }
|
30
|
+
end
|
@@ -0,0 +1,66 @@
|
|
1
|
+
#
|
2
|
+
# xml parser
|
3
|
+
#
|
4
|
+
|
5
|
+
class XHTMLParser
|
6
|
+
|
7
|
+
rule
|
8
|
+
target : /* none */
|
9
|
+
| xml_doc
|
10
|
+
|
11
|
+
xml_doc : xml_header extra xml_body
|
12
|
+
| xml_header xml_body
|
13
|
+
| xml_body
|
14
|
+
|
15
|
+
xml_header : xtag_in element attributes xtag_out
|
16
|
+
|
17
|
+
xml_body : tag_from contents tag_to
|
18
|
+
|
19
|
+
tag_from : tag_in element attributes tag_out
|
20
|
+
|
21
|
+
tag_empty : tag_in element attributes etag_out
|
22
|
+
|
23
|
+
tag_to : etag_in element tag_out
|
24
|
+
|
25
|
+
attributes : /* none */
|
26
|
+
| attributes attribute
|
27
|
+
|
28
|
+
attribute : attr equal quoted
|
29
|
+
|
30
|
+
quoted : quote1 value quote1
|
31
|
+
| quote2 value quote2
|
32
|
+
|
33
|
+
contents : /* none */
|
34
|
+
| contents content
|
35
|
+
|
36
|
+
content : text
|
37
|
+
| extra
|
38
|
+
| tag_from contents tag_to
|
39
|
+
| tag_empty
|
40
|
+
|
41
|
+
extra : tag_in ext extra_texts tag_out
|
42
|
+
|
43
|
+
extra_texts : /* none */
|
44
|
+
| extra_texts rem_in remtexts rem_out
|
45
|
+
| extra_texts exttext
|
46
|
+
|
47
|
+
remtexts : remtext
|
48
|
+
| remtexts remtext
|
49
|
+
|
50
|
+
end
|
51
|
+
|
52
|
+
---- header ----
|
53
|
+
#
|
54
|
+
# generated by racc
|
55
|
+
#
|
56
|
+
require 'xhtmlparser.rex'
|
57
|
+
---- inner ----
|
58
|
+
|
59
|
+
---- footer ----
|
60
|
+
|
61
|
+
exit if ARGV.size == 0
|
62
|
+
filename = ARGV.shift
|
63
|
+
|
64
|
+
htmlparser = XHTMLParser.new
|
65
|
+
htmlparser.scan_file filename
|
66
|
+
|
@@ -0,0 +1,70 @@
|
|
1
|
+
#
|
2
|
+
# xhtmlparser.rex
|
3
|
+
# lexical scanner definition for rex
|
4
|
+
#
|
5
|
+
# usage
|
6
|
+
# rex xhtmlparser.rex --stub
|
7
|
+
# ruby xhtmlparser.rex.rb sample.xhtml
|
8
|
+
#
|
9
|
+
|
10
|
+
class XHTMLParser
|
11
|
+
|
12
|
+
macro
|
13
|
+
BLANK /\s+/
|
14
|
+
TAG_IN /\</
|
15
|
+
TAG_OUT /\>/
|
16
|
+
ETAG_IN /\<\//
|
17
|
+
ETAG_OUT /\/\>/
|
18
|
+
XTAG_IN /\<\?/
|
19
|
+
XTAG_OUT /\?\>/
|
20
|
+
EXT /\!/
|
21
|
+
REM /\-\-/
|
22
|
+
EQUAL /\=/
|
23
|
+
Q1 /\'/
|
24
|
+
Q2 /\"/
|
25
|
+
|
26
|
+
rule
|
27
|
+
|
28
|
+
# [:state] pattern [actions]
|
29
|
+
/{{XTAG_IN}}/ { state = :TAG; [:xtag_in, text] }
|
30
|
+
/{{ETAG_IN}}/ { state = :TAG; [:etag_in, text] }
|
31
|
+
/{{TAG_IN}}/ { state = :TAG; [:tag_in, text] }
|
32
|
+
:TAG /{{EXT}}/ { state = :EXT; [:ext, text] }
|
33
|
+
|
34
|
+
:EXT /{{REM}}/ { state = :REM; [:rem_in, text] }
|
35
|
+
:EXT /{{XTAG_OUT}}/ { state = nil; [:xtag_out, text] }
|
36
|
+
:EXT /{{TAG_OUT}}/ { state = nil; [:tag_out, text] }
|
37
|
+
:EXT /.+(?={{REM}})/ { [:exttext, text] }
|
38
|
+
:EXT /.+(?={{TAG_OUT}})/ { [:exttext, text] }
|
39
|
+
:EXT /.+(?=$)/ { [:exttext, text] }
|
40
|
+
:EXT /\n/
|
41
|
+
|
42
|
+
:REM /{{REM}}/ { state = :EXT; [:rem_out, text] }
|
43
|
+
:REM /.+(?={{REM}})/ { [:remtext, text] }
|
44
|
+
:REM /.+(?=$)/ { [:remtext, text] }
|
45
|
+
:REM /\n/
|
46
|
+
|
47
|
+
:TAG /{{BLANK}}/
|
48
|
+
:TAG /{{XTAG_OUT}}/ { state = nil; [:xtag_out, text] }
|
49
|
+
:TAG /{{ETAG_OUT}}/ { state = nil; [:etag_out, text] }
|
50
|
+
:TAG /{{TAG_OUT}}/ { state = nil; [:tag_out, text] }
|
51
|
+
:TAG /{{EQUAL}}/ { [:equal, text] }
|
52
|
+
:TAG /{{Q1}}/ { state = :Q1; [:quote1, text] }
|
53
|
+
:Q1 /{{Q1}}/ { state = :TAG; [:quote1, text] }
|
54
|
+
:Q1 /[^{{Q1}}]+(?={{Q1}})/ { [:value, text] }
|
55
|
+
:TAG /{{Q2}}/ { state = :Q2; [:quote2, text] }
|
56
|
+
:Q2 /{{Q2}}/ { state = :TAG; [:quote2, text] }
|
57
|
+
:Q2 /[^{{Q2}}]+(?={{Q2}})/ { [:value, text] }
|
58
|
+
|
59
|
+
:TAG /[\w\-]+(?={{EQUAL}})/ { [:attr, text] }
|
60
|
+
:TAG /[\w\-]+/ { [:element, text] }
|
61
|
+
|
62
|
+
/\s+(?=\S)/
|
63
|
+
/.*\S(?=\s*{{ETAG_IN}})/ { [:text, text] }
|
64
|
+
/.*\S(?=\s*{{TAG_IN}})/ { [:text, text] }
|
65
|
+
/.*\S(?=\s*$)/ { [:text, text] }
|
66
|
+
/\s+(?=$)/
|
67
|
+
|
68
|
+
inner
|
69
|
+
|
70
|
+
end
|