Home | History | Annotate | Download | only in parse
      1 /*
      2  * Copyright 2016 Google Inc. All Rights Reserved.
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *     http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 
     17 package com.google.turbine.parse;
     18 
     19 import com.google.turbine.diag.SourceFile;
     20 import java.util.Iterator;
     21 
     22 /**
     23  * A {@link Lexer} that wraps an iterator over an existing token stream.
     24  *
     25  * <p>Used when parsing pre-processed constant expression initializers.
     26  */
     27 public class IteratorLexer implements Lexer {
     28 
     29   private final SourceFile source;
     30   private final Iterator<SavedToken> it;
     31   private SavedToken curr;
     32 
     33   public IteratorLexer(SourceFile source, Iterator<SavedToken> it) {
     34     this.source = source;
     35     this.it = it;
     36   }
     37 
     38   @Override
     39   public SourceFile source() {
     40     return source;
     41   }
     42 
     43   @Override
     44   public Token next() {
     45     if (it.hasNext()) {
     46       curr = it.next();
     47       return curr.token;
     48     }
     49     return Token.EOF;
     50   }
     51 
     52   @Override
     53   public String stringValue() {
     54     return curr.value;
     55   }
     56 
     57   @Override
     58   public int position() {
     59     // TODO(cushon): test expression position EOF handling
     60     return curr != null ? curr.position : -1;
     61   }
     62 }
     63