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 * Cross reference a Lua file 27 */ 28 29 package org.opengrok.indexer.analysis.lua; 30 31 import java.io.IOException; 32 import org.opengrok.indexer.analysis.JFlexSymbolMatcher; 33 import org.opengrok.indexer.util.StringUtils; 34 import org.opengrok.indexer.web.HtmlConsts; 35 36 /** 37 * @author Evan Kinney 38 */ 39 %% 40 %public 41 %class LuaXref 42 %extends JFlexSymbolMatcher 43 %unicode 44 %int 45 %char 46 %include ../CommonLexer.lexh 47 %include ../CommonXref.lexh 48 %{ 49 int bracketLevel; 50 51 @Override reset()52 public void reset() { 53 super.reset(); 54 bracketLevel = 0; 55 } 56 57 @Override yypop()58 public void yypop() throws IOException { 59 onDisjointSpanChanged(null, yychar); 60 super.yypop(); 61 } 62 chkLOC()63 protected void chkLOC() { 64 switch (yystate()) { 65 case COMMENT: 66 case SCOMMENT: 67 break; 68 default: 69 phLOC(); 70 break; 71 } 72 } 73 %} 74 75 File = [a-zA-Z]{FNameChar}* "." ([Ll][Uu][Aa] | [Tt][Xx][Tt] | 76 [Hh][Tt][Mm][Ll]? | [Dd][Ii][Ff][Ff] | [Pp][Aa][Tt][Cc][Hh]) 77 78 %state STRING LSTRING COMMENT SCOMMENT QSTRING 79 80 %include ../Common.lexh 81 %include ../CommonURI.lexh 82 %include ../CommonPath.lexh 83 %include Lua.lexh 84 %% 85 <YYINITIAL> { 86 {Identifier} { 87 chkLOC(); 88 String id = yytext(); 89 onFilteredSymbolMatched(id, yychar, Consts.kwd); 90 } 91 {Number} { 92 chkLOC(); 93 onDisjointSpanChanged(HtmlConsts.NUMBER_CLASS, yychar); 94 onNonSymbolMatched(yytext(), yychar); 95 onDisjointSpanChanged(null, yychar); 96 } 97 \" { 98 chkLOC(); 99 yypush(STRING); 100 onDisjointSpanChanged(HtmlConsts.STRING_CLASS, yychar); 101 onNonSymbolMatched(yytext(), yychar); 102 } 103 "[" [=]* "[" { 104 chkLOC(); 105 String capture = yytext(); 106 bracketLevel = LuaUtils.countOpeningLongBracket(capture); 107 yypush(LSTRING); 108 onDisjointSpanChanged(HtmlConsts.STRING_CLASS, yychar); 109 onNonSymbolMatched(capture, yychar); 110 } 111 \' { 112 chkLOC(); 113 yypush(QSTRING); 114 onDisjointSpanChanged(HtmlConsts.STRING_CLASS, yychar); 115 onNonSymbolMatched(yytext(), yychar); 116 } 117 "--[" [=]* "[" { 118 String capture = yytext(); 119 String bracket = capture.substring(2); 120 bracketLevel = LuaUtils.countOpeningLongBracket(bracket); 121 yypush(COMMENT); 122 onDisjointSpanChanged(HtmlConsts.COMMENT_CLASS, yychar); 123 onNonSymbolMatched(capture, yychar); 124 } 125 "--" { 126 yypush(SCOMMENT); 127 onDisjointSpanChanged(HtmlConsts.COMMENT_CLASS, yychar); 128 onNonSymbolMatched(yytext(), yychar); 129 } 130 } 131 132 "<" ({File}|{FPath}) ">" { 133 chkLOC(); 134 onNonSymbolMatched("<", yychar); 135 String path = yytext(); 136 path = path.substring(1, path.length() - 1); 137 onFilelikeMatched(path, yychar + 1); 138 onNonSymbolMatched(">", yychar + 1 + path.length()); 139 } 140 141 <STRING> { 142 \\[\"\\] | 143 \" {WhspChar}+ \" { chkLOC(); onNonSymbolMatched(yytext(), yychar); } 144 \" { 145 chkLOC(); 146 onNonSymbolMatched(yytext(), yychar); 147 yypop(); 148 } 149 } 150 151 <QSTRING> { 152 \\[\'\\] | 153 \' {WhspChar}+ \' { chkLOC(); onNonSymbolMatched(yytext(), yychar); } 154 \' { 155 chkLOC(); 156 onNonSymbolMatched(yytext(), yychar); 157 yypop(); 158 } 159 } 160 161 <LSTRING, COMMENT> { 162 "]" [=]* "]" { 163 chkLOC(); 164 String capture = yytext(); 165 onNonSymbolMatched(capture, yychar); 166 if (LuaUtils.isClosingLongBracket(capture, bracketLevel)) yypop(); 167 } 168 } 169 170 <STRING, QSTRING, LSTRING> { 171 {WhspChar}*{EOL} { 172 onDisjointSpanChanged(null, yychar); 173 onEndOfLineMatched(yytext(), yychar); 174 onDisjointSpanChanged(HtmlConsts.STRING_CLASS, yychar); 175 } 176 } 177 178 <COMMENT> { 179 {WhspChar}*{EOL} { 180 onDisjointSpanChanged(null, yychar); 181 onEndOfLineMatched(yytext(), yychar); 182 onDisjointSpanChanged(HtmlConsts.COMMENT_CLASS, yychar); 183 } 184 } 185 186 <SCOMMENT> { 187 {WhspChar}*{EOL} { 188 yypop(); 189 onEndOfLineMatched(yytext(), yychar); 190 } 191 } 192 193 <YYINITIAL, STRING, LSTRING, COMMENT, SCOMMENT, QSTRING> { 194 {WhspChar}*{EOL} { onEndOfLineMatched(yytext(), yychar); } 195 [[\s]--[\n]] { onNonSymbolMatched(yytext(), yychar); } 196 [^\n] { chkLOC(); onNonSymbolMatched(yytext(), yychar); } 197 } 198 199 <STRING, LSTRING, COMMENT, SCOMMENT, QSTRING> { 200 {FPath} { 201 chkLOC(); 202 onPathlikeMatched(yytext(), '/', false, yychar); 203 } 204 {File} { 205 chkLOC(); 206 String path = yytext(); 207 onFilelikeMatched(path, yychar); 208 } 209 {FNameChar}+ "@" {FNameChar}+ "." {FNameChar}+ { 210 chkLOC(); 211 onEmailAddressMatched(yytext(), yychar); 212 } 213 } 214 215 <STRING, LSTRING, COMMENT, SCOMMENT> { 216 {BrowseableURI} { 217 chkLOC(); 218 onUriMatched(yytext(), yychar); 219 } 220 } 221 <QSTRING> { 222 {BrowseableURI} { 223 chkLOC(); 224 onUriMatched(yytext(), yychar, StringUtils.APOS_NO_BSESC); 225 } 226 } 227