xref: /OpenGrok/opengrok-indexer/src/main/jflex/util/LineBreakerScanner.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) 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 LineBreakerScanner
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. We also ensure there are two
41      * entries to describe the boundaries.
42      */
43     offsets.add(yychar);
44     length = yychar;
45 %eof}
46 %{
47     private long length;
48 
49     private List<Long> offsets;
50 
getLength()51     public long getLength() {
52         return length;
53     }
54 
55     /**
56      * Sets the required target to write, and adds a first offset of 0.
57      * @param offsets a required instance
58      */
setTarget(List<Long> offsets)59     public void setTarget(List<Long> offsets) {
60         this.length = 0;
61         this.offsets = offsets;
62         offsets.add(0L);
63     }
64 
65     /**
66      * Call {@link #yylex()} until {@code false}, which consumes all input so
67      * that the argument to {@link #setTarget(List)} contains the entire
68      * transformation.
69      */
consume()70     public void consume() throws IOException {
71         while (yylex()) {
72             //noinspection UnnecessaryContinue
73             continue;
74         }
75     }
76 %}
77 
78 %include ../analysis/Common.lexh
79 %%
80 
81 {EOL}    {
82     offsets.add(yychar + yylength());
83 }
84 
85 [^\n\r]    {
86 }
87