001/* 002 * Copyright (c) 2012-2018 Institut National des Sciences Appliquées de Lyon (INSA Lyon) and others 003 * 004 * This program and the accompanying materials are made available under the 005 * terms of the Eclipse Public License 2.0 which is available at 006 * http://www.eclipse.org/legal/epl-2.0. 007 * 008 * SPDX-License-Identifier: EPL-2.0 009 */ 010 011package org.eclipse.golo.compiler.parser; 012 013import java.io.IOException; 014 015/** 016 * JavaCharStream extension allowing to track token offsets. 017 */ 018public class JavaOffsetCharStream extends JavaCharStream { 019 020 private int beginOffset; 021 022 private int currentOffset; 023 024 public JavaOffsetCharStream(final JavaCharStream delegate) { 025 super(delegate.inputStream); 026 } 027 028 @Override 029 public char BeginToken() throws IOException { 030 /* 031 * JavaCC use a pre fetch buffer and may not call readChar causing our 032 * offset not to be updated 033 */ 034 if (inBuf > 0) { 035 currentOffset++; 036 } 037 char c = super.BeginToken(); 038 beginOffset = currentOffset; 039 return c; 040 } 041 042 @Override 043 public char readChar() throws IOException { 044 char c = super.readChar(); 045 currentOffset++; 046 return c; 047 } 048 049 @Override 050 public void backup(int amount) { 051 super.backup(amount); 052 currentOffset -= amount; 053 } 054 055 public int getBeginOffset() { 056 return beginOffset; 057 } 058 059 public int getCurrentOffset() { 060 return currentOffset; 061 } 062}