1 /*
 2  * Copyright (c) 2024, Oracle and/or its affiliates. All rights reserved.
 3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 4  *
 5  * This code is free software; you can redistribute it and/or modify it
 6  * under the terms of the GNU General Public License version 2 only, as
 7  * published by the Free Software Foundation.  Oracle designates this
 8  * particular file as subject to the "Classpath" exception as provided
 9  * by Oracle in the LICENSE file that accompanied this code.
10  *
11  * This code is distributed in the hope that it will be useful, but WITHOUT
12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
14  * version 2 for more details (a copy is included in the LICENSE file that
15  * accompanied this code).
16  *
17  * You should have received a copy of the GNU General Public License version
18  * 2 along with this work; if not, write to the Free Software Foundation,
19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20  *
21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22  * or visit www.oracle.com if you need additional information or have any
23  * questions.
24  */
25 package hat.tools.textmodel.tokens;
26 
27 import java.util.Optional;
28 import java.util.function.Consumer;
29 
30 public interface Token extends Span {
31 
32     Parent parent();
33 
34     Pos pos();
35 
36     default int depth() {
37         return (parent() != null) ? parent().depth() + 1 : 0;
38     }
39 
40     int len();
41     @Override
42     default int startOffset() {
43         return (pos().textOffset());
44     }
45 
46     @Override
47     default int endOffset() {
48         return startOffset() + len();
49     }
50 
51     default String asString() {
52         return new String(pos().text(), pos().textOffset(), Math.min(len(), pos().text().length - pos().textOffset()));
53     }
54     default boolean next(Parent.TknPredicate1<Token> predicate) {
55         var opt = next();
56         if (opt.isPresent()) {
57             var o= opt.get();
58             return predicate.test(o);
59         }
60         return false;
61     }
62     default boolean next2(Parent.TknPredicate2<Token> predicate) {
63         var opt1 = next();
64         if (opt1.isPresent()) {
65             var opt2 = opt1.get().next();
66             if (opt2.isPresent()) {
67                 return   predicate.test(opt1.get(), opt2.get());
68             }
69         }
70         return false;
71     }
72     default boolean prev(Parent.TknPredicate1 predicate) {
73         var opt = prev();
74         if (opt.isPresent()) {
75             return predicate.test(opt.get());
76         }else  {
77             return false;
78         }
79     }
80     default Optional<Token> next(){
81         return parent().nextSiblingOf(this);
82     }
83     default Optional<Token> prev(){
84         return parent().prevSiblingOf(this);
85     }
86 
87     default boolean is(String s) {
88         var s1= asString();
89         return s1.equals(s);
90     }
91 
92     void visit(Consumer<Token> visitor);
93 
94 }