1
+ ###
2
+ # test ruby built-in lexers
3
+ # answer questions
4
+ # does end-of-line comment include newline in lexeme - yes/no?
5
+ #
6
+ # - [[6, 21], :on_comment, "## a comment here\n", END],
7
+
8
+
9
+
10
+ require 'ripper'
11
+ require 'pp'
12
+
13
+ code = <<STR
14
+
15
+
16
+ 5.times do | x |
17
+ puts x
18
+ puts "hello"
19
+ puts 'hello' ## a comment here
20
+ ## another comment here
21
+ ## another here
22
+
23
+ ## yet another here
24
+ end
25
+
26
+
27
+ STR
28
+
29
+
30
+ puts code
31
+ pp Ripper . lex ( code )
32
+
33
+
34
+ puts code
35
+ ## unknown keyword: :keep_tokens
36
+ ## note: requires ruby 3.2+ or such - double check!!!!
37
+ pp RubyVM ::AbstractSyntaxTree . parse ( code ,
38
+ keep_tokens : true ) . tokens
39
+
40
+ # =>
41
+ # [[0, :tIDENTIFIER, "puts", [1, 0, 1, 4]],
42
+ # [1, :"(", "(", [1, 4, 1, 5]],
43
+ # [2, :tSTRING_BEG, "'", [1, 5, 1, 6]],
44
+ # [3, :tSTRING_CONTENT, "test", [1, 6, 1, 10]],
45
+ # [4, :tSTRING_END, "'", [1, 10, 1, 11]],
46
+ # [5, :",", ",", [1, 11, 1, 12]],
47
+ # [6, :tSP, " ", [1, 12, 1, 13]],
48
+ # [7, :")", ")", [1, 13, 1, 14]]]
49
+
50
+
51
+ __END__
52
+
53
+ [[[1, 0], :on_ignored_nl, "\n", BEG],
54
+ [[2, 0], :on_ignored_nl, "\n", BEG],
55
+ [[3, 0], :on_int, "5", END],
56
+ [[3, 1], :on_period, ".", DOT],
57
+ [[3, 2], :on_ident, "times", ARG],
58
+ [[3, 7], :on_sp, " ", ARG],
59
+ [[3, 11], :on_kw, "do", BEG],
60
+ [[3, 13], :on_sp, " ", BEG],
61
+ [[3, 17], :on_op, "|", BEG|LABEL],
62
+ [[3, 18], :on_sp, " ", BEG|LABEL],
63
+ [[3, 22], :on_ident, "x", ARG],
64
+ [[3, 23], :on_sp, " ", ARG],
65
+ [[3, 27], :on_op, "|", BEG|LABEL],
66
+ [[3, 28], :on_ignored_nl, "\n", BEG|LABEL],
67
+ [[4, 0], :on_sp, "\t", BEG|LABEL],
68
+ [[4, 1], :on_ident, "puts", CMDARG],
69
+ [[4, 5], :on_sp, " ", CMDARG],
70
+ [[4, 6], :on_ident, "x", END|LABEL],
71
+ [[4, 7], :on_nl, "\n", BEG],
72
+ [[5, 0], :on_sp, " ", BEG],
73
+ [[5, 2], :on_ident, "puts", CMDARG],
74
+ [[5, 6], :on_sp, " ", CMDARG],
75
+ [[5, 7], :on_tstring_beg, "\"", CMDARG],
76
+ [[5, 8], :on_tstring_content, "hello", CMDARG],
77
+ [[5, 13], :on_tstring_end, "\"", END],
78
+ [[5, 14], :on_nl, "\n", BEG],
79
+ [[6, 0], :on_sp, " ", BEG],
80
+ [[6, 2], :on_ident, "puts", CMDARG],
81
+ [[6, 6], :on_sp, " ", CMDARG],
82
+ [[6, 7], :on_tstring_beg, "'", CMDARG],
83
+ [[6, 8], :on_tstring_content, "hello", CMDARG],
84
+ [[6, 13], :on_tstring_end, "'", END],
85
+ [[6, 14], :on_sp, " ", END],
86
+ [[6, 21], :on_comment, "## a comment here\n", END],
87
+ [[7, 0], :on_kw, "end", END],
88
+ [[7, 3], :on_nl, "\n", BEG],
89
+ [[8, 0], :on_ignored_nl, "\n", BEG],
90
+ [[9, 0], :on_ignored_nl, "\n", BEG]]
0 commit comments