xref: /OpenGrok/opengrok-indexer/src/main/jflex/analysis/lua/LuaSymbolTokenizer.lex (revision d219b4cea555a12b602d2d5518daa22134ad4879)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * See LICENSE.txt included in this distribution for the specific
9  * language governing permissions and limitations under the License.
10  *
11  * When distributing Covered Code, include this CDDL HEADER in each
12  * file and include the License file at LICENSE.txt.
13  * If applicable, add the following below this CDDL HEADER, with the
14  * fields enclosed by brackets "[]" replaced with your own identifying
15  * information: Portions Copyright [yyyy] [name of copyright owner]
16  *
17  * CDDL HEADER END
18  */
19 
20 /*
21  * Copyright (c) 2016, 2021, Oracle and/or its affiliates. All rights reserved.
22  * Portions Copyright (c) 2017, Chris Fraire <cfraire@me.com>.
23  */
24 
25 /*
26  * Get Lua symbols - ignores comments, strings, keywords
27  */
28 
29 package org.opengrok.indexer.analysis.lua;
30 
31 import org.opengrok.indexer.analysis.JFlexSymbolMatcher;
32 
33 /**
34  * @author Evan Kinney
35  */
36 %%
37 %public
38 %class LuaSymbolTokenizer
39 %extends JFlexSymbolMatcher
40 %unicode
41 %int
42 %include ../CommonLexer.lexh
43 %char
44 %{
45     int bracketLevel;
46 
47     @Override
reset()48     public void reset() {
49         super.reset();
50         bracketLevel = 0;
51     }
52 %}
53 
54 %state STRING LSTRING COMMENT SCOMMENT QSTRING
55 
56 %include ../Common.lexh
57 %include Lua.lexh
58 %%
59 
60 <YYINITIAL> {
61     {Identifier} {
62         String id = yytext();
63         if (!Consts.kwd.contains(id)) {
64             onSymbolMatched(id, yychar);
65             return yystate();
66         }
67     }
68     {Number}    {}
69     \"     { yybegin(STRING);   }
70     "[" [=]* "["    {
71         String capture = yytext();
72         bracketLevel = LuaUtils.countOpeningLongBracket(capture);
73         yybegin(LSTRING);
74     }
75     \'     { yybegin(QSTRING);  }
76     "--[" [=]* "["    {
77         String capture = yytext();
78         String bracket = capture.substring(2);
79         bracketLevel = LuaUtils.countOpeningLongBracket(bracket);
80         yybegin(COMMENT);
81     }
82     "--"   { yybegin(SCOMMENT); }
83 }
84 
85 <STRING> {
86     \\[\"\\]    {}
87     \"   { yybegin(YYINITIAL); }
88 }
89 
90 <QSTRING> {
91     \\[\'\\]    {}
92     \' { yybegin(YYINITIAL); }
93 }
94 
95 <LSTRING, COMMENT> {
96     "]" [=]* "]"    {
97         String capture = yytext();
98         if (LuaUtils.isClosingLongBracket(capture, bracketLevel)) {
99             yybegin(YYINITIAL);
100         }
101     }
102 }
103 
104 <SCOMMENT> {
105     {WhspChar}*{EOL} { yybegin(YYINITIAL); }
106 }
107 
108 <YYINITIAL, STRING, LSTRING, COMMENT, SCOMMENT, QSTRING> {
109 {WhspChar}+    {}
110 [^] {}
111 }
112