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 26 package jdk.incubator.code.parser.impl; 27 28 import jdk.incubator.code.parser.impl.Position.LineMap; 29 import jdk.incubator.code.parser.impl.Tokens.Token; 30 import java.util.Arrays; 31 32 /** 33 * The lexical analyzer maps an input stream consisting of ASCII 34 * characters and Unicode escapes into a token sequence. 35 * 36 * <p><b>This is NOT part of any supported API. 37 * If you write code that depends on this, you do so at your own risk. 38 * This code and its internal interfaces are subject to change or 39 * deletion without notice.</b> 40 */ 41 public sealed interface Lexer permits Scanner { 42 43 /** 44 * Consume the next token. 45 */ 46 void nextToken(); 47 48 /** 49 * Return current token. 50 */ 51 Tokens.Token token(); 52 53 /** 54 * Return token with given lookahead. 55 */ 56 Tokens.Token token(int lookahead); 57 58 /** 59 * Return the last character position of the previous token. 60 */ 61 Tokens.Token prevToken(); 62 63 /** 64 * Return the position where a lexical error occurred; 65 */ 66 int errPos(); 67 68 /** 69 * Set the position where a lexical error occurred; 70 */ 71 void errPos(int pos); 72 73 /** 74 * Build a map for translating between line numbers and 75 * positions in the input. 76 * 77 * @return a LineMap 78 */ 79 LineMap getLineMap(); 80 81 default boolean is(Tokens.TokenKind tk) { 82 Tokens.Token t = token(); 83 if (t.kind == tk) { 84 return true; 85 } 86 return false; 87 } 88 89 default Tokens.Token accept(Tokens.TokenKind tk) { 90 Tokens.Token t = token(); 91 if (t.kind == tk) { 92 nextToken(); 93 return t; 94 } else { 95 // @@@ Exception 96 LineMap lineMap = getLineMap(); 97 int lineNumber = lineMap.getLineNumber(t.pos); 98 int columnNumber = lineMap.getColumnNumber(t.pos); 99 throw new IllegalArgumentException("Expected " + tk + " but observed " + t.kind + 100 " " + lineNumber + ":" + columnNumber); 101 } 102 } 103 104 default Tokens.Token accept(Tokens.TokenKind... tks) { 105 Token t = token(); 106 for (Tokens.TokenKind tk : tks) { 107 if (acceptIf(tk)) { 108 return t; 109 } 110 } 111 // @@@ Exception 112 LineMap lineMap = getLineMap(); 113 int lineNumber = lineMap.getLineNumber(t.pos); 114 int columnNumber = lineMap.getColumnNumber(t.pos); 115 throw new IllegalArgumentException("Expected one of " + Arrays.toString(tks) + " but observed " + t.kind + 116 " " + lineNumber + ":" + columnNumber); 117 } 118 119 default boolean acceptIf(Tokens.TokenKind tk) { 120 Tokens.Token t = token(); 121 if (t.kind == tk) { 122 nextToken(); 123 return true; 124 } 125 return false; 126 } 127 128 default RuntimeException unexpected() { 129 Tokens.Token t = token(); 130 LineMap lineMap = getLineMap(); 131 int lineNumber = lineMap.getLineNumber(t.pos); 132 int columnNumber = lineMap.getColumnNumber(t.pos); 133 return new IllegalArgumentException("Unexpected token " + t.kind + 134 " " + lineNumber + ":" + columnNumber); 135 } 136 }