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) 2020, Chris Fraire <cfraire@me.com>.
22  */
23 
24 package org.opengrok.indexer.util;
25 
26 import java.io.IOException;
27 import java.util.List;
28 %%
29 %public
30 %class SourceSplitterScanner
31 %char
32 %unicode
33 %type boolean
34 %eofval{
35     return false;
36 %eofval}
37 %eof{
38     /*
39      * Following JFlexXref's custom, an empty file or a file ending with EOL
40      * produces an additional line of length zero.
41      */
42     lines.add(builder.toString());
43     builder.setLength(0);
44 
45     length = yychar;
46 %eof}
47 %{
48     private final StringBuilder builder = new StringBuilder();
49 
50     private int length;
51 
52     private List<String> lines;
53 
getLength()54     public int getLength() {
55         return length;
56     }
57 
58     /**
59      * Sets the required target to write.
60      * @param lines a required instance
61      */
setTarget(List<String> lines)62     public void setTarget(List<String> lines) {
63         this.builder.setLength(0);
64         this.length = 0;
65         this.lines = lines;
66     }
67 
68     /**
69      * Call {@link #yylex()} until {@code false}, which consumes all input so
70      * that the argument to {@link #setTarget(List)} contains the entire
71      * transformation.
72      */
consume()73     public void consume() throws IOException {
74         while (yylex()) {
75             //noinspection UnnecessaryContinue
76             continue;
77         }
78     }
79 %}
80 
81 %include Common.lexh
82 %%
83 
84 {EOL}    {
85     for (int i = 0; i < yylength(); ++i) {
86         builder.append(yycharat(i)); // faster than yytext()
87     }
88     lines.add(builder.toString());
89     builder.setLength(0);
90 }
91 
92 [^\n\r]    {
93     for (int i = 0; i < yylength(); ++i) {
94         builder.append(yycharat(i)); // faster than yytext()
95     }
96 }
97