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