target
stringlengths
20
113k
src_fm
stringlengths
11
86.3k
src_fm_fc
stringlengths
21
86.4k
src_fm_fc_co
stringlengths
30
86.4k
src_fm_fc_ms
stringlengths
42
86.8k
src_fm_fc_ms_ff
stringlengths
43
86.8k
@Test public void isFirstLoadInProgress_false() throws Exception { NowPlayingViewModel nowPlayingViewModel = new NowPlayingViewModel(mockApplication, mockServiceGateway); testScheduler.triggerActions(); testScheduler.advanceTimeBy(10, TimeUnit.SECONDS); assertThat(nowPlayingViewModel.getFirstLoad().get()).isFalse(); }
@NonNull public ObservableBoolean getFirstLoad() { return firstLoad; }
NowPlayingViewModel extends ViewModel { @NonNull public ObservableBoolean getFirstLoad() { return firstLoad; } }
NowPlayingViewModel extends ViewModel { @NonNull public ObservableBoolean getFirstLoad() { return firstLoad; } @Inject NowPlayingViewModel(@NonNull Application application, @NonNull ServiceGateway serviceGateway); }
NowPlayingViewModel extends ViewModel { @NonNull public ObservableBoolean getFirstLoad() { return firstLoad; } @Inject NowPlayingViewModel(@NonNull Application application, @NonNull ServiceGateway serviceGateway); @Override void onCleared(); @NonNull List<MovieViewInfo> getMovieViewInfoList(); void loadMoreNowPlayingInfo(); @NonNull Observable<Boolean> getShowErrorObserver(); @NonNull ObservableField<String> getToolbarTitle(); @NonNull ObservableBoolean getFirstLoad(); @NonNull BehaviorSubject<Boolean> getLoadMoreEnabledBehaviorSubject(); @NonNull PublishSubject<AdapterUiCommand> getAdapterDataPublishSubject(); }
NowPlayingViewModel extends ViewModel { @NonNull public ObservableBoolean getFirstLoad() { return firstLoad; } @Inject NowPlayingViewModel(@NonNull Application application, @NonNull ServiceGateway serviceGateway); @Override void onCleared(); @NonNull List<MovieViewInfo> getMovieViewInfoList(); void loadMoreNowPlayingInfo(); @NonNull Observable<Boolean> getShowErrorObserver(); @NonNull ObservableField<String> getToolbarTitle(); @NonNull ObservableBoolean getFirstLoad(); @NonNull BehaviorSubject<Boolean> getLoadMoreEnabledBehaviorSubject(); @NonNull PublishSubject<AdapterUiCommand> getAdapterDataPublishSubject(); }
@Test public void getMovieViewInfoList() throws Exception { NowPlayingViewModel nowPlayingViewModel = new NowPlayingViewModel(mockApplication, mockServiceGateway); List<MovieViewInfo> movieViewInfos = nowPlayingViewModel.getMovieViewInfoList(); assertThat(movieViewInfos.size()).isEqualTo(0); }
@NonNull public List<MovieViewInfo> getMovieViewInfoList() { return movieViewInfoList; }
NowPlayingViewModel extends ViewModel { @NonNull public List<MovieViewInfo> getMovieViewInfoList() { return movieViewInfoList; } }
NowPlayingViewModel extends ViewModel { @NonNull public List<MovieViewInfo> getMovieViewInfoList() { return movieViewInfoList; } @Inject NowPlayingViewModel(@NonNull Application application, @NonNull ServiceGateway serviceGateway); }
NowPlayingViewModel extends ViewModel { @NonNull public List<MovieViewInfo> getMovieViewInfoList() { return movieViewInfoList; } @Inject NowPlayingViewModel(@NonNull Application application, @NonNull ServiceGateway serviceGateway); @Override void onCleared(); @NonNull List<MovieViewInfo> getMovieViewInfoList(); void loadMoreNowPlayingInfo(); @NonNull Observable<Boolean> getShowErrorObserver(); @NonNull ObservableField<String> getToolbarTitle(); @NonNull ObservableBoolean getFirstLoad(); @NonNull BehaviorSubject<Boolean> getLoadMoreEnabledBehaviorSubject(); @NonNull PublishSubject<AdapterUiCommand> getAdapterDataPublishSubject(); }
NowPlayingViewModel extends ViewModel { @NonNull public List<MovieViewInfo> getMovieViewInfoList() { return movieViewInfoList; } @Inject NowPlayingViewModel(@NonNull Application application, @NonNull ServiceGateway serviceGateway); @Override void onCleared(); @NonNull List<MovieViewInfo> getMovieViewInfoList(); void loadMoreNowPlayingInfo(); @NonNull Observable<Boolean> getShowErrorObserver(); @NonNull ObservableField<String> getToolbarTitle(); @NonNull ObservableBoolean getFirstLoad(); @NonNull BehaviorSubject<Boolean> getLoadMoreEnabledBehaviorSubject(); @NonNull PublishSubject<AdapterUiCommand> getAdapterDataPublishSubject(); }
@Test public void isAtScrollEnd() throws Exception { LinearLayoutManager mockLinearLayoutManager = Mockito.mock(LinearLayoutManager.class); RecyclerView mockRecyclerView = Mockito.mock(RecyclerView.class); RecyclerViewScrollEvent mockRecyclerViewScrollEvent = Mockito.mock(RecyclerViewScrollEvent.class); when(mockLinearLayoutManager.getItemCount()).thenReturn(50); when(mockLinearLayoutManager.findLastVisibleItemPosition()).thenReturn(50); when(mockRecyclerView.getLayoutManager()).thenReturn(mockLinearLayoutManager); when(mockRecyclerViewScrollEvent.dx()).thenReturn(0); when(mockRecyclerViewScrollEvent.dy()).thenReturn(100); when(mockRecyclerViewScrollEvent.view()).thenReturn(mockRecyclerView); ScrollEventCalculator scrollEventCalculator = new ScrollEventCalculator(mockRecyclerViewScrollEvent); boolean value = scrollEventCalculator.isAtScrollEnd(); assertThat(value).isTrue(); }
public boolean isAtScrollEnd() { RecyclerView.LayoutManager layoutManager = recyclerViewScrollEvent.view().getLayoutManager(); if (layoutManager instanceof LinearLayoutManager) { LinearLayoutManager linearLayoutManager = (LinearLayoutManager) layoutManager; int totalItemCount = linearLayoutManager.getItemCount(); int lastVisibleItem = linearLayoutManager.findLastVisibleItemPosition(); return totalItemCount <= (lastVisibleItem + 2); } else { return false; } }
ScrollEventCalculator { public boolean isAtScrollEnd() { RecyclerView.LayoutManager layoutManager = recyclerViewScrollEvent.view().getLayoutManager(); if (layoutManager instanceof LinearLayoutManager) { LinearLayoutManager linearLayoutManager = (LinearLayoutManager) layoutManager; int totalItemCount = linearLayoutManager.getItemCount(); int lastVisibleItem = linearLayoutManager.findLastVisibleItemPosition(); return totalItemCount <= (lastVisibleItem + 2); } else { return false; } } }
ScrollEventCalculator { public boolean isAtScrollEnd() { RecyclerView.LayoutManager layoutManager = recyclerViewScrollEvent.view().getLayoutManager(); if (layoutManager instanceof LinearLayoutManager) { LinearLayoutManager linearLayoutManager = (LinearLayoutManager) layoutManager; int totalItemCount = linearLayoutManager.getItemCount(); int lastVisibleItem = linearLayoutManager.findLastVisibleItemPosition(); return totalItemCount <= (lastVisibleItem + 2); } else { return false; } } ScrollEventCalculator(RecyclerViewScrollEvent recyclerViewScrollEvent); }
ScrollEventCalculator { public boolean isAtScrollEnd() { RecyclerView.LayoutManager layoutManager = recyclerViewScrollEvent.view().getLayoutManager(); if (layoutManager instanceof LinearLayoutManager) { LinearLayoutManager linearLayoutManager = (LinearLayoutManager) layoutManager; int totalItemCount = linearLayoutManager.getItemCount(); int lastVisibleItem = linearLayoutManager.findLastVisibleItemPosition(); return totalItemCount <= (lastVisibleItem + 2); } else { return false; } } ScrollEventCalculator(RecyclerViewScrollEvent recyclerViewScrollEvent); boolean isAtScrollEnd(); }
ScrollEventCalculator { public boolean isAtScrollEnd() { RecyclerView.LayoutManager layoutManager = recyclerViewScrollEvent.view().getLayoutManager(); if (layoutManager instanceof LinearLayoutManager) { LinearLayoutManager linearLayoutManager = (LinearLayoutManager) layoutManager; int totalItemCount = linearLayoutManager.getItemCount(); int lastVisibleItem = linearLayoutManager.findLastVisibleItemPosition(); return totalItemCount <= (lastVisibleItem + 2); } else { return false; } } ScrollEventCalculator(RecyclerViewScrollEvent recyclerViewScrollEvent); boolean isAtScrollEnd(); }
@Test public void isAtScrollEnd_false() throws Exception { LinearLayoutManager mockLinearLayoutManager = Mockito.mock(LinearLayoutManager.class); RecyclerView mockRecyclerView = Mockito.mock(RecyclerView.class); RecyclerViewScrollEvent mockRecyclerViewScrollEvent = Mockito.mock(RecyclerViewScrollEvent.class); when(mockLinearLayoutManager.getItemCount()).thenReturn(50); when(mockLinearLayoutManager.findLastVisibleItemPosition()).thenReturn(5); when(mockRecyclerView.getLayoutManager()).thenReturn(mockLinearLayoutManager); when(mockRecyclerViewScrollEvent.dx()).thenReturn(0); when(mockRecyclerViewScrollEvent.dy()).thenReturn(100); when(mockRecyclerViewScrollEvent.view()).thenReturn(mockRecyclerView); ScrollEventCalculator scrollEventCalculator = new ScrollEventCalculator(mockRecyclerViewScrollEvent); boolean value = scrollEventCalculator.isAtScrollEnd(); assertThat(value).isFalse(); }
public boolean isAtScrollEnd() { RecyclerView.LayoutManager layoutManager = recyclerViewScrollEvent.view().getLayoutManager(); if (layoutManager instanceof LinearLayoutManager) { LinearLayoutManager linearLayoutManager = (LinearLayoutManager) layoutManager; int totalItemCount = linearLayoutManager.getItemCount(); int lastVisibleItem = linearLayoutManager.findLastVisibleItemPosition(); return totalItemCount <= (lastVisibleItem + 2); } else { return false; } }
ScrollEventCalculator { public boolean isAtScrollEnd() { RecyclerView.LayoutManager layoutManager = recyclerViewScrollEvent.view().getLayoutManager(); if (layoutManager instanceof LinearLayoutManager) { LinearLayoutManager linearLayoutManager = (LinearLayoutManager) layoutManager; int totalItemCount = linearLayoutManager.getItemCount(); int lastVisibleItem = linearLayoutManager.findLastVisibleItemPosition(); return totalItemCount <= (lastVisibleItem + 2); } else { return false; } } }
ScrollEventCalculator { public boolean isAtScrollEnd() { RecyclerView.LayoutManager layoutManager = recyclerViewScrollEvent.view().getLayoutManager(); if (layoutManager instanceof LinearLayoutManager) { LinearLayoutManager linearLayoutManager = (LinearLayoutManager) layoutManager; int totalItemCount = linearLayoutManager.getItemCount(); int lastVisibleItem = linearLayoutManager.findLastVisibleItemPosition(); return totalItemCount <= (lastVisibleItem + 2); } else { return false; } } ScrollEventCalculator(RecyclerViewScrollEvent recyclerViewScrollEvent); }
ScrollEventCalculator { public boolean isAtScrollEnd() { RecyclerView.LayoutManager layoutManager = recyclerViewScrollEvent.view().getLayoutManager(); if (layoutManager instanceof LinearLayoutManager) { LinearLayoutManager linearLayoutManager = (LinearLayoutManager) layoutManager; int totalItemCount = linearLayoutManager.getItemCount(); int lastVisibleItem = linearLayoutManager.findLastVisibleItemPosition(); return totalItemCount <= (lastVisibleItem + 2); } else { return false; } } ScrollEventCalculator(RecyclerViewScrollEvent recyclerViewScrollEvent); boolean isAtScrollEnd(); }
ScrollEventCalculator { public boolean isAtScrollEnd() { RecyclerView.LayoutManager layoutManager = recyclerViewScrollEvent.view().getLayoutManager(); if (layoutManager instanceof LinearLayoutManager) { LinearLayoutManager linearLayoutManager = (LinearLayoutManager) layoutManager; int totalItemCount = linearLayoutManager.getItemCount(); int lastVisibleItem = linearLayoutManager.findLastVisibleItemPosition(); return totalItemCount <= (lastVisibleItem + 2); } else { return false; } } ScrollEventCalculator(RecyclerViewScrollEvent recyclerViewScrollEvent); boolean isAtScrollEnd(); }
@Test public void isSingleton() throws Exception { assertEquals(mRxBus, RxBus.getDefault()); }
public static RxBus getDefault() { return sInstance; }
RxBus { public static RxBus getDefault() { return sInstance; } }
RxBus { public static RxBus getDefault() { return sInstance; } private RxBus(); }
RxBus { public static RxBus getDefault() { return sInstance; } private RxBus(); static RxBus getDefault(); @SuppressWarnings("ConstantConditions") void post(@android.support.annotation.NonNull Event event); Observable<Event> register(@android.support.annotation.NonNull final String tag); Observable<Event> register(@android.support.annotation.NonNull final Class aClass); Observable<Event> register(@android.support.annotation.NonNull final String[] tags); Observable<Event> register(@android.support.annotation.NonNull final Class[] aClass); }
RxBus { public static RxBus getDefault() { return sInstance; } private RxBus(); static RxBus getDefault(); @SuppressWarnings("ConstantConditions") void post(@android.support.annotation.NonNull Event event); Observable<Event> register(@android.support.annotation.NonNull final String tag); Observable<Event> register(@android.support.annotation.NonNull final Class aClass); Observable<Event> register(@android.support.annotation.NonNull final String[] tags); Observable<Event> register(@android.support.annotation.NonNull final Class[] aClass); }
@Test public void getPhone() throws Exception { BarcodeInfo.Phone phone = new BarcodeInfo.Phone("7894561230"); BarcodeInfo barcodeInfo = new BarcodeInfo(); barcodeInfo.setPhone(phone); assertEquals(barcodeInfo.getPhone(), phone); }
Phone getPhone() { return mPhone; }
BarcodeInfo { Phone getPhone() { return mPhone; } }
BarcodeInfo { Phone getPhone() { return mPhone; } }
BarcodeInfo { Phone getPhone() { return mPhone; } void setBoundingBox(Rect boundingBox); void setContact(Contact contact); void setUrlBookmark(UrlBookmark urlBookmark); void setSms(Sms sms); void setGeoPoint(GeoPoint geoPoint); void setCalendarEvent(CalendarEvent calendarEvent); void setPhone(Phone phone); void setRawValue(String rawValue); }
BarcodeInfo { Phone getPhone() { return mPhone; } void setBoundingBox(Rect boundingBox); void setContact(Contact contact); void setUrlBookmark(UrlBookmark urlBookmark); void setSms(Sms sms); void setGeoPoint(GeoPoint geoPoint); void setCalendarEvent(CalendarEvent calendarEvent); void setPhone(Phone phone); void setRawValue(String rawValue); }
@Test public void getRawValue() throws Exception { BarcodeInfo barcodeInfo = new BarcodeInfo(); barcodeInfo.setRawValue("Raw value"); assertEquals(barcodeInfo.getRawValue(), "Raw value"); }
String getRawValue() { return mRawValue; }
BarcodeInfo { String getRawValue() { return mRawValue; } }
BarcodeInfo { String getRawValue() { return mRawValue; } }
BarcodeInfo { String getRawValue() { return mRawValue; } void setBoundingBox(Rect boundingBox); void setContact(Contact contact); void setUrlBookmark(UrlBookmark urlBookmark); void setSms(Sms sms); void setGeoPoint(GeoPoint geoPoint); void setCalendarEvent(CalendarEvent calendarEvent); void setPhone(Phone phone); void setRawValue(String rawValue); }
BarcodeInfo { String getRawValue() { return mRawValue; } void setBoundingBox(Rect boundingBox); void setContact(Contact contact); void setUrlBookmark(UrlBookmark urlBookmark); void setSms(Sms sms); void setGeoPoint(GeoPoint geoPoint); void setCalendarEvent(CalendarEvent calendarEvent); void setPhone(Phone phone); void setRawValue(String rawValue); }
@SuppressWarnings("ConstantConditions") @Test public void postNull() throws Exception { try { mRxBus.post(null); fail(); } catch (IllegalArgumentException e) { } }
@SuppressWarnings("ConstantConditions") public void post(@android.support.annotation.NonNull Event event) { if (event == null) throw new IllegalArgumentException("Event cannot be null."); sBus.onNext(event); }
RxBus { @SuppressWarnings("ConstantConditions") public void post(@android.support.annotation.NonNull Event event) { if (event == null) throw new IllegalArgumentException("Event cannot be null."); sBus.onNext(event); } }
RxBus { @SuppressWarnings("ConstantConditions") public void post(@android.support.annotation.NonNull Event event) { if (event == null) throw new IllegalArgumentException("Event cannot be null."); sBus.onNext(event); } private RxBus(); }
RxBus { @SuppressWarnings("ConstantConditions") public void post(@android.support.annotation.NonNull Event event) { if (event == null) throw new IllegalArgumentException("Event cannot be null."); sBus.onNext(event); } private RxBus(); static RxBus getDefault(); @SuppressWarnings("ConstantConditions") void post(@android.support.annotation.NonNull Event event); Observable<Event> register(@android.support.annotation.NonNull final String tag); Observable<Event> register(@android.support.annotation.NonNull final Class aClass); Observable<Event> register(@android.support.annotation.NonNull final String[] tags); Observable<Event> register(@android.support.annotation.NonNull final Class[] aClass); }
RxBus { @SuppressWarnings("ConstantConditions") public void post(@android.support.annotation.NonNull Event event) { if (event == null) throw new IllegalArgumentException("Event cannot be null."); sBus.onNext(event); } private RxBus(); static RxBus getDefault(); @SuppressWarnings("ConstantConditions") void post(@android.support.annotation.NonNull Event event); Observable<Event> register(@android.support.annotation.NonNull final String tag); Observable<Event> register(@android.support.annotation.NonNull final Class aClass); Observable<Event> register(@android.support.annotation.NonNull final String[] tags); Observable<Event> register(@android.support.annotation.NonNull final Class[] aClass); }
@Test public void getTag() throws Exception { String mockTag = "Test"; Event event = new Event(mockTag); assertEquals(event.getTag(), mockTag); Integer mockObj = 34; event = new Event(mockObj); assertEquals(event.getTag(), mockObj.getClass().getSimpleName()); }
@android.support.annotation.NonNull public String getTag() { return mTag; }
Event { @android.support.annotation.NonNull public String getTag() { return mTag; } }
Event { @android.support.annotation.NonNull public String getTag() { return mTag; } @SuppressWarnings("ConstantConditions") Event(@android.support.annotation.NonNull String tag); @SuppressWarnings("ConstantConditions") Event(@android.support.annotation.NonNull Object o); }
Event { @android.support.annotation.NonNull public String getTag() { return mTag; } @SuppressWarnings("ConstantConditions") Event(@android.support.annotation.NonNull String tag); @SuppressWarnings("ConstantConditions") Event(@android.support.annotation.NonNull Object o); @android.support.annotation.NonNull String getTag(); Object getObject(); }
Event { @android.support.annotation.NonNull public String getTag() { return mTag; } @SuppressWarnings("ConstantConditions") Event(@android.support.annotation.NonNull String tag); @SuppressWarnings("ConstantConditions") Event(@android.support.annotation.NonNull Object o); @android.support.annotation.NonNull String getTag(); Object getObject(); }
@Test public void getObject() throws Exception { Integer mockObj = 34; Event event = new Event(mockObj); assertEquals(event.getObject(), mockObj); }
public Object getObject() { return mObject; }
Event { public Object getObject() { return mObject; } }
Event { public Object getObject() { return mObject; } @SuppressWarnings("ConstantConditions") Event(@android.support.annotation.NonNull String tag); @SuppressWarnings("ConstantConditions") Event(@android.support.annotation.NonNull Object o); }
Event { public Object getObject() { return mObject; } @SuppressWarnings("ConstantConditions") Event(@android.support.annotation.NonNull String tag); @SuppressWarnings("ConstantConditions") Event(@android.support.annotation.NonNull Object o); @android.support.annotation.NonNull String getTag(); Object getObject(); }
Event { public Object getObject() { return mObject; } @SuppressWarnings("ConstantConditions") Event(@android.support.annotation.NonNull String tag); @SuppressWarnings("ConstantConditions") Event(@android.support.annotation.NonNull Object o); @android.support.annotation.NonNull String getTag(); Object getObject(); }
@SuppressWarnings("ConstantConditions") @Test public void checkLLabel() throws Exception { try { new InfoModel(null); fail(); } catch (IllegalArgumentException e) { } InfoModel infoModel = new InfoModel(MOCK_LABEL); assertEquals(infoModel.getLabel(), MOCK_LABEL); }
public String getLabel() { return label; }
InfoModel { public String getLabel() { return label; } }
InfoModel { public String getLabel() { return label; } InfoModel(@NonNull String label); }
InfoModel { public String getLabel() { return label; } InfoModel(@NonNull String label); String getInfo(); void setInfo(@NonNull String info); String getImageUrl(); void setImageUrl(String imageUrl); String getLabel(); }
InfoModel { public String getLabel() { return label; } InfoModel(@NonNull String label); String getInfo(); void setInfo(@NonNull String info); String getImageUrl(); void setImageUrl(String imageUrl); String getLabel(); }
@Test public void getContact() throws Exception { BarcodeInfo.Contact contact = new BarcodeInfo.Contact(); BarcodeInfo barcodeInfo = new BarcodeInfo(); barcodeInfo.setContact(contact); assertEquals(barcodeInfo.getContact(), contact); }
Contact getContact() { return mContact; }
BarcodeInfo { Contact getContact() { return mContact; } }
BarcodeInfo { Contact getContact() { return mContact; } }
BarcodeInfo { Contact getContact() { return mContact; } void setBoundingBox(Rect boundingBox); void setContact(Contact contact); void setUrlBookmark(UrlBookmark urlBookmark); void setSms(Sms sms); void setGeoPoint(GeoPoint geoPoint); void setCalendarEvent(CalendarEvent calendarEvent); void setPhone(Phone phone); void setRawValue(String rawValue); }
BarcodeInfo { Contact getContact() { return mContact; } void setBoundingBox(Rect boundingBox); void setContact(Contact contact); void setUrlBookmark(UrlBookmark urlBookmark); void setSms(Sms sms); void setGeoPoint(GeoPoint geoPoint); void setCalendarEvent(CalendarEvent calendarEvent); void setPhone(Phone phone); void setRawValue(String rawValue); }
@Test public void getUrlBookmark() throws Exception { BarcodeInfo.UrlBookmark urlBookmark = new BarcodeInfo.UrlBookmark("Google", "www.google.com"); BarcodeInfo barcodeInfo = new BarcodeInfo(); barcodeInfo.setUrlBookmark(urlBookmark); assertEquals(barcodeInfo.getUrlBookmark(), urlBookmark); }
UrlBookmark getUrlBookmark() { return mUrlBookmark; }
BarcodeInfo { UrlBookmark getUrlBookmark() { return mUrlBookmark; } }
BarcodeInfo { UrlBookmark getUrlBookmark() { return mUrlBookmark; } }
BarcodeInfo { UrlBookmark getUrlBookmark() { return mUrlBookmark; } void setBoundingBox(Rect boundingBox); void setContact(Contact contact); void setUrlBookmark(UrlBookmark urlBookmark); void setSms(Sms sms); void setGeoPoint(GeoPoint geoPoint); void setCalendarEvent(CalendarEvent calendarEvent); void setPhone(Phone phone); void setRawValue(String rawValue); }
BarcodeInfo { UrlBookmark getUrlBookmark() { return mUrlBookmark; } void setBoundingBox(Rect boundingBox); void setContact(Contact contact); void setUrlBookmark(UrlBookmark urlBookmark); void setSms(Sms sms); void setGeoPoint(GeoPoint geoPoint); void setCalendarEvent(CalendarEvent calendarEvent); void setPhone(Phone phone); void setRawValue(String rawValue); }
@Test public void getSms() throws Exception { BarcodeInfo.Sms sms = new BarcodeInfo.Sms("This is test message.", "1234567890"); BarcodeInfo info = new BarcodeInfo(); info.setSms(sms); assertEquals(info.getSms(), sms); }
Sms getSms() { return mSms; }
BarcodeInfo { Sms getSms() { return mSms; } }
BarcodeInfo { Sms getSms() { return mSms; } }
BarcodeInfo { Sms getSms() { return mSms; } void setBoundingBox(Rect boundingBox); void setContact(Contact contact); void setUrlBookmark(UrlBookmark urlBookmark); void setSms(Sms sms); void setGeoPoint(GeoPoint geoPoint); void setCalendarEvent(CalendarEvent calendarEvent); void setPhone(Phone phone); void setRawValue(String rawValue); }
BarcodeInfo { Sms getSms() { return mSms; } void setBoundingBox(Rect boundingBox); void setContact(Contact contact); void setUrlBookmark(UrlBookmark urlBookmark); void setSms(Sms sms); void setGeoPoint(GeoPoint geoPoint); void setCalendarEvent(CalendarEvent calendarEvent); void setPhone(Phone phone); void setRawValue(String rawValue); }
@Test public void getGeoPoint() throws Exception { BarcodeInfo.GeoPoint geoPoint = new BarcodeInfo.GeoPoint(23.0225, 72.5714); BarcodeInfo info = new BarcodeInfo(); info.setGeoPoint(geoPoint); assertEquals(info.getGeoPoint(), geoPoint); }
GeoPoint getGeoPoint() { return mGeoPoint; }
BarcodeInfo { GeoPoint getGeoPoint() { return mGeoPoint; } }
BarcodeInfo { GeoPoint getGeoPoint() { return mGeoPoint; } }
BarcodeInfo { GeoPoint getGeoPoint() { return mGeoPoint; } void setBoundingBox(Rect boundingBox); void setContact(Contact contact); void setUrlBookmark(UrlBookmark urlBookmark); void setSms(Sms sms); void setGeoPoint(GeoPoint geoPoint); void setCalendarEvent(CalendarEvent calendarEvent); void setPhone(Phone phone); void setRawValue(String rawValue); }
BarcodeInfo { GeoPoint getGeoPoint() { return mGeoPoint; } void setBoundingBox(Rect boundingBox); void setContact(Contact contact); void setUrlBookmark(UrlBookmark urlBookmark); void setSms(Sms sms); void setGeoPoint(GeoPoint geoPoint); void setCalendarEvent(CalendarEvent calendarEvent); void setPhone(Phone phone); void setRawValue(String rawValue); }
@Test public void getCalendarEvent() throws Exception { BarcodeInfo.CalendarEvent calendarEvent = new BarcodeInfo.CalendarEvent(); BarcodeInfo barcodeInfo = new BarcodeInfo(); barcodeInfo.setCalendarEvent(calendarEvent); assertEquals(barcodeInfo.getCalendarEvent(), calendarEvent); }
CalendarEvent getCalendarEvent() { return mCalendarEvent; }
BarcodeInfo { CalendarEvent getCalendarEvent() { return mCalendarEvent; } }
BarcodeInfo { CalendarEvent getCalendarEvent() { return mCalendarEvent; } }
BarcodeInfo { CalendarEvent getCalendarEvent() { return mCalendarEvent; } void setBoundingBox(Rect boundingBox); void setContact(Contact contact); void setUrlBookmark(UrlBookmark urlBookmark); void setSms(Sms sms); void setGeoPoint(GeoPoint geoPoint); void setCalendarEvent(CalendarEvent calendarEvent); void setPhone(Phone phone); void setRawValue(String rawValue); }
BarcodeInfo { CalendarEvent getCalendarEvent() { return mCalendarEvent; } void setBoundingBox(Rect boundingBox); void setContact(Contact contact); void setUrlBookmark(UrlBookmark urlBookmark); void setSms(Sms sms); void setGeoPoint(GeoPoint geoPoint); void setCalendarEvent(CalendarEvent calendarEvent); void setPhone(Phone phone); void setRawValue(String rawValue); }
@Test public void shouldFindImplementation() { Class<? extends DummyComponent> clazz = ClasspathUtils .findImplementationClass(DummyComponent.class); assertEquals(clazz, DummyComponentImpl.class); }
@SuppressWarnings("unchecked") public static <T> Class<? extends T> findImplementationClass(Class<T> clazz) { if (!Modifier.isAbstract(clazz.getModifiers())) return clazz; String implementationClass = String.format("%s.impl.%sImpl", clazz.getPackage().getName(), clazz.getSimpleName()); try { return (Class<? extends T>) Class.forName(implementationClass); } catch (ClassNotFoundException e) { throw new NoImplementationClassFoundException(clazz, e); } }
ClasspathUtils { @SuppressWarnings("unchecked") public static <T> Class<? extends T> findImplementationClass(Class<T> clazz) { if (!Modifier.isAbstract(clazz.getModifiers())) return clazz; String implementationClass = String.format("%s.impl.%sImpl", clazz.getPackage().getName(), clazz.getSimpleName()); try { return (Class<? extends T>) Class.forName(implementationClass); } catch (ClassNotFoundException e) { throw new NoImplementationClassFoundException(clazz, e); } } }
ClasspathUtils { @SuppressWarnings("unchecked") public static <T> Class<? extends T> findImplementationClass(Class<T> clazz) { if (!Modifier.isAbstract(clazz.getModifiers())) return clazz; String implementationClass = String.format("%s.impl.%sImpl", clazz.getPackage().getName(), clazz.getSimpleName()); try { return (Class<? extends T>) Class.forName(implementationClass); } catch (ClassNotFoundException e) { throw new NoImplementationClassFoundException(clazz, e); } } private ClasspathUtils(); }
ClasspathUtils { @SuppressWarnings("unchecked") public static <T> Class<? extends T> findImplementationClass(Class<T> clazz) { if (!Modifier.isAbstract(clazz.getModifiers())) return clazz; String implementationClass = String.format("%s.impl.%sImpl", clazz.getPackage().getName(), clazz.getSimpleName()); try { return (Class<? extends T>) Class.forName(implementationClass); } catch (ClassNotFoundException e) { throw new NoImplementationClassFoundException(clazz, e); } } private ClasspathUtils(); @SuppressWarnings("unchecked") static Class<? extends T> findImplementationClass(Class<T> clazz); }
ClasspathUtils { @SuppressWarnings("unchecked") public static <T> Class<? extends T> findImplementationClass(Class<T> clazz) { if (!Modifier.isAbstract(clazz.getModifiers())) return clazz; String implementationClass = String.format("%s.impl.%sImpl", clazz.getPackage().getName(), clazz.getSimpleName()); try { return (Class<? extends T>) Class.forName(implementationClass); } catch (ClassNotFoundException e) { throw new NoImplementationClassFoundException(clazz, e); } } private ClasspathUtils(); @SuppressWarnings("unchecked") static Class<? extends T> findImplementationClass(Class<T> clazz); }
@Test(expectedExceptions = RuntimeException.class) public void shouldThrowExceptionWhenNoImplementationIsFound() { ClasspathUtils.findImplementationClass(List.class); }
@SuppressWarnings("unchecked") public static <T> Class<? extends T> findImplementationClass(Class<T> clazz) { if (!Modifier.isAbstract(clazz.getModifiers())) return clazz; String implementationClass = String.format("%s.impl.%sImpl", clazz.getPackage().getName(), clazz.getSimpleName()); try { return (Class<? extends T>) Class.forName(implementationClass); } catch (ClassNotFoundException e) { throw new NoImplementationClassFoundException(clazz, e); } }
ClasspathUtils { @SuppressWarnings("unchecked") public static <T> Class<? extends T> findImplementationClass(Class<T> clazz) { if (!Modifier.isAbstract(clazz.getModifiers())) return clazz; String implementationClass = String.format("%s.impl.%sImpl", clazz.getPackage().getName(), clazz.getSimpleName()); try { return (Class<? extends T>) Class.forName(implementationClass); } catch (ClassNotFoundException e) { throw new NoImplementationClassFoundException(clazz, e); } } }
ClasspathUtils { @SuppressWarnings("unchecked") public static <T> Class<? extends T> findImplementationClass(Class<T> clazz) { if (!Modifier.isAbstract(clazz.getModifiers())) return clazz; String implementationClass = String.format("%s.impl.%sImpl", clazz.getPackage().getName(), clazz.getSimpleName()); try { return (Class<? extends T>) Class.forName(implementationClass); } catch (ClassNotFoundException e) { throw new NoImplementationClassFoundException(clazz, e); } } private ClasspathUtils(); }
ClasspathUtils { @SuppressWarnings("unchecked") public static <T> Class<? extends T> findImplementationClass(Class<T> clazz) { if (!Modifier.isAbstract(clazz.getModifiers())) return clazz; String implementationClass = String.format("%s.impl.%sImpl", clazz.getPackage().getName(), clazz.getSimpleName()); try { return (Class<? extends T>) Class.forName(implementationClass); } catch (ClassNotFoundException e) { throw new NoImplementationClassFoundException(clazz, e); } } private ClasspathUtils(); @SuppressWarnings("unchecked") static Class<? extends T> findImplementationClass(Class<T> clazz); }
ClasspathUtils { @SuppressWarnings("unchecked") public static <T> Class<? extends T> findImplementationClass(Class<T> clazz) { if (!Modifier.isAbstract(clazz.getModifiers())) return clazz; String implementationClass = String.format("%s.impl.%sImpl", clazz.getPackage().getName(), clazz.getSimpleName()); try { return (Class<? extends T>) Class.forName(implementationClass); } catch (ClassNotFoundException e) { throw new NoImplementationClassFoundException(clazz, e); } } private ClasspathUtils(); @SuppressWarnings("unchecked") static Class<? extends T> findImplementationClass(Class<T> clazz); }
@Test public void shouldCreatePageWithWebDriverConstructorAndInitElements() { DummyPageWithWebDriverConstructor page = WisePageFactory.initElements( this.driver, DummyPageWithWebDriverConstructor.class); assertNotNull(page.getDummyComponent()); }
public static <T> T initElements(WebDriver driver, Class<T> clazz) { T instance = instantiatePage(driver, clazz); return initElements(driver, instance); }
WisePageFactory { public static <T> T initElements(WebDriver driver, Class<T> clazz) { T instance = instantiatePage(driver, clazz); return initElements(driver, instance); } }
WisePageFactory { public static <T> T initElements(WebDriver driver, Class<T> clazz) { T instance = instantiatePage(driver, clazz); return initElements(driver, instance); } private WisePageFactory(); }
WisePageFactory { public static <T> T initElements(WebDriver driver, Class<T> clazz) { T instance = instantiatePage(driver, clazz); return initElements(driver, instance); } private WisePageFactory(); static T initElements(WebDriver driver, Class<T> clazz); static T initElements(SearchContext searchContext, T instance); }
WisePageFactory { public static <T> T initElements(WebDriver driver, Class<T> clazz) { T instance = instantiatePage(driver, clazz); return initElements(driver, instance); } private WisePageFactory(); static T initElements(WebDriver driver, Class<T> clazz); static T initElements(SearchContext searchContext, T instance); }
@Test public void shouldCreatePageWithNoArgConstructorAndInitElements() { DummyPageWithNoArgConstructor page = WisePageFactory.initElements(this.driver, DummyPageWithNoArgConstructor.class); assertNotNull(page.getDummyComponent()); }
public static <T> T initElements(WebDriver driver, Class<T> clazz) { T instance = instantiatePage(driver, clazz); return initElements(driver, instance); }
WisePageFactory { public static <T> T initElements(WebDriver driver, Class<T> clazz) { T instance = instantiatePage(driver, clazz); return initElements(driver, instance); } }
WisePageFactory { public static <T> T initElements(WebDriver driver, Class<T> clazz) { T instance = instantiatePage(driver, clazz); return initElements(driver, instance); } private WisePageFactory(); }
WisePageFactory { public static <T> T initElements(WebDriver driver, Class<T> clazz) { T instance = instantiatePage(driver, clazz); return initElements(driver, instance); } private WisePageFactory(); static T initElements(WebDriver driver, Class<T> clazz); static T initElements(SearchContext searchContext, T instance); }
WisePageFactory { public static <T> T initElements(WebDriver driver, Class<T> clazz) { T instance = instantiatePage(driver, clazz); return initElements(driver, instance); } private WisePageFactory(); static T initElements(WebDriver driver, Class<T> clazz); static T initElements(SearchContext searchContext, T instance); }
@Test public void shouldInitElementsOfInstance() { DummyPageWithWebDriverConstructor page = new DummyPageWithWebDriverConstructor(this.driver); WisePageFactory.initElements(this.driver, page); assertNotNull(page.getDummyComponent()); }
public static <T> T initElements(WebDriver driver, Class<T> clazz) { T instance = instantiatePage(driver, clazz); return initElements(driver, instance); }
WisePageFactory { public static <T> T initElements(WebDriver driver, Class<T> clazz) { T instance = instantiatePage(driver, clazz); return initElements(driver, instance); } }
WisePageFactory { public static <T> T initElements(WebDriver driver, Class<T> clazz) { T instance = instantiatePage(driver, clazz); return initElements(driver, instance); } private WisePageFactory(); }
WisePageFactory { public static <T> T initElements(WebDriver driver, Class<T> clazz) { T instance = instantiatePage(driver, clazz); return initElements(driver, instance); } private WisePageFactory(); static T initElements(WebDriver driver, Class<T> clazz); static T initElements(SearchContext searchContext, T instance); }
WisePageFactory { public static <T> T initElements(WebDriver driver, Class<T> clazz) { T instance = instantiatePage(driver, clazz); return initElements(driver, instance); } private WisePageFactory(); static T initElements(WebDriver driver, Class<T> clazz); static T initElements(SearchContext searchContext, T instance); }
@Test(expectedExceptions = PageInstantiationException.class) public void shouldThrowExceptionWhileInstantiatingPageWithoutProperConstructor() { WisePageFactory.initElements(this.driver, DummyPageWithoutProperConstructor.class); }
public static <T> T initElements(WebDriver driver, Class<T> clazz) { T instance = instantiatePage(driver, clazz); return initElements(driver, instance); }
WisePageFactory { public static <T> T initElements(WebDriver driver, Class<T> clazz) { T instance = instantiatePage(driver, clazz); return initElements(driver, instance); } }
WisePageFactory { public static <T> T initElements(WebDriver driver, Class<T> clazz) { T instance = instantiatePage(driver, clazz); return initElements(driver, instance); } private WisePageFactory(); }
WisePageFactory { public static <T> T initElements(WebDriver driver, Class<T> clazz) { T instance = instantiatePage(driver, clazz); return initElements(driver, instance); } private WisePageFactory(); static T initElements(WebDriver driver, Class<T> clazz); static T initElements(SearchContext searchContext, T instance); }
WisePageFactory { public static <T> T initElements(WebDriver driver, Class<T> clazz) { T instance = instantiatePage(driver, clazz); return initElements(driver, instance); } private WisePageFactory(); static T initElements(WebDriver driver, Class<T> clazz); static T initElements(SearchContext searchContext, T instance); }
@Test public void shouldFindElement() { DummyComponent select = Wiselenium.findElement(DummyComponent.class, BY_SELECT1, this.driver); assertNotNull(select); }
public static <E> E findElement(Class<E> clazz, By by, SearchContext searchContext) { WebElement webElement = searchContext.findElement(by); return decorateElement(clazz, webElement); }
Wiselenium { public static <E> E findElement(Class<E> clazz, By by, SearchContext searchContext) { WebElement webElement = searchContext.findElement(by); return decorateElement(clazz, webElement); } }
Wiselenium { public static <E> E findElement(Class<E> clazz, By by, SearchContext searchContext) { WebElement webElement = searchContext.findElement(by); return decorateElement(clazz, webElement); } private Wiselenium(); }
Wiselenium { public static <E> E findElement(Class<E> clazz, By by, SearchContext searchContext) { WebElement webElement = searchContext.findElement(by); return decorateElement(clazz, webElement); } private Wiselenium(); static E findElement(Class<E> clazz, By by, SearchContext searchContext); static List<E> findElements(Class<E> clazz, By by, SearchContext searchContext); static E decorateElement(Class<E> clazz, WebElement webElement); static List<E> decorateElements(Class<E> clazz, List<WebElement> webElements); }
Wiselenium { public static <E> E findElement(Class<E> clazz, By by, SearchContext searchContext) { WebElement webElement = searchContext.findElement(by); return decorateElement(clazz, webElement); } private Wiselenium(); static E findElement(Class<E> clazz, By by, SearchContext searchContext); static List<E> findElements(Class<E> clazz, By by, SearchContext searchContext); static E decorateElement(Class<E> clazz, WebElement webElement); static List<E> decorateElements(Class<E> clazz, List<WebElement> webElements); }
@SuppressWarnings("null") @Test public void shouldFindElements() { List<DummyComponent> elements = Wiselenium.findElements( DummyComponent.class, BY_RADIOBUTTON, this.driver); assertTrue(elements != null && !elements.isEmpty()); for (DummyComponent element : elements) { assertNotNull(element); } }
public static <E> List<E> findElements(Class<E> clazz, By by, SearchContext searchContext) { List<WebElement> webElements = searchContext.findElements(by); if (webElements.isEmpty()) return Lists.newArrayList(); WiseDecorator decorator = new WiseDecorator(new DefaultElementLocatorFactory(searchContext)); return decorator.decorate(clazz, webElements); }
Wiselenium { public static <E> List<E> findElements(Class<E> clazz, By by, SearchContext searchContext) { List<WebElement> webElements = searchContext.findElements(by); if (webElements.isEmpty()) return Lists.newArrayList(); WiseDecorator decorator = new WiseDecorator(new DefaultElementLocatorFactory(searchContext)); return decorator.decorate(clazz, webElements); } }
Wiselenium { public static <E> List<E> findElements(Class<E> clazz, By by, SearchContext searchContext) { List<WebElement> webElements = searchContext.findElements(by); if (webElements.isEmpty()) return Lists.newArrayList(); WiseDecorator decorator = new WiseDecorator(new DefaultElementLocatorFactory(searchContext)); return decorator.decorate(clazz, webElements); } private Wiselenium(); }
Wiselenium { public static <E> List<E> findElements(Class<E> clazz, By by, SearchContext searchContext) { List<WebElement> webElements = searchContext.findElements(by); if (webElements.isEmpty()) return Lists.newArrayList(); WiseDecorator decorator = new WiseDecorator(new DefaultElementLocatorFactory(searchContext)); return decorator.decorate(clazz, webElements); } private Wiselenium(); static E findElement(Class<E> clazz, By by, SearchContext searchContext); static List<E> findElements(Class<E> clazz, By by, SearchContext searchContext); static E decorateElement(Class<E> clazz, WebElement webElement); static List<E> decorateElements(Class<E> clazz, List<WebElement> webElements); }
Wiselenium { public static <E> List<E> findElements(Class<E> clazz, By by, SearchContext searchContext) { List<WebElement> webElements = searchContext.findElements(by); if (webElements.isEmpty()) return Lists.newArrayList(); WiseDecorator decorator = new WiseDecorator(new DefaultElementLocatorFactory(searchContext)); return decorator.decorate(clazz, webElements); } private Wiselenium(); static E findElement(Class<E> clazz, By by, SearchContext searchContext); static List<E> findElements(Class<E> clazz, By by, SearchContext searchContext); static E decorateElement(Class<E> clazz, WebElement webElement); static List<E> decorateElements(Class<E> clazz, List<WebElement> webElements); }
@Test public void shouldReturnEmptyListWhenElementsAreNotFound() { List<DummyComponent> selects = Wiselenium.findElements( DummyComponent.class, By.id("inexistent"), this.driver); assertTrue(selects != null && selects.isEmpty()); }
public static <E> List<E> findElements(Class<E> clazz, By by, SearchContext searchContext) { List<WebElement> webElements = searchContext.findElements(by); if (webElements.isEmpty()) return Lists.newArrayList(); WiseDecorator decorator = new WiseDecorator(new DefaultElementLocatorFactory(searchContext)); return decorator.decorate(clazz, webElements); }
Wiselenium { public static <E> List<E> findElements(Class<E> clazz, By by, SearchContext searchContext) { List<WebElement> webElements = searchContext.findElements(by); if (webElements.isEmpty()) return Lists.newArrayList(); WiseDecorator decorator = new WiseDecorator(new DefaultElementLocatorFactory(searchContext)); return decorator.decorate(clazz, webElements); } }
Wiselenium { public static <E> List<E> findElements(Class<E> clazz, By by, SearchContext searchContext) { List<WebElement> webElements = searchContext.findElements(by); if (webElements.isEmpty()) return Lists.newArrayList(); WiseDecorator decorator = new WiseDecorator(new DefaultElementLocatorFactory(searchContext)); return decorator.decorate(clazz, webElements); } private Wiselenium(); }
Wiselenium { public static <E> List<E> findElements(Class<E> clazz, By by, SearchContext searchContext) { List<WebElement> webElements = searchContext.findElements(by); if (webElements.isEmpty()) return Lists.newArrayList(); WiseDecorator decorator = new WiseDecorator(new DefaultElementLocatorFactory(searchContext)); return decorator.decorate(clazz, webElements); } private Wiselenium(); static E findElement(Class<E> clazz, By by, SearchContext searchContext); static List<E> findElements(Class<E> clazz, By by, SearchContext searchContext); static E decorateElement(Class<E> clazz, WebElement webElement); static List<E> decorateElements(Class<E> clazz, List<WebElement> webElements); }
Wiselenium { public static <E> List<E> findElements(Class<E> clazz, By by, SearchContext searchContext) { List<WebElement> webElements = searchContext.findElements(by); if (webElements.isEmpty()) return Lists.newArrayList(); WiseDecorator decorator = new WiseDecorator(new DefaultElementLocatorFactory(searchContext)); return decorator.decorate(clazz, webElements); } private Wiselenium(); static E findElement(Class<E> clazz, By by, SearchContext searchContext); static List<E> findElements(Class<E> clazz, By by, SearchContext searchContext); static E decorateElement(Class<E> clazz, WebElement webElement); static List<E> decorateElements(Class<E> clazz, List<WebElement> webElements); }
@Test(expectedExceptions = NoSuchElementException.class) public void shouldThrowExceptionWhenElementIsntFound() { Wiselenium.findElement(DummyComponent.class, By.id("inexistent"), this.driver); }
public static <E> E findElement(Class<E> clazz, By by, SearchContext searchContext) { WebElement webElement = searchContext.findElement(by); return decorateElement(clazz, webElement); }
Wiselenium { public static <E> E findElement(Class<E> clazz, By by, SearchContext searchContext) { WebElement webElement = searchContext.findElement(by); return decorateElement(clazz, webElement); } }
Wiselenium { public static <E> E findElement(Class<E> clazz, By by, SearchContext searchContext) { WebElement webElement = searchContext.findElement(by); return decorateElement(clazz, webElement); } private Wiselenium(); }
Wiselenium { public static <E> E findElement(Class<E> clazz, By by, SearchContext searchContext) { WebElement webElement = searchContext.findElement(by); return decorateElement(clazz, webElement); } private Wiselenium(); static E findElement(Class<E> clazz, By by, SearchContext searchContext); static List<E> findElements(Class<E> clazz, By by, SearchContext searchContext); static E decorateElement(Class<E> clazz, WebElement webElement); static List<E> decorateElements(Class<E> clazz, List<WebElement> webElements); }
Wiselenium { public static <E> E findElement(Class<E> clazz, By by, SearchContext searchContext) { WebElement webElement = searchContext.findElement(by); return decorateElement(clazz, webElement); } private Wiselenium(); static E findElement(Class<E> clazz, By by, SearchContext searchContext); static List<E> findElements(Class<E> clazz, By by, SearchContext searchContext); static E decorateElement(Class<E> clazz, WebElement webElement); static List<E> decorateElements(Class<E> clazz, List<WebElement> webElements); }
@Test public void peek() throws Exception { Reader in = new StringReader("I see you"); LexerStream ls = new LexerStream(in); assertEquals(1, ls.getLine()); assertEquals(1, ls.getCol()); int c = ls.peek(); assertEquals('I', (char)c); assertTrue(1 == ls.getLine() && 1 == ls.getCol()); c = ls.peek(2); assertEquals(' ', (char)c); assertTrue(1 == ls.getLine() && 1 == ls.getCol()); c = ls.peek(4); assertEquals('e', (char)c); assertTrue(1 == ls.getLine() && 1 == ls.getCol()); c = ls.peek(8); assertEquals('o', (char)c); assertTrue(1 == ls.getLine() && 1 == ls.getCol()); c = ls.read(); assertEquals('I', (char)c); assertTrue(1 == ls.getLine() && 2 == ls.getCol()); }
public int peek() throws IOException { super.mark(1); int c = super.read(); super.reset(); return c; }
LexerStream extends LineNumberReader { public int peek() throws IOException { super.mark(1); int c = super.read(); super.reset(); return c; } }
LexerStream extends LineNumberReader { public int peek() throws IOException { super.mark(1); int c = super.read(); super.reset(); return c; } LexerStream(Reader in_stream); }
LexerStream extends LineNumberReader { public int peek() throws IOException { super.mark(1); int c = super.read(); super.reset(); return c; } LexerStream(Reader in_stream); int getLine(); int getCol(); int getPosition(); @Override int read(); @Override String readLine(); int peek(); int peek(int ahead); @Override int read(char[] cbuf, int off, int len); @Override long skip(long n); @Override void mark(int readAheadLimit); @Override void reset(); }
LexerStream extends LineNumberReader { public int peek() throws IOException { super.mark(1); int c = super.read(); super.reset(); return c; } LexerStream(Reader in_stream); int getLine(); int getCol(); int getPosition(); @Override int read(); @Override String readLine(); int peek(); int peek(int ahead); @Override int read(char[] cbuf, int off, int len); @Override long skip(long n); @Override void mark(int readAheadLimit); @Override void reset(); }
@Test public void chars() throws Exception { String str = "'a' '\\n' '' '\\t'"; LexerStream ls = new LexerStream(new StringReader(str)); LexCharLiteral lex = new LexCharLiteral(new BaseTokenFactory(), ls); Token tok = lex.read(); assertEquals(TokenType.LiteralChar, tok.getType()); assertEquals("a", tok.getValue()); assertEquals(1, tok.getLine()); assertEquals(1, tok.getCol()); assertEquals(1, tok.getEndLine()); assertEquals(4, tok.getEndCol()); ls.read(); tok = lex.read(); assertEquals(TokenType.LiteralChar, tok.getType()); assertEquals("\n", tok.getValue()); assertEquals(1, tok.getLine()); assertEquals(5, tok.getCol()); assertEquals(1, tok.getEndLine()); assertEquals(9, tok.getEndCol()); ls.read(); tok = lex.read(); assertEquals(TokenType.LiteralChar, tok.getType()); assertEquals("''", tok.getValue()); assertEquals(1, tok.getLine()); assertEquals(10, tok.getCol()); assertEquals(1, tok.getEndLine()); assertEquals(12, tok.getEndCol()); ls.read(); tok = lex.read(); assertEquals(TokenType.LiteralChar, tok.getType()); assertEquals("\t", tok.getValue()); assertEquals(1, tok.getLine()); assertEquals(13, tok.getCol()); assertEquals(1, tok.getEndLine()); assertEquals(17, tok.getEndCol()); }
public Token read() throws IOException, LexerException { int start_index = in_stream.getPosition(); int start_line = in_stream.getLine(); int start_col = in_stream.getCol(); int code_point; int next = in_stream.peek(); if (next != '\'') { throw new LexerException(start_line, start_col, "Not a character literal"); } in_stream.read(); next = in_stream.peek(); if (next == '\'') { in_stream.read(); return factory.create(TokenType.LiteralChar, start_index, start_line, start_col, 2, "''"); } if (next == '\\') { code_point = LexEscape.read(in_stream); } else { code_point = in_stream.read(); } if (in_stream.read() != '\'') { throw new LexerException(start_line, start_col, "Unterminated character literal"); } String ch_str = String.valueOf(Character.toChars(code_point)); return factory.create(TokenType.LiteralChar, start_index, start_line, start_col, in_stream.getPosition(), in_stream.getLine(), in_stream.getCol(), ch_str); }
LexCharLiteral { public Token read() throws IOException, LexerException { int start_index = in_stream.getPosition(); int start_line = in_stream.getLine(); int start_col = in_stream.getCol(); int code_point; int next = in_stream.peek(); if (next != '\'') { throw new LexerException(start_line, start_col, "Not a character literal"); } in_stream.read(); next = in_stream.peek(); if (next == '\'') { in_stream.read(); return factory.create(TokenType.LiteralChar, start_index, start_line, start_col, 2, "''"); } if (next == '\\') { code_point = LexEscape.read(in_stream); } else { code_point = in_stream.read(); } if (in_stream.read() != '\'') { throw new LexerException(start_line, start_col, "Unterminated character literal"); } String ch_str = String.valueOf(Character.toChars(code_point)); return factory.create(TokenType.LiteralChar, start_index, start_line, start_col, in_stream.getPosition(), in_stream.getLine(), in_stream.getCol(), ch_str); } }
LexCharLiteral { public Token read() throws IOException, LexerException { int start_index = in_stream.getPosition(); int start_line = in_stream.getLine(); int start_col = in_stream.getCol(); int code_point; int next = in_stream.peek(); if (next != '\'') { throw new LexerException(start_line, start_col, "Not a character literal"); } in_stream.read(); next = in_stream.peek(); if (next == '\'') { in_stream.read(); return factory.create(TokenType.LiteralChar, start_index, start_line, start_col, 2, "''"); } if (next == '\\') { code_point = LexEscape.read(in_stream); } else { code_point = in_stream.read(); } if (in_stream.read() != '\'') { throw new LexerException(start_line, start_col, "Unterminated character literal"); } String ch_str = String.valueOf(Character.toChars(code_point)); return factory.create(TokenType.LiteralChar, start_index, start_line, start_col, in_stream.getPosition(), in_stream.getLine(), in_stream.getCol(), ch_str); } LexCharLiteral(TokenFactory factory, LexerStream in_stream); }
LexCharLiteral { public Token read() throws IOException, LexerException { int start_index = in_stream.getPosition(); int start_line = in_stream.getLine(); int start_col = in_stream.getCol(); int code_point; int next = in_stream.peek(); if (next != '\'') { throw new LexerException(start_line, start_col, "Not a character literal"); } in_stream.read(); next = in_stream.peek(); if (next == '\'') { in_stream.read(); return factory.create(TokenType.LiteralChar, start_index, start_line, start_col, 2, "''"); } if (next == '\\') { code_point = LexEscape.read(in_stream); } else { code_point = in_stream.read(); } if (in_stream.read() != '\'') { throw new LexerException(start_line, start_col, "Unterminated character literal"); } String ch_str = String.valueOf(Character.toChars(code_point)); return factory.create(TokenType.LiteralChar, start_index, start_line, start_col, in_stream.getPosition(), in_stream.getLine(), in_stream.getCol(), ch_str); } LexCharLiteral(TokenFactory factory, LexerStream in_stream); Token read(); }
LexCharLiteral { public Token read() throws IOException, LexerException { int start_index = in_stream.getPosition(); int start_line = in_stream.getLine(); int start_col = in_stream.getCol(); int code_point; int next = in_stream.peek(); if (next != '\'') { throw new LexerException(start_line, start_col, "Not a character literal"); } in_stream.read(); next = in_stream.peek(); if (next == '\'') { in_stream.read(); return factory.create(TokenType.LiteralChar, start_index, start_line, start_col, 2, "''"); } if (next == '\\') { code_point = LexEscape.read(in_stream); } else { code_point = in_stream.read(); } if (in_stream.read() != '\'') { throw new LexerException(start_line, start_col, "Unterminated character literal"); } String ch_str = String.valueOf(Character.toChars(code_point)); return factory.create(TokenType.LiteralChar, start_index, start_line, start_col, in_stream.getPosition(), in_stream.getLine(), in_stream.getCol(), ch_str); } LexCharLiteral(TokenFactory factory, LexerStream in_stream); Token read(); }
@Test public void normal() throws Exception { String str = "q\"!foo!\""; LexerStream ls = new LexerStream(new StringReader(str)); LexDelimitedString lex = new LexDelimitedString(new BaseTokenFactory(), ls); Token tok = lex.read(); Assert.assertEquals(TokenType.LiteralUtf8, tok.getType()); assertTrue(tok.getValue() instanceof String); Assert.assertEquals("foo", tok.getValue()); Assert.assertEquals(1, tok.getLine()); Assert.assertEquals(1, tok.getCol()); Assert.assertEquals(1, tok.getEndLine()); Assert.assertEquals(9, tok.getEndCol()); }
public Token read() throws IOException, LexerException { int start_index = in_stream.getPosition(); int start_line = in_stream.getLine(); int start_col = in_stream.getCol(); int next = in_stream.peek(); if (next == -1) { throw new LexerException(start_line, start_col, "Unexpected end of input stream when parsing delimited string literal"); } if (next != 'q') { throw new LexerException(start_line, start_col, "Not a delimited string literal"); } in_stream.read(); next = in_stream.peek(); if (next != '"') { throw new LexerException(start_line, start_col, "Not a delimited string literal"); } in_stream.read(); next = in_stream.peek(); boolean is_identifier = false; boolean is_nesting = false; int nests = 0; StringBuilder delimiter = new StringBuilder(); if (is_nesting_open(next)) { is_nesting = true; delimiter.append(Character.toChars(in_stream.read())); } else if (Character.isLetter(next) || next == '_') { is_identifier = true; delimiter.append(Character.toChars(in_stream.read())); next = in_stream.peek(); while (Character.isLetterOrDigit(next) || next == '_') { delimiter.append(Character.toChars(in_stream.read())); next = in_stream.peek(); } if (next != '\n') { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "String delimiter must be followed by newline"); } in_stream.read(); } else { delimiter.append(Character.toChars(in_stream.read())); } next = in_stream.peek(); String start_delim = delimiter.toString(); String end_delim; if (is_identifier) { end_delim = start_delim; } else { end_delim = String.valueOf((char)valid_close(start_delim.charAt(0))); } StringBuilder result = new StringBuilder(); while (next != -1) { if (next == '\n' && is_identifier) { result.append((char)in_stream.read()); next = in_stream.peek(); StringBuilder sb = new StringBuilder(); for (char c : end_delim.toCharArray()) { if (next == -1) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Unexpected end of stream in delimited string literal"); } else if (next != c) { break; } sb.append(Character.toChars(in_stream.read())); next = in_stream.peek(); } if (next == '"' && sb.toString().equals(end_delim)) { in_stream.read(); break; } result.append(sb); } else if (is_nesting && next == start_delim.charAt(0)) { nests++; } else if (next == end_delim.charAt(0)) { nests--; if (nests < 0) { if (in_stream.peek(2) == '"') { in_stream.read(); in_stream.read(); break; } else { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Improperly delimited string"); } } } result.append(Character.toChars(in_stream.read())); next = in_stream.peek(); } if (next == -1) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Unexpected end of stream in delimited string literal"); } return factory.create(TokenType.LiteralUtf8, start_index, start_line, start_col, in_stream.getPosition(), in_stream.getLine(), in_stream.getCol(), result.toString()); }
LexDelimitedString { public Token read() throws IOException, LexerException { int start_index = in_stream.getPosition(); int start_line = in_stream.getLine(); int start_col = in_stream.getCol(); int next = in_stream.peek(); if (next == -1) { throw new LexerException(start_line, start_col, "Unexpected end of input stream when parsing delimited string literal"); } if (next != 'q') { throw new LexerException(start_line, start_col, "Not a delimited string literal"); } in_stream.read(); next = in_stream.peek(); if (next != '"') { throw new LexerException(start_line, start_col, "Not a delimited string literal"); } in_stream.read(); next = in_stream.peek(); boolean is_identifier = false; boolean is_nesting = false; int nests = 0; StringBuilder delimiter = new StringBuilder(); if (is_nesting_open(next)) { is_nesting = true; delimiter.append(Character.toChars(in_stream.read())); } else if (Character.isLetter(next) || next == '_') { is_identifier = true; delimiter.append(Character.toChars(in_stream.read())); next = in_stream.peek(); while (Character.isLetterOrDigit(next) || next == '_') { delimiter.append(Character.toChars(in_stream.read())); next = in_stream.peek(); } if (next != '\n') { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "String delimiter must be followed by newline"); } in_stream.read(); } else { delimiter.append(Character.toChars(in_stream.read())); } next = in_stream.peek(); String start_delim = delimiter.toString(); String end_delim; if (is_identifier) { end_delim = start_delim; } else { end_delim = String.valueOf((char)valid_close(start_delim.charAt(0))); } StringBuilder result = new StringBuilder(); while (next != -1) { if (next == '\n' && is_identifier) { result.append((char)in_stream.read()); next = in_stream.peek(); StringBuilder sb = new StringBuilder(); for (char c : end_delim.toCharArray()) { if (next == -1) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Unexpected end of stream in delimited string literal"); } else if (next != c) { break; } sb.append(Character.toChars(in_stream.read())); next = in_stream.peek(); } if (next == '"' && sb.toString().equals(end_delim)) { in_stream.read(); break; } result.append(sb); } else if (is_nesting && next == start_delim.charAt(0)) { nests++; } else if (next == end_delim.charAt(0)) { nests--; if (nests < 0) { if (in_stream.peek(2) == '"') { in_stream.read(); in_stream.read(); break; } else { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Improperly delimited string"); } } } result.append(Character.toChars(in_stream.read())); next = in_stream.peek(); } if (next == -1) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Unexpected end of stream in delimited string literal"); } return factory.create(TokenType.LiteralUtf8, start_index, start_line, start_col, in_stream.getPosition(), in_stream.getLine(), in_stream.getCol(), result.toString()); } }
LexDelimitedString { public Token read() throws IOException, LexerException { int start_index = in_stream.getPosition(); int start_line = in_stream.getLine(); int start_col = in_stream.getCol(); int next = in_stream.peek(); if (next == -1) { throw new LexerException(start_line, start_col, "Unexpected end of input stream when parsing delimited string literal"); } if (next != 'q') { throw new LexerException(start_line, start_col, "Not a delimited string literal"); } in_stream.read(); next = in_stream.peek(); if (next != '"') { throw new LexerException(start_line, start_col, "Not a delimited string literal"); } in_stream.read(); next = in_stream.peek(); boolean is_identifier = false; boolean is_nesting = false; int nests = 0; StringBuilder delimiter = new StringBuilder(); if (is_nesting_open(next)) { is_nesting = true; delimiter.append(Character.toChars(in_stream.read())); } else if (Character.isLetter(next) || next == '_') { is_identifier = true; delimiter.append(Character.toChars(in_stream.read())); next = in_stream.peek(); while (Character.isLetterOrDigit(next) || next == '_') { delimiter.append(Character.toChars(in_stream.read())); next = in_stream.peek(); } if (next != '\n') { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "String delimiter must be followed by newline"); } in_stream.read(); } else { delimiter.append(Character.toChars(in_stream.read())); } next = in_stream.peek(); String start_delim = delimiter.toString(); String end_delim; if (is_identifier) { end_delim = start_delim; } else { end_delim = String.valueOf((char)valid_close(start_delim.charAt(0))); } StringBuilder result = new StringBuilder(); while (next != -1) { if (next == '\n' && is_identifier) { result.append((char)in_stream.read()); next = in_stream.peek(); StringBuilder sb = new StringBuilder(); for (char c : end_delim.toCharArray()) { if (next == -1) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Unexpected end of stream in delimited string literal"); } else if (next != c) { break; } sb.append(Character.toChars(in_stream.read())); next = in_stream.peek(); } if (next == '"' && sb.toString().equals(end_delim)) { in_stream.read(); break; } result.append(sb); } else if (is_nesting && next == start_delim.charAt(0)) { nests++; } else if (next == end_delim.charAt(0)) { nests--; if (nests < 0) { if (in_stream.peek(2) == '"') { in_stream.read(); in_stream.read(); break; } else { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Improperly delimited string"); } } } result.append(Character.toChars(in_stream.read())); next = in_stream.peek(); } if (next == -1) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Unexpected end of stream in delimited string literal"); } return factory.create(TokenType.LiteralUtf8, start_index, start_line, start_col, in_stream.getPosition(), in_stream.getLine(), in_stream.getCol(), result.toString()); } LexDelimitedString(TokenFactory factory, LexerStream in_stream); }
LexDelimitedString { public Token read() throws IOException, LexerException { int start_index = in_stream.getPosition(); int start_line = in_stream.getLine(); int start_col = in_stream.getCol(); int next = in_stream.peek(); if (next == -1) { throw new LexerException(start_line, start_col, "Unexpected end of input stream when parsing delimited string literal"); } if (next != 'q') { throw new LexerException(start_line, start_col, "Not a delimited string literal"); } in_stream.read(); next = in_stream.peek(); if (next != '"') { throw new LexerException(start_line, start_col, "Not a delimited string literal"); } in_stream.read(); next = in_stream.peek(); boolean is_identifier = false; boolean is_nesting = false; int nests = 0; StringBuilder delimiter = new StringBuilder(); if (is_nesting_open(next)) { is_nesting = true; delimiter.append(Character.toChars(in_stream.read())); } else if (Character.isLetter(next) || next == '_') { is_identifier = true; delimiter.append(Character.toChars(in_stream.read())); next = in_stream.peek(); while (Character.isLetterOrDigit(next) || next == '_') { delimiter.append(Character.toChars(in_stream.read())); next = in_stream.peek(); } if (next != '\n') { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "String delimiter must be followed by newline"); } in_stream.read(); } else { delimiter.append(Character.toChars(in_stream.read())); } next = in_stream.peek(); String start_delim = delimiter.toString(); String end_delim; if (is_identifier) { end_delim = start_delim; } else { end_delim = String.valueOf((char)valid_close(start_delim.charAt(0))); } StringBuilder result = new StringBuilder(); while (next != -1) { if (next == '\n' && is_identifier) { result.append((char)in_stream.read()); next = in_stream.peek(); StringBuilder sb = new StringBuilder(); for (char c : end_delim.toCharArray()) { if (next == -1) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Unexpected end of stream in delimited string literal"); } else if (next != c) { break; } sb.append(Character.toChars(in_stream.read())); next = in_stream.peek(); } if (next == '"' && sb.toString().equals(end_delim)) { in_stream.read(); break; } result.append(sb); } else if (is_nesting && next == start_delim.charAt(0)) { nests++; } else if (next == end_delim.charAt(0)) { nests--; if (nests < 0) { if (in_stream.peek(2) == '"') { in_stream.read(); in_stream.read(); break; } else { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Improperly delimited string"); } } } result.append(Character.toChars(in_stream.read())); next = in_stream.peek(); } if (next == -1) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Unexpected end of stream in delimited string literal"); } return factory.create(TokenType.LiteralUtf8, start_index, start_line, start_col, in_stream.getPosition(), in_stream.getLine(), in_stream.getCol(), result.toString()); } LexDelimitedString(TokenFactory factory, LexerStream in_stream); Token read(); }
LexDelimitedString { public Token read() throws IOException, LexerException { int start_index = in_stream.getPosition(); int start_line = in_stream.getLine(); int start_col = in_stream.getCol(); int next = in_stream.peek(); if (next == -1) { throw new LexerException(start_line, start_col, "Unexpected end of input stream when parsing delimited string literal"); } if (next != 'q') { throw new LexerException(start_line, start_col, "Not a delimited string literal"); } in_stream.read(); next = in_stream.peek(); if (next != '"') { throw new LexerException(start_line, start_col, "Not a delimited string literal"); } in_stream.read(); next = in_stream.peek(); boolean is_identifier = false; boolean is_nesting = false; int nests = 0; StringBuilder delimiter = new StringBuilder(); if (is_nesting_open(next)) { is_nesting = true; delimiter.append(Character.toChars(in_stream.read())); } else if (Character.isLetter(next) || next == '_') { is_identifier = true; delimiter.append(Character.toChars(in_stream.read())); next = in_stream.peek(); while (Character.isLetterOrDigit(next) || next == '_') { delimiter.append(Character.toChars(in_stream.read())); next = in_stream.peek(); } if (next != '\n') { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "String delimiter must be followed by newline"); } in_stream.read(); } else { delimiter.append(Character.toChars(in_stream.read())); } next = in_stream.peek(); String start_delim = delimiter.toString(); String end_delim; if (is_identifier) { end_delim = start_delim; } else { end_delim = String.valueOf((char)valid_close(start_delim.charAt(0))); } StringBuilder result = new StringBuilder(); while (next != -1) { if (next == '\n' && is_identifier) { result.append((char)in_stream.read()); next = in_stream.peek(); StringBuilder sb = new StringBuilder(); for (char c : end_delim.toCharArray()) { if (next == -1) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Unexpected end of stream in delimited string literal"); } else if (next != c) { break; } sb.append(Character.toChars(in_stream.read())); next = in_stream.peek(); } if (next == '"' && sb.toString().equals(end_delim)) { in_stream.read(); break; } result.append(sb); } else if (is_nesting && next == start_delim.charAt(0)) { nests++; } else if (next == end_delim.charAt(0)) { nests--; if (nests < 0) { if (in_stream.peek(2) == '"') { in_stream.read(); in_stream.read(); break; } else { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Improperly delimited string"); } } } result.append(Character.toChars(in_stream.read())); next = in_stream.peek(); } if (next == -1) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Unexpected end of stream in delimited string literal"); } return factory.create(TokenType.LiteralUtf8, start_index, start_line, start_col, in_stream.getPosition(), in_stream.getLine(), in_stream.getCol(), result.toString()); } LexDelimitedString(TokenFactory factory, LexerStream in_stream); Token read(); }
@Test public void bracketed() throws Exception { String str = "q\"{foo][{b}f}\"bar"; LexerStream ls = new LexerStream(new StringReader(str)); LexDelimitedString lex = new LexDelimitedString(new BaseTokenFactory(), ls); Token tok = lex.read(); Assert.assertEquals(TokenType.LiteralUtf8, tok.getType()); assertTrue(tok.getValue() instanceof String); Assert.assertEquals("foo][{b}f", tok.getValue()); Assert.assertEquals(1, tok.getLine()); Assert.assertEquals(1, tok.getCol()); Assert.assertEquals(1, tok.getEndLine()); Assert.assertEquals(15, tok.getEndCol()); }
public Token read() throws IOException, LexerException { int start_index = in_stream.getPosition(); int start_line = in_stream.getLine(); int start_col = in_stream.getCol(); int next = in_stream.peek(); if (next == -1) { throw new LexerException(start_line, start_col, "Unexpected end of input stream when parsing delimited string literal"); } if (next != 'q') { throw new LexerException(start_line, start_col, "Not a delimited string literal"); } in_stream.read(); next = in_stream.peek(); if (next != '"') { throw new LexerException(start_line, start_col, "Not a delimited string literal"); } in_stream.read(); next = in_stream.peek(); boolean is_identifier = false; boolean is_nesting = false; int nests = 0; StringBuilder delimiter = new StringBuilder(); if (is_nesting_open(next)) { is_nesting = true; delimiter.append(Character.toChars(in_stream.read())); } else if (Character.isLetter(next) || next == '_') { is_identifier = true; delimiter.append(Character.toChars(in_stream.read())); next = in_stream.peek(); while (Character.isLetterOrDigit(next) || next == '_') { delimiter.append(Character.toChars(in_stream.read())); next = in_stream.peek(); } if (next != '\n') { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "String delimiter must be followed by newline"); } in_stream.read(); } else { delimiter.append(Character.toChars(in_stream.read())); } next = in_stream.peek(); String start_delim = delimiter.toString(); String end_delim; if (is_identifier) { end_delim = start_delim; } else { end_delim = String.valueOf((char)valid_close(start_delim.charAt(0))); } StringBuilder result = new StringBuilder(); while (next != -1) { if (next == '\n' && is_identifier) { result.append((char)in_stream.read()); next = in_stream.peek(); StringBuilder sb = new StringBuilder(); for (char c : end_delim.toCharArray()) { if (next == -1) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Unexpected end of stream in delimited string literal"); } else if (next != c) { break; } sb.append(Character.toChars(in_stream.read())); next = in_stream.peek(); } if (next == '"' && sb.toString().equals(end_delim)) { in_stream.read(); break; } result.append(sb); } else if (is_nesting && next == start_delim.charAt(0)) { nests++; } else if (next == end_delim.charAt(0)) { nests--; if (nests < 0) { if (in_stream.peek(2) == '"') { in_stream.read(); in_stream.read(); break; } else { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Improperly delimited string"); } } } result.append(Character.toChars(in_stream.read())); next = in_stream.peek(); } if (next == -1) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Unexpected end of stream in delimited string literal"); } return factory.create(TokenType.LiteralUtf8, start_index, start_line, start_col, in_stream.getPosition(), in_stream.getLine(), in_stream.getCol(), result.toString()); }
LexDelimitedString { public Token read() throws IOException, LexerException { int start_index = in_stream.getPosition(); int start_line = in_stream.getLine(); int start_col = in_stream.getCol(); int next = in_stream.peek(); if (next == -1) { throw new LexerException(start_line, start_col, "Unexpected end of input stream when parsing delimited string literal"); } if (next != 'q') { throw new LexerException(start_line, start_col, "Not a delimited string literal"); } in_stream.read(); next = in_stream.peek(); if (next != '"') { throw new LexerException(start_line, start_col, "Not a delimited string literal"); } in_stream.read(); next = in_stream.peek(); boolean is_identifier = false; boolean is_nesting = false; int nests = 0; StringBuilder delimiter = new StringBuilder(); if (is_nesting_open(next)) { is_nesting = true; delimiter.append(Character.toChars(in_stream.read())); } else if (Character.isLetter(next) || next == '_') { is_identifier = true; delimiter.append(Character.toChars(in_stream.read())); next = in_stream.peek(); while (Character.isLetterOrDigit(next) || next == '_') { delimiter.append(Character.toChars(in_stream.read())); next = in_stream.peek(); } if (next != '\n') { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "String delimiter must be followed by newline"); } in_stream.read(); } else { delimiter.append(Character.toChars(in_stream.read())); } next = in_stream.peek(); String start_delim = delimiter.toString(); String end_delim; if (is_identifier) { end_delim = start_delim; } else { end_delim = String.valueOf((char)valid_close(start_delim.charAt(0))); } StringBuilder result = new StringBuilder(); while (next != -1) { if (next == '\n' && is_identifier) { result.append((char)in_stream.read()); next = in_stream.peek(); StringBuilder sb = new StringBuilder(); for (char c : end_delim.toCharArray()) { if (next == -1) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Unexpected end of stream in delimited string literal"); } else if (next != c) { break; } sb.append(Character.toChars(in_stream.read())); next = in_stream.peek(); } if (next == '"' && sb.toString().equals(end_delim)) { in_stream.read(); break; } result.append(sb); } else if (is_nesting && next == start_delim.charAt(0)) { nests++; } else if (next == end_delim.charAt(0)) { nests--; if (nests < 0) { if (in_stream.peek(2) == '"') { in_stream.read(); in_stream.read(); break; } else { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Improperly delimited string"); } } } result.append(Character.toChars(in_stream.read())); next = in_stream.peek(); } if (next == -1) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Unexpected end of stream in delimited string literal"); } return factory.create(TokenType.LiteralUtf8, start_index, start_line, start_col, in_stream.getPosition(), in_stream.getLine(), in_stream.getCol(), result.toString()); } }
LexDelimitedString { public Token read() throws IOException, LexerException { int start_index = in_stream.getPosition(); int start_line = in_stream.getLine(); int start_col = in_stream.getCol(); int next = in_stream.peek(); if (next == -1) { throw new LexerException(start_line, start_col, "Unexpected end of input stream when parsing delimited string literal"); } if (next != 'q') { throw new LexerException(start_line, start_col, "Not a delimited string literal"); } in_stream.read(); next = in_stream.peek(); if (next != '"') { throw new LexerException(start_line, start_col, "Not a delimited string literal"); } in_stream.read(); next = in_stream.peek(); boolean is_identifier = false; boolean is_nesting = false; int nests = 0; StringBuilder delimiter = new StringBuilder(); if (is_nesting_open(next)) { is_nesting = true; delimiter.append(Character.toChars(in_stream.read())); } else if (Character.isLetter(next) || next == '_') { is_identifier = true; delimiter.append(Character.toChars(in_stream.read())); next = in_stream.peek(); while (Character.isLetterOrDigit(next) || next == '_') { delimiter.append(Character.toChars(in_stream.read())); next = in_stream.peek(); } if (next != '\n') { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "String delimiter must be followed by newline"); } in_stream.read(); } else { delimiter.append(Character.toChars(in_stream.read())); } next = in_stream.peek(); String start_delim = delimiter.toString(); String end_delim; if (is_identifier) { end_delim = start_delim; } else { end_delim = String.valueOf((char)valid_close(start_delim.charAt(0))); } StringBuilder result = new StringBuilder(); while (next != -1) { if (next == '\n' && is_identifier) { result.append((char)in_stream.read()); next = in_stream.peek(); StringBuilder sb = new StringBuilder(); for (char c : end_delim.toCharArray()) { if (next == -1) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Unexpected end of stream in delimited string literal"); } else if (next != c) { break; } sb.append(Character.toChars(in_stream.read())); next = in_stream.peek(); } if (next == '"' && sb.toString().equals(end_delim)) { in_stream.read(); break; } result.append(sb); } else if (is_nesting && next == start_delim.charAt(0)) { nests++; } else if (next == end_delim.charAt(0)) { nests--; if (nests < 0) { if (in_stream.peek(2) == '"') { in_stream.read(); in_stream.read(); break; } else { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Improperly delimited string"); } } } result.append(Character.toChars(in_stream.read())); next = in_stream.peek(); } if (next == -1) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Unexpected end of stream in delimited string literal"); } return factory.create(TokenType.LiteralUtf8, start_index, start_line, start_col, in_stream.getPosition(), in_stream.getLine(), in_stream.getCol(), result.toString()); } LexDelimitedString(TokenFactory factory, LexerStream in_stream); }
LexDelimitedString { public Token read() throws IOException, LexerException { int start_index = in_stream.getPosition(); int start_line = in_stream.getLine(); int start_col = in_stream.getCol(); int next = in_stream.peek(); if (next == -1) { throw new LexerException(start_line, start_col, "Unexpected end of input stream when parsing delimited string literal"); } if (next != 'q') { throw new LexerException(start_line, start_col, "Not a delimited string literal"); } in_stream.read(); next = in_stream.peek(); if (next != '"') { throw new LexerException(start_line, start_col, "Not a delimited string literal"); } in_stream.read(); next = in_stream.peek(); boolean is_identifier = false; boolean is_nesting = false; int nests = 0; StringBuilder delimiter = new StringBuilder(); if (is_nesting_open(next)) { is_nesting = true; delimiter.append(Character.toChars(in_stream.read())); } else if (Character.isLetter(next) || next == '_') { is_identifier = true; delimiter.append(Character.toChars(in_stream.read())); next = in_stream.peek(); while (Character.isLetterOrDigit(next) || next == '_') { delimiter.append(Character.toChars(in_stream.read())); next = in_stream.peek(); } if (next != '\n') { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "String delimiter must be followed by newline"); } in_stream.read(); } else { delimiter.append(Character.toChars(in_stream.read())); } next = in_stream.peek(); String start_delim = delimiter.toString(); String end_delim; if (is_identifier) { end_delim = start_delim; } else { end_delim = String.valueOf((char)valid_close(start_delim.charAt(0))); } StringBuilder result = new StringBuilder(); while (next != -1) { if (next == '\n' && is_identifier) { result.append((char)in_stream.read()); next = in_stream.peek(); StringBuilder sb = new StringBuilder(); for (char c : end_delim.toCharArray()) { if (next == -1) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Unexpected end of stream in delimited string literal"); } else if (next != c) { break; } sb.append(Character.toChars(in_stream.read())); next = in_stream.peek(); } if (next == '"' && sb.toString().equals(end_delim)) { in_stream.read(); break; } result.append(sb); } else if (is_nesting && next == start_delim.charAt(0)) { nests++; } else if (next == end_delim.charAt(0)) { nests--; if (nests < 0) { if (in_stream.peek(2) == '"') { in_stream.read(); in_stream.read(); break; } else { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Improperly delimited string"); } } } result.append(Character.toChars(in_stream.read())); next = in_stream.peek(); } if (next == -1) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Unexpected end of stream in delimited string literal"); } return factory.create(TokenType.LiteralUtf8, start_index, start_line, start_col, in_stream.getPosition(), in_stream.getLine(), in_stream.getCol(), result.toString()); } LexDelimitedString(TokenFactory factory, LexerStream in_stream); Token read(); }
LexDelimitedString { public Token read() throws IOException, LexerException { int start_index = in_stream.getPosition(); int start_line = in_stream.getLine(); int start_col = in_stream.getCol(); int next = in_stream.peek(); if (next == -1) { throw new LexerException(start_line, start_col, "Unexpected end of input stream when parsing delimited string literal"); } if (next != 'q') { throw new LexerException(start_line, start_col, "Not a delimited string literal"); } in_stream.read(); next = in_stream.peek(); if (next != '"') { throw new LexerException(start_line, start_col, "Not a delimited string literal"); } in_stream.read(); next = in_stream.peek(); boolean is_identifier = false; boolean is_nesting = false; int nests = 0; StringBuilder delimiter = new StringBuilder(); if (is_nesting_open(next)) { is_nesting = true; delimiter.append(Character.toChars(in_stream.read())); } else if (Character.isLetter(next) || next == '_') { is_identifier = true; delimiter.append(Character.toChars(in_stream.read())); next = in_stream.peek(); while (Character.isLetterOrDigit(next) || next == '_') { delimiter.append(Character.toChars(in_stream.read())); next = in_stream.peek(); } if (next != '\n') { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "String delimiter must be followed by newline"); } in_stream.read(); } else { delimiter.append(Character.toChars(in_stream.read())); } next = in_stream.peek(); String start_delim = delimiter.toString(); String end_delim; if (is_identifier) { end_delim = start_delim; } else { end_delim = String.valueOf((char)valid_close(start_delim.charAt(0))); } StringBuilder result = new StringBuilder(); while (next != -1) { if (next == '\n' && is_identifier) { result.append((char)in_stream.read()); next = in_stream.peek(); StringBuilder sb = new StringBuilder(); for (char c : end_delim.toCharArray()) { if (next == -1) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Unexpected end of stream in delimited string literal"); } else if (next != c) { break; } sb.append(Character.toChars(in_stream.read())); next = in_stream.peek(); } if (next == '"' && sb.toString().equals(end_delim)) { in_stream.read(); break; } result.append(sb); } else if (is_nesting && next == start_delim.charAt(0)) { nests++; } else if (next == end_delim.charAt(0)) { nests--; if (nests < 0) { if (in_stream.peek(2) == '"') { in_stream.read(); in_stream.read(); break; } else { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Improperly delimited string"); } } } result.append(Character.toChars(in_stream.read())); next = in_stream.peek(); } if (next == -1) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Unexpected end of stream in delimited string literal"); } return factory.create(TokenType.LiteralUtf8, start_index, start_line, start_col, in_stream.getPosition(), in_stream.getLine(), in_stream.getCol(), result.toString()); } LexDelimitedString(TokenFactory factory, LexerStream in_stream); Token read(); }
@Test public void parens() throws Exception { String str = "q\"((f)\""; LexerStream ls = new LexerStream(new StringReader(str)); LexDelimitedString lex = new LexDelimitedString(new BaseTokenFactory(), ls); Token tok = lex.read(); Assert.assertEquals(TokenType.LiteralUtf8, tok.getType()); assertTrue(tok.getValue() instanceof String); Assert.assertEquals("(f", tok.getValue()); Assert.assertEquals(1, tok.getLine()); Assert.assertEquals(1, tok.getCol()); Assert.assertEquals(1, tok.getEndLine()); Assert.assertEquals(13, tok.getEndCol()); }
public Token read() throws IOException, LexerException { int start_index = in_stream.getPosition(); int start_line = in_stream.getLine(); int start_col = in_stream.getCol(); int next = in_stream.peek(); if (next == -1) { throw new LexerException(start_line, start_col, "Unexpected end of input stream when parsing delimited string literal"); } if (next != 'q') { throw new LexerException(start_line, start_col, "Not a delimited string literal"); } in_stream.read(); next = in_stream.peek(); if (next != '"') { throw new LexerException(start_line, start_col, "Not a delimited string literal"); } in_stream.read(); next = in_stream.peek(); boolean is_identifier = false; boolean is_nesting = false; int nests = 0; StringBuilder delimiter = new StringBuilder(); if (is_nesting_open(next)) { is_nesting = true; delimiter.append(Character.toChars(in_stream.read())); } else if (Character.isLetter(next) || next == '_') { is_identifier = true; delimiter.append(Character.toChars(in_stream.read())); next = in_stream.peek(); while (Character.isLetterOrDigit(next) || next == '_') { delimiter.append(Character.toChars(in_stream.read())); next = in_stream.peek(); } if (next != '\n') { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "String delimiter must be followed by newline"); } in_stream.read(); } else { delimiter.append(Character.toChars(in_stream.read())); } next = in_stream.peek(); String start_delim = delimiter.toString(); String end_delim; if (is_identifier) { end_delim = start_delim; } else { end_delim = String.valueOf((char)valid_close(start_delim.charAt(0))); } StringBuilder result = new StringBuilder(); while (next != -1) { if (next == '\n' && is_identifier) { result.append((char)in_stream.read()); next = in_stream.peek(); StringBuilder sb = new StringBuilder(); for (char c : end_delim.toCharArray()) { if (next == -1) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Unexpected end of stream in delimited string literal"); } else if (next != c) { break; } sb.append(Character.toChars(in_stream.read())); next = in_stream.peek(); } if (next == '"' && sb.toString().equals(end_delim)) { in_stream.read(); break; } result.append(sb); } else if (is_nesting && next == start_delim.charAt(0)) { nests++; } else if (next == end_delim.charAt(0)) { nests--; if (nests < 0) { if (in_stream.peek(2) == '"') { in_stream.read(); in_stream.read(); break; } else { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Improperly delimited string"); } } } result.append(Character.toChars(in_stream.read())); next = in_stream.peek(); } if (next == -1) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Unexpected end of stream in delimited string literal"); } return factory.create(TokenType.LiteralUtf8, start_index, start_line, start_col, in_stream.getPosition(), in_stream.getLine(), in_stream.getCol(), result.toString()); }
LexDelimitedString { public Token read() throws IOException, LexerException { int start_index = in_stream.getPosition(); int start_line = in_stream.getLine(); int start_col = in_stream.getCol(); int next = in_stream.peek(); if (next == -1) { throw new LexerException(start_line, start_col, "Unexpected end of input stream when parsing delimited string literal"); } if (next != 'q') { throw new LexerException(start_line, start_col, "Not a delimited string literal"); } in_stream.read(); next = in_stream.peek(); if (next != '"') { throw new LexerException(start_line, start_col, "Not a delimited string literal"); } in_stream.read(); next = in_stream.peek(); boolean is_identifier = false; boolean is_nesting = false; int nests = 0; StringBuilder delimiter = new StringBuilder(); if (is_nesting_open(next)) { is_nesting = true; delimiter.append(Character.toChars(in_stream.read())); } else if (Character.isLetter(next) || next == '_') { is_identifier = true; delimiter.append(Character.toChars(in_stream.read())); next = in_stream.peek(); while (Character.isLetterOrDigit(next) || next == '_') { delimiter.append(Character.toChars(in_stream.read())); next = in_stream.peek(); } if (next != '\n') { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "String delimiter must be followed by newline"); } in_stream.read(); } else { delimiter.append(Character.toChars(in_stream.read())); } next = in_stream.peek(); String start_delim = delimiter.toString(); String end_delim; if (is_identifier) { end_delim = start_delim; } else { end_delim = String.valueOf((char)valid_close(start_delim.charAt(0))); } StringBuilder result = new StringBuilder(); while (next != -1) { if (next == '\n' && is_identifier) { result.append((char)in_stream.read()); next = in_stream.peek(); StringBuilder sb = new StringBuilder(); for (char c : end_delim.toCharArray()) { if (next == -1) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Unexpected end of stream in delimited string literal"); } else if (next != c) { break; } sb.append(Character.toChars(in_stream.read())); next = in_stream.peek(); } if (next == '"' && sb.toString().equals(end_delim)) { in_stream.read(); break; } result.append(sb); } else if (is_nesting && next == start_delim.charAt(0)) { nests++; } else if (next == end_delim.charAt(0)) { nests--; if (nests < 0) { if (in_stream.peek(2) == '"') { in_stream.read(); in_stream.read(); break; } else { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Improperly delimited string"); } } } result.append(Character.toChars(in_stream.read())); next = in_stream.peek(); } if (next == -1) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Unexpected end of stream in delimited string literal"); } return factory.create(TokenType.LiteralUtf8, start_index, start_line, start_col, in_stream.getPosition(), in_stream.getLine(), in_stream.getCol(), result.toString()); } }
LexDelimitedString { public Token read() throws IOException, LexerException { int start_index = in_stream.getPosition(); int start_line = in_stream.getLine(); int start_col = in_stream.getCol(); int next = in_stream.peek(); if (next == -1) { throw new LexerException(start_line, start_col, "Unexpected end of input stream when parsing delimited string literal"); } if (next != 'q') { throw new LexerException(start_line, start_col, "Not a delimited string literal"); } in_stream.read(); next = in_stream.peek(); if (next != '"') { throw new LexerException(start_line, start_col, "Not a delimited string literal"); } in_stream.read(); next = in_stream.peek(); boolean is_identifier = false; boolean is_nesting = false; int nests = 0; StringBuilder delimiter = new StringBuilder(); if (is_nesting_open(next)) { is_nesting = true; delimiter.append(Character.toChars(in_stream.read())); } else if (Character.isLetter(next) || next == '_') { is_identifier = true; delimiter.append(Character.toChars(in_stream.read())); next = in_stream.peek(); while (Character.isLetterOrDigit(next) || next == '_') { delimiter.append(Character.toChars(in_stream.read())); next = in_stream.peek(); } if (next != '\n') { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "String delimiter must be followed by newline"); } in_stream.read(); } else { delimiter.append(Character.toChars(in_stream.read())); } next = in_stream.peek(); String start_delim = delimiter.toString(); String end_delim; if (is_identifier) { end_delim = start_delim; } else { end_delim = String.valueOf((char)valid_close(start_delim.charAt(0))); } StringBuilder result = new StringBuilder(); while (next != -1) { if (next == '\n' && is_identifier) { result.append((char)in_stream.read()); next = in_stream.peek(); StringBuilder sb = new StringBuilder(); for (char c : end_delim.toCharArray()) { if (next == -1) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Unexpected end of stream in delimited string literal"); } else if (next != c) { break; } sb.append(Character.toChars(in_stream.read())); next = in_stream.peek(); } if (next == '"' && sb.toString().equals(end_delim)) { in_stream.read(); break; } result.append(sb); } else if (is_nesting && next == start_delim.charAt(0)) { nests++; } else if (next == end_delim.charAt(0)) { nests--; if (nests < 0) { if (in_stream.peek(2) == '"') { in_stream.read(); in_stream.read(); break; } else { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Improperly delimited string"); } } } result.append(Character.toChars(in_stream.read())); next = in_stream.peek(); } if (next == -1) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Unexpected end of stream in delimited string literal"); } return factory.create(TokenType.LiteralUtf8, start_index, start_line, start_col, in_stream.getPosition(), in_stream.getLine(), in_stream.getCol(), result.toString()); } LexDelimitedString(TokenFactory factory, LexerStream in_stream); }
LexDelimitedString { public Token read() throws IOException, LexerException { int start_index = in_stream.getPosition(); int start_line = in_stream.getLine(); int start_col = in_stream.getCol(); int next = in_stream.peek(); if (next == -1) { throw new LexerException(start_line, start_col, "Unexpected end of input stream when parsing delimited string literal"); } if (next != 'q') { throw new LexerException(start_line, start_col, "Not a delimited string literal"); } in_stream.read(); next = in_stream.peek(); if (next != '"') { throw new LexerException(start_line, start_col, "Not a delimited string literal"); } in_stream.read(); next = in_stream.peek(); boolean is_identifier = false; boolean is_nesting = false; int nests = 0; StringBuilder delimiter = new StringBuilder(); if (is_nesting_open(next)) { is_nesting = true; delimiter.append(Character.toChars(in_stream.read())); } else if (Character.isLetter(next) || next == '_') { is_identifier = true; delimiter.append(Character.toChars(in_stream.read())); next = in_stream.peek(); while (Character.isLetterOrDigit(next) || next == '_') { delimiter.append(Character.toChars(in_stream.read())); next = in_stream.peek(); } if (next != '\n') { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "String delimiter must be followed by newline"); } in_stream.read(); } else { delimiter.append(Character.toChars(in_stream.read())); } next = in_stream.peek(); String start_delim = delimiter.toString(); String end_delim; if (is_identifier) { end_delim = start_delim; } else { end_delim = String.valueOf((char)valid_close(start_delim.charAt(0))); } StringBuilder result = new StringBuilder(); while (next != -1) { if (next == '\n' && is_identifier) { result.append((char)in_stream.read()); next = in_stream.peek(); StringBuilder sb = new StringBuilder(); for (char c : end_delim.toCharArray()) { if (next == -1) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Unexpected end of stream in delimited string literal"); } else if (next != c) { break; } sb.append(Character.toChars(in_stream.read())); next = in_stream.peek(); } if (next == '"' && sb.toString().equals(end_delim)) { in_stream.read(); break; } result.append(sb); } else if (is_nesting && next == start_delim.charAt(0)) { nests++; } else if (next == end_delim.charAt(0)) { nests--; if (nests < 0) { if (in_stream.peek(2) == '"') { in_stream.read(); in_stream.read(); break; } else { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Improperly delimited string"); } } } result.append(Character.toChars(in_stream.read())); next = in_stream.peek(); } if (next == -1) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Unexpected end of stream in delimited string literal"); } return factory.create(TokenType.LiteralUtf8, start_index, start_line, start_col, in_stream.getPosition(), in_stream.getLine(), in_stream.getCol(), result.toString()); } LexDelimitedString(TokenFactory factory, LexerStream in_stream); Token read(); }
LexDelimitedString { public Token read() throws IOException, LexerException { int start_index = in_stream.getPosition(); int start_line = in_stream.getLine(); int start_col = in_stream.getCol(); int next = in_stream.peek(); if (next == -1) { throw new LexerException(start_line, start_col, "Unexpected end of input stream when parsing delimited string literal"); } if (next != 'q') { throw new LexerException(start_line, start_col, "Not a delimited string literal"); } in_stream.read(); next = in_stream.peek(); if (next != '"') { throw new LexerException(start_line, start_col, "Not a delimited string literal"); } in_stream.read(); next = in_stream.peek(); boolean is_identifier = false; boolean is_nesting = false; int nests = 0; StringBuilder delimiter = new StringBuilder(); if (is_nesting_open(next)) { is_nesting = true; delimiter.append(Character.toChars(in_stream.read())); } else if (Character.isLetter(next) || next == '_') { is_identifier = true; delimiter.append(Character.toChars(in_stream.read())); next = in_stream.peek(); while (Character.isLetterOrDigit(next) || next == '_') { delimiter.append(Character.toChars(in_stream.read())); next = in_stream.peek(); } if (next != '\n') { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "String delimiter must be followed by newline"); } in_stream.read(); } else { delimiter.append(Character.toChars(in_stream.read())); } next = in_stream.peek(); String start_delim = delimiter.toString(); String end_delim; if (is_identifier) { end_delim = start_delim; } else { end_delim = String.valueOf((char)valid_close(start_delim.charAt(0))); } StringBuilder result = new StringBuilder(); while (next != -1) { if (next == '\n' && is_identifier) { result.append((char)in_stream.read()); next = in_stream.peek(); StringBuilder sb = new StringBuilder(); for (char c : end_delim.toCharArray()) { if (next == -1) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Unexpected end of stream in delimited string literal"); } else if (next != c) { break; } sb.append(Character.toChars(in_stream.read())); next = in_stream.peek(); } if (next == '"' && sb.toString().equals(end_delim)) { in_stream.read(); break; } result.append(sb); } else if (is_nesting && next == start_delim.charAt(0)) { nests++; } else if (next == end_delim.charAt(0)) { nests--; if (nests < 0) { if (in_stream.peek(2) == '"') { in_stream.read(); in_stream.read(); break; } else { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Improperly delimited string"); } } } result.append(Character.toChars(in_stream.read())); next = in_stream.peek(); } if (next == -1) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Unexpected end of stream in delimited string literal"); } return factory.create(TokenType.LiteralUtf8, start_index, start_line, start_col, in_stream.getPosition(), in_stream.getLine(), in_stream.getCol(), result.toString()); } LexDelimitedString(TokenFactory factory, LexerStream in_stream); Token read(); }
@Test public void identifier() throws Exception { String str = "q\"END\nsome\nmultiline\nstring\nEND\" "; LexerStream ls = new LexerStream(new StringReader(str)); LexDelimitedString lex = new LexDelimitedString(new BaseTokenFactory(), ls); Token tok = lex.read(); Assert.assertEquals(TokenType.LiteralUtf8, tok.getType()); assertTrue(tok.getValue() instanceof String); Assert.assertEquals("some\nmultiline\nstring\n", tok.getValue()); Assert.assertEquals(1, tok.getLine()); Assert.assertEquals(1, tok.getCol()); Assert.assertEquals(5, tok.getEndLine()); Assert.assertEquals(5, tok.getEndCol()); }
public Token read() throws IOException, LexerException { int start_index = in_stream.getPosition(); int start_line = in_stream.getLine(); int start_col = in_stream.getCol(); int next = in_stream.peek(); if (next == -1) { throw new LexerException(start_line, start_col, "Unexpected end of input stream when parsing delimited string literal"); } if (next != 'q') { throw new LexerException(start_line, start_col, "Not a delimited string literal"); } in_stream.read(); next = in_stream.peek(); if (next != '"') { throw new LexerException(start_line, start_col, "Not a delimited string literal"); } in_stream.read(); next = in_stream.peek(); boolean is_identifier = false; boolean is_nesting = false; int nests = 0; StringBuilder delimiter = new StringBuilder(); if (is_nesting_open(next)) { is_nesting = true; delimiter.append(Character.toChars(in_stream.read())); } else if (Character.isLetter(next) || next == '_') { is_identifier = true; delimiter.append(Character.toChars(in_stream.read())); next = in_stream.peek(); while (Character.isLetterOrDigit(next) || next == '_') { delimiter.append(Character.toChars(in_stream.read())); next = in_stream.peek(); } if (next != '\n') { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "String delimiter must be followed by newline"); } in_stream.read(); } else { delimiter.append(Character.toChars(in_stream.read())); } next = in_stream.peek(); String start_delim = delimiter.toString(); String end_delim; if (is_identifier) { end_delim = start_delim; } else { end_delim = String.valueOf((char)valid_close(start_delim.charAt(0))); } StringBuilder result = new StringBuilder(); while (next != -1) { if (next == '\n' && is_identifier) { result.append((char)in_stream.read()); next = in_stream.peek(); StringBuilder sb = new StringBuilder(); for (char c : end_delim.toCharArray()) { if (next == -1) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Unexpected end of stream in delimited string literal"); } else if (next != c) { break; } sb.append(Character.toChars(in_stream.read())); next = in_stream.peek(); } if (next == '"' && sb.toString().equals(end_delim)) { in_stream.read(); break; } result.append(sb); } else if (is_nesting && next == start_delim.charAt(0)) { nests++; } else if (next == end_delim.charAt(0)) { nests--; if (nests < 0) { if (in_stream.peek(2) == '"') { in_stream.read(); in_stream.read(); break; } else { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Improperly delimited string"); } } } result.append(Character.toChars(in_stream.read())); next = in_stream.peek(); } if (next == -1) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Unexpected end of stream in delimited string literal"); } return factory.create(TokenType.LiteralUtf8, start_index, start_line, start_col, in_stream.getPosition(), in_stream.getLine(), in_stream.getCol(), result.toString()); }
LexDelimitedString { public Token read() throws IOException, LexerException { int start_index = in_stream.getPosition(); int start_line = in_stream.getLine(); int start_col = in_stream.getCol(); int next = in_stream.peek(); if (next == -1) { throw new LexerException(start_line, start_col, "Unexpected end of input stream when parsing delimited string literal"); } if (next != 'q') { throw new LexerException(start_line, start_col, "Not a delimited string literal"); } in_stream.read(); next = in_stream.peek(); if (next != '"') { throw new LexerException(start_line, start_col, "Not a delimited string literal"); } in_stream.read(); next = in_stream.peek(); boolean is_identifier = false; boolean is_nesting = false; int nests = 0; StringBuilder delimiter = new StringBuilder(); if (is_nesting_open(next)) { is_nesting = true; delimiter.append(Character.toChars(in_stream.read())); } else if (Character.isLetter(next) || next == '_') { is_identifier = true; delimiter.append(Character.toChars(in_stream.read())); next = in_stream.peek(); while (Character.isLetterOrDigit(next) || next == '_') { delimiter.append(Character.toChars(in_stream.read())); next = in_stream.peek(); } if (next != '\n') { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "String delimiter must be followed by newline"); } in_stream.read(); } else { delimiter.append(Character.toChars(in_stream.read())); } next = in_stream.peek(); String start_delim = delimiter.toString(); String end_delim; if (is_identifier) { end_delim = start_delim; } else { end_delim = String.valueOf((char)valid_close(start_delim.charAt(0))); } StringBuilder result = new StringBuilder(); while (next != -1) { if (next == '\n' && is_identifier) { result.append((char)in_stream.read()); next = in_stream.peek(); StringBuilder sb = new StringBuilder(); for (char c : end_delim.toCharArray()) { if (next == -1) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Unexpected end of stream in delimited string literal"); } else if (next != c) { break; } sb.append(Character.toChars(in_stream.read())); next = in_stream.peek(); } if (next == '"' && sb.toString().equals(end_delim)) { in_stream.read(); break; } result.append(sb); } else if (is_nesting && next == start_delim.charAt(0)) { nests++; } else if (next == end_delim.charAt(0)) { nests--; if (nests < 0) { if (in_stream.peek(2) == '"') { in_stream.read(); in_stream.read(); break; } else { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Improperly delimited string"); } } } result.append(Character.toChars(in_stream.read())); next = in_stream.peek(); } if (next == -1) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Unexpected end of stream in delimited string literal"); } return factory.create(TokenType.LiteralUtf8, start_index, start_line, start_col, in_stream.getPosition(), in_stream.getLine(), in_stream.getCol(), result.toString()); } }
LexDelimitedString { public Token read() throws IOException, LexerException { int start_index = in_stream.getPosition(); int start_line = in_stream.getLine(); int start_col = in_stream.getCol(); int next = in_stream.peek(); if (next == -1) { throw new LexerException(start_line, start_col, "Unexpected end of input stream when parsing delimited string literal"); } if (next != 'q') { throw new LexerException(start_line, start_col, "Not a delimited string literal"); } in_stream.read(); next = in_stream.peek(); if (next != '"') { throw new LexerException(start_line, start_col, "Not a delimited string literal"); } in_stream.read(); next = in_stream.peek(); boolean is_identifier = false; boolean is_nesting = false; int nests = 0; StringBuilder delimiter = new StringBuilder(); if (is_nesting_open(next)) { is_nesting = true; delimiter.append(Character.toChars(in_stream.read())); } else if (Character.isLetter(next) || next == '_') { is_identifier = true; delimiter.append(Character.toChars(in_stream.read())); next = in_stream.peek(); while (Character.isLetterOrDigit(next) || next == '_') { delimiter.append(Character.toChars(in_stream.read())); next = in_stream.peek(); } if (next != '\n') { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "String delimiter must be followed by newline"); } in_stream.read(); } else { delimiter.append(Character.toChars(in_stream.read())); } next = in_stream.peek(); String start_delim = delimiter.toString(); String end_delim; if (is_identifier) { end_delim = start_delim; } else { end_delim = String.valueOf((char)valid_close(start_delim.charAt(0))); } StringBuilder result = new StringBuilder(); while (next != -1) { if (next == '\n' && is_identifier) { result.append((char)in_stream.read()); next = in_stream.peek(); StringBuilder sb = new StringBuilder(); for (char c : end_delim.toCharArray()) { if (next == -1) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Unexpected end of stream in delimited string literal"); } else if (next != c) { break; } sb.append(Character.toChars(in_stream.read())); next = in_stream.peek(); } if (next == '"' && sb.toString().equals(end_delim)) { in_stream.read(); break; } result.append(sb); } else if (is_nesting && next == start_delim.charAt(0)) { nests++; } else if (next == end_delim.charAt(0)) { nests--; if (nests < 0) { if (in_stream.peek(2) == '"') { in_stream.read(); in_stream.read(); break; } else { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Improperly delimited string"); } } } result.append(Character.toChars(in_stream.read())); next = in_stream.peek(); } if (next == -1) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Unexpected end of stream in delimited string literal"); } return factory.create(TokenType.LiteralUtf8, start_index, start_line, start_col, in_stream.getPosition(), in_stream.getLine(), in_stream.getCol(), result.toString()); } LexDelimitedString(TokenFactory factory, LexerStream in_stream); }
LexDelimitedString { public Token read() throws IOException, LexerException { int start_index = in_stream.getPosition(); int start_line = in_stream.getLine(); int start_col = in_stream.getCol(); int next = in_stream.peek(); if (next == -1) { throw new LexerException(start_line, start_col, "Unexpected end of input stream when parsing delimited string literal"); } if (next != 'q') { throw new LexerException(start_line, start_col, "Not a delimited string literal"); } in_stream.read(); next = in_stream.peek(); if (next != '"') { throw new LexerException(start_line, start_col, "Not a delimited string literal"); } in_stream.read(); next = in_stream.peek(); boolean is_identifier = false; boolean is_nesting = false; int nests = 0; StringBuilder delimiter = new StringBuilder(); if (is_nesting_open(next)) { is_nesting = true; delimiter.append(Character.toChars(in_stream.read())); } else if (Character.isLetter(next) || next == '_') { is_identifier = true; delimiter.append(Character.toChars(in_stream.read())); next = in_stream.peek(); while (Character.isLetterOrDigit(next) || next == '_') { delimiter.append(Character.toChars(in_stream.read())); next = in_stream.peek(); } if (next != '\n') { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "String delimiter must be followed by newline"); } in_stream.read(); } else { delimiter.append(Character.toChars(in_stream.read())); } next = in_stream.peek(); String start_delim = delimiter.toString(); String end_delim; if (is_identifier) { end_delim = start_delim; } else { end_delim = String.valueOf((char)valid_close(start_delim.charAt(0))); } StringBuilder result = new StringBuilder(); while (next != -1) { if (next == '\n' && is_identifier) { result.append((char)in_stream.read()); next = in_stream.peek(); StringBuilder sb = new StringBuilder(); for (char c : end_delim.toCharArray()) { if (next == -1) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Unexpected end of stream in delimited string literal"); } else if (next != c) { break; } sb.append(Character.toChars(in_stream.read())); next = in_stream.peek(); } if (next == '"' && sb.toString().equals(end_delim)) { in_stream.read(); break; } result.append(sb); } else if (is_nesting && next == start_delim.charAt(0)) { nests++; } else if (next == end_delim.charAt(0)) { nests--; if (nests < 0) { if (in_stream.peek(2) == '"') { in_stream.read(); in_stream.read(); break; } else { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Improperly delimited string"); } } } result.append(Character.toChars(in_stream.read())); next = in_stream.peek(); } if (next == -1) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Unexpected end of stream in delimited string literal"); } return factory.create(TokenType.LiteralUtf8, start_index, start_line, start_col, in_stream.getPosition(), in_stream.getLine(), in_stream.getCol(), result.toString()); } LexDelimitedString(TokenFactory factory, LexerStream in_stream); Token read(); }
LexDelimitedString { public Token read() throws IOException, LexerException { int start_index = in_stream.getPosition(); int start_line = in_stream.getLine(); int start_col = in_stream.getCol(); int next = in_stream.peek(); if (next == -1) { throw new LexerException(start_line, start_col, "Unexpected end of input stream when parsing delimited string literal"); } if (next != 'q') { throw new LexerException(start_line, start_col, "Not a delimited string literal"); } in_stream.read(); next = in_stream.peek(); if (next != '"') { throw new LexerException(start_line, start_col, "Not a delimited string literal"); } in_stream.read(); next = in_stream.peek(); boolean is_identifier = false; boolean is_nesting = false; int nests = 0; StringBuilder delimiter = new StringBuilder(); if (is_nesting_open(next)) { is_nesting = true; delimiter.append(Character.toChars(in_stream.read())); } else if (Character.isLetter(next) || next == '_') { is_identifier = true; delimiter.append(Character.toChars(in_stream.read())); next = in_stream.peek(); while (Character.isLetterOrDigit(next) || next == '_') { delimiter.append(Character.toChars(in_stream.read())); next = in_stream.peek(); } if (next != '\n') { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "String delimiter must be followed by newline"); } in_stream.read(); } else { delimiter.append(Character.toChars(in_stream.read())); } next = in_stream.peek(); String start_delim = delimiter.toString(); String end_delim; if (is_identifier) { end_delim = start_delim; } else { end_delim = String.valueOf((char)valid_close(start_delim.charAt(0))); } StringBuilder result = new StringBuilder(); while (next != -1) { if (next == '\n' && is_identifier) { result.append((char)in_stream.read()); next = in_stream.peek(); StringBuilder sb = new StringBuilder(); for (char c : end_delim.toCharArray()) { if (next == -1) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Unexpected end of stream in delimited string literal"); } else if (next != c) { break; } sb.append(Character.toChars(in_stream.read())); next = in_stream.peek(); } if (next == '"' && sb.toString().equals(end_delim)) { in_stream.read(); break; } result.append(sb); } else if (is_nesting && next == start_delim.charAt(0)) { nests++; } else if (next == end_delim.charAt(0)) { nests--; if (nests < 0) { if (in_stream.peek(2) == '"') { in_stream.read(); in_stream.read(); break; } else { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Improperly delimited string"); } } } result.append(Character.toChars(in_stream.read())); next = in_stream.peek(); } if (next == -1) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Unexpected end of stream in delimited string literal"); } return factory.create(TokenType.LiteralUtf8, start_index, start_line, start_col, in_stream.getPosition(), in_stream.getLine(), in_stream.getCol(), result.toString()); } LexDelimitedString(TokenFactory factory, LexerStream in_stream); Token read(); }
@Test(expected = LexerException.class) public void empty() throws Exception { String str = ""; LexerStream ls = new LexerStream(new StringReader(str)); LexDelimitedString lex = new LexDelimitedString(new BaseTokenFactory(), ls); lex.read(); }
public Token read() throws IOException, LexerException { int start_index = in_stream.getPosition(); int start_line = in_stream.getLine(); int start_col = in_stream.getCol(); int next = in_stream.peek(); if (next == -1) { throw new LexerException(start_line, start_col, "Unexpected end of input stream when parsing delimited string literal"); } if (next != 'q') { throw new LexerException(start_line, start_col, "Not a delimited string literal"); } in_stream.read(); next = in_stream.peek(); if (next != '"') { throw new LexerException(start_line, start_col, "Not a delimited string literal"); } in_stream.read(); next = in_stream.peek(); boolean is_identifier = false; boolean is_nesting = false; int nests = 0; StringBuilder delimiter = new StringBuilder(); if (is_nesting_open(next)) { is_nesting = true; delimiter.append(Character.toChars(in_stream.read())); } else if (Character.isLetter(next) || next == '_') { is_identifier = true; delimiter.append(Character.toChars(in_stream.read())); next = in_stream.peek(); while (Character.isLetterOrDigit(next) || next == '_') { delimiter.append(Character.toChars(in_stream.read())); next = in_stream.peek(); } if (next != '\n') { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "String delimiter must be followed by newline"); } in_stream.read(); } else { delimiter.append(Character.toChars(in_stream.read())); } next = in_stream.peek(); String start_delim = delimiter.toString(); String end_delim; if (is_identifier) { end_delim = start_delim; } else { end_delim = String.valueOf((char)valid_close(start_delim.charAt(0))); } StringBuilder result = new StringBuilder(); while (next != -1) { if (next == '\n' && is_identifier) { result.append((char)in_stream.read()); next = in_stream.peek(); StringBuilder sb = new StringBuilder(); for (char c : end_delim.toCharArray()) { if (next == -1) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Unexpected end of stream in delimited string literal"); } else if (next != c) { break; } sb.append(Character.toChars(in_stream.read())); next = in_stream.peek(); } if (next == '"' && sb.toString().equals(end_delim)) { in_stream.read(); break; } result.append(sb); } else if (is_nesting && next == start_delim.charAt(0)) { nests++; } else if (next == end_delim.charAt(0)) { nests--; if (nests < 0) { if (in_stream.peek(2) == '"') { in_stream.read(); in_stream.read(); break; } else { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Improperly delimited string"); } } } result.append(Character.toChars(in_stream.read())); next = in_stream.peek(); } if (next == -1) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Unexpected end of stream in delimited string literal"); } return factory.create(TokenType.LiteralUtf8, start_index, start_line, start_col, in_stream.getPosition(), in_stream.getLine(), in_stream.getCol(), result.toString()); }
LexDelimitedString { public Token read() throws IOException, LexerException { int start_index = in_stream.getPosition(); int start_line = in_stream.getLine(); int start_col = in_stream.getCol(); int next = in_stream.peek(); if (next == -1) { throw new LexerException(start_line, start_col, "Unexpected end of input stream when parsing delimited string literal"); } if (next != 'q') { throw new LexerException(start_line, start_col, "Not a delimited string literal"); } in_stream.read(); next = in_stream.peek(); if (next != '"') { throw new LexerException(start_line, start_col, "Not a delimited string literal"); } in_stream.read(); next = in_stream.peek(); boolean is_identifier = false; boolean is_nesting = false; int nests = 0; StringBuilder delimiter = new StringBuilder(); if (is_nesting_open(next)) { is_nesting = true; delimiter.append(Character.toChars(in_stream.read())); } else if (Character.isLetter(next) || next == '_') { is_identifier = true; delimiter.append(Character.toChars(in_stream.read())); next = in_stream.peek(); while (Character.isLetterOrDigit(next) || next == '_') { delimiter.append(Character.toChars(in_stream.read())); next = in_stream.peek(); } if (next != '\n') { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "String delimiter must be followed by newline"); } in_stream.read(); } else { delimiter.append(Character.toChars(in_stream.read())); } next = in_stream.peek(); String start_delim = delimiter.toString(); String end_delim; if (is_identifier) { end_delim = start_delim; } else { end_delim = String.valueOf((char)valid_close(start_delim.charAt(0))); } StringBuilder result = new StringBuilder(); while (next != -1) { if (next == '\n' && is_identifier) { result.append((char)in_stream.read()); next = in_stream.peek(); StringBuilder sb = new StringBuilder(); for (char c : end_delim.toCharArray()) { if (next == -1) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Unexpected end of stream in delimited string literal"); } else if (next != c) { break; } sb.append(Character.toChars(in_stream.read())); next = in_stream.peek(); } if (next == '"' && sb.toString().equals(end_delim)) { in_stream.read(); break; } result.append(sb); } else if (is_nesting && next == start_delim.charAt(0)) { nests++; } else if (next == end_delim.charAt(0)) { nests--; if (nests < 0) { if (in_stream.peek(2) == '"') { in_stream.read(); in_stream.read(); break; } else { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Improperly delimited string"); } } } result.append(Character.toChars(in_stream.read())); next = in_stream.peek(); } if (next == -1) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Unexpected end of stream in delimited string literal"); } return factory.create(TokenType.LiteralUtf8, start_index, start_line, start_col, in_stream.getPosition(), in_stream.getLine(), in_stream.getCol(), result.toString()); } }
LexDelimitedString { public Token read() throws IOException, LexerException { int start_index = in_stream.getPosition(); int start_line = in_stream.getLine(); int start_col = in_stream.getCol(); int next = in_stream.peek(); if (next == -1) { throw new LexerException(start_line, start_col, "Unexpected end of input stream when parsing delimited string literal"); } if (next != 'q') { throw new LexerException(start_line, start_col, "Not a delimited string literal"); } in_stream.read(); next = in_stream.peek(); if (next != '"') { throw new LexerException(start_line, start_col, "Not a delimited string literal"); } in_stream.read(); next = in_stream.peek(); boolean is_identifier = false; boolean is_nesting = false; int nests = 0; StringBuilder delimiter = new StringBuilder(); if (is_nesting_open(next)) { is_nesting = true; delimiter.append(Character.toChars(in_stream.read())); } else if (Character.isLetter(next) || next == '_') { is_identifier = true; delimiter.append(Character.toChars(in_stream.read())); next = in_stream.peek(); while (Character.isLetterOrDigit(next) || next == '_') { delimiter.append(Character.toChars(in_stream.read())); next = in_stream.peek(); } if (next != '\n') { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "String delimiter must be followed by newline"); } in_stream.read(); } else { delimiter.append(Character.toChars(in_stream.read())); } next = in_stream.peek(); String start_delim = delimiter.toString(); String end_delim; if (is_identifier) { end_delim = start_delim; } else { end_delim = String.valueOf((char)valid_close(start_delim.charAt(0))); } StringBuilder result = new StringBuilder(); while (next != -1) { if (next == '\n' && is_identifier) { result.append((char)in_stream.read()); next = in_stream.peek(); StringBuilder sb = new StringBuilder(); for (char c : end_delim.toCharArray()) { if (next == -1) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Unexpected end of stream in delimited string literal"); } else if (next != c) { break; } sb.append(Character.toChars(in_stream.read())); next = in_stream.peek(); } if (next == '"' && sb.toString().equals(end_delim)) { in_stream.read(); break; } result.append(sb); } else if (is_nesting && next == start_delim.charAt(0)) { nests++; } else if (next == end_delim.charAt(0)) { nests--; if (nests < 0) { if (in_stream.peek(2) == '"') { in_stream.read(); in_stream.read(); break; } else { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Improperly delimited string"); } } } result.append(Character.toChars(in_stream.read())); next = in_stream.peek(); } if (next == -1) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Unexpected end of stream in delimited string literal"); } return factory.create(TokenType.LiteralUtf8, start_index, start_line, start_col, in_stream.getPosition(), in_stream.getLine(), in_stream.getCol(), result.toString()); } LexDelimitedString(TokenFactory factory, LexerStream in_stream); }
LexDelimitedString { public Token read() throws IOException, LexerException { int start_index = in_stream.getPosition(); int start_line = in_stream.getLine(); int start_col = in_stream.getCol(); int next = in_stream.peek(); if (next == -1) { throw new LexerException(start_line, start_col, "Unexpected end of input stream when parsing delimited string literal"); } if (next != 'q') { throw new LexerException(start_line, start_col, "Not a delimited string literal"); } in_stream.read(); next = in_stream.peek(); if (next != '"') { throw new LexerException(start_line, start_col, "Not a delimited string literal"); } in_stream.read(); next = in_stream.peek(); boolean is_identifier = false; boolean is_nesting = false; int nests = 0; StringBuilder delimiter = new StringBuilder(); if (is_nesting_open(next)) { is_nesting = true; delimiter.append(Character.toChars(in_stream.read())); } else if (Character.isLetter(next) || next == '_') { is_identifier = true; delimiter.append(Character.toChars(in_stream.read())); next = in_stream.peek(); while (Character.isLetterOrDigit(next) || next == '_') { delimiter.append(Character.toChars(in_stream.read())); next = in_stream.peek(); } if (next != '\n') { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "String delimiter must be followed by newline"); } in_stream.read(); } else { delimiter.append(Character.toChars(in_stream.read())); } next = in_stream.peek(); String start_delim = delimiter.toString(); String end_delim; if (is_identifier) { end_delim = start_delim; } else { end_delim = String.valueOf((char)valid_close(start_delim.charAt(0))); } StringBuilder result = new StringBuilder(); while (next != -1) { if (next == '\n' && is_identifier) { result.append((char)in_stream.read()); next = in_stream.peek(); StringBuilder sb = new StringBuilder(); for (char c : end_delim.toCharArray()) { if (next == -1) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Unexpected end of stream in delimited string literal"); } else if (next != c) { break; } sb.append(Character.toChars(in_stream.read())); next = in_stream.peek(); } if (next == '"' && sb.toString().equals(end_delim)) { in_stream.read(); break; } result.append(sb); } else if (is_nesting && next == start_delim.charAt(0)) { nests++; } else if (next == end_delim.charAt(0)) { nests--; if (nests < 0) { if (in_stream.peek(2) == '"') { in_stream.read(); in_stream.read(); break; } else { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Improperly delimited string"); } } } result.append(Character.toChars(in_stream.read())); next = in_stream.peek(); } if (next == -1) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Unexpected end of stream in delimited string literal"); } return factory.create(TokenType.LiteralUtf8, start_index, start_line, start_col, in_stream.getPosition(), in_stream.getLine(), in_stream.getCol(), result.toString()); } LexDelimitedString(TokenFactory factory, LexerStream in_stream); Token read(); }
LexDelimitedString { public Token read() throws IOException, LexerException { int start_index = in_stream.getPosition(); int start_line = in_stream.getLine(); int start_col = in_stream.getCol(); int next = in_stream.peek(); if (next == -1) { throw new LexerException(start_line, start_col, "Unexpected end of input stream when parsing delimited string literal"); } if (next != 'q') { throw new LexerException(start_line, start_col, "Not a delimited string literal"); } in_stream.read(); next = in_stream.peek(); if (next != '"') { throw new LexerException(start_line, start_col, "Not a delimited string literal"); } in_stream.read(); next = in_stream.peek(); boolean is_identifier = false; boolean is_nesting = false; int nests = 0; StringBuilder delimiter = new StringBuilder(); if (is_nesting_open(next)) { is_nesting = true; delimiter.append(Character.toChars(in_stream.read())); } else if (Character.isLetter(next) || next == '_') { is_identifier = true; delimiter.append(Character.toChars(in_stream.read())); next = in_stream.peek(); while (Character.isLetterOrDigit(next) || next == '_') { delimiter.append(Character.toChars(in_stream.read())); next = in_stream.peek(); } if (next != '\n') { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "String delimiter must be followed by newline"); } in_stream.read(); } else { delimiter.append(Character.toChars(in_stream.read())); } next = in_stream.peek(); String start_delim = delimiter.toString(); String end_delim; if (is_identifier) { end_delim = start_delim; } else { end_delim = String.valueOf((char)valid_close(start_delim.charAt(0))); } StringBuilder result = new StringBuilder(); while (next != -1) { if (next == '\n' && is_identifier) { result.append((char)in_stream.read()); next = in_stream.peek(); StringBuilder sb = new StringBuilder(); for (char c : end_delim.toCharArray()) { if (next == -1) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Unexpected end of stream in delimited string literal"); } else if (next != c) { break; } sb.append(Character.toChars(in_stream.read())); next = in_stream.peek(); } if (next == '"' && sb.toString().equals(end_delim)) { in_stream.read(); break; } result.append(sb); } else if (is_nesting && next == start_delim.charAt(0)) { nests++; } else if (next == end_delim.charAt(0)) { nests--; if (nests < 0) { if (in_stream.peek(2) == '"') { in_stream.read(); in_stream.read(); break; } else { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Improperly delimited string"); } } } result.append(Character.toChars(in_stream.read())); next = in_stream.peek(); } if (next == -1) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Unexpected end of stream in delimited string literal"); } return factory.create(TokenType.LiteralUtf8, start_index, start_line, start_col, in_stream.getPosition(), in_stream.getLine(), in_stream.getCol(), result.toString()); } LexDelimitedString(TokenFactory factory, LexerStream in_stream); Token read(); }
@Test(expected = LexerException.class) public void not() throws Exception { String str = "q[]"; LexerStream ls = new LexerStream(new StringReader(str)); LexDelimitedString lex = new LexDelimitedString(new BaseTokenFactory(), ls); lex.read(); }
public Token read() throws IOException, LexerException { int start_index = in_stream.getPosition(); int start_line = in_stream.getLine(); int start_col = in_stream.getCol(); int next = in_stream.peek(); if (next == -1) { throw new LexerException(start_line, start_col, "Unexpected end of input stream when parsing delimited string literal"); } if (next != 'q') { throw new LexerException(start_line, start_col, "Not a delimited string literal"); } in_stream.read(); next = in_stream.peek(); if (next != '"') { throw new LexerException(start_line, start_col, "Not a delimited string literal"); } in_stream.read(); next = in_stream.peek(); boolean is_identifier = false; boolean is_nesting = false; int nests = 0; StringBuilder delimiter = new StringBuilder(); if (is_nesting_open(next)) { is_nesting = true; delimiter.append(Character.toChars(in_stream.read())); } else if (Character.isLetter(next) || next == '_') { is_identifier = true; delimiter.append(Character.toChars(in_stream.read())); next = in_stream.peek(); while (Character.isLetterOrDigit(next) || next == '_') { delimiter.append(Character.toChars(in_stream.read())); next = in_stream.peek(); } if (next != '\n') { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "String delimiter must be followed by newline"); } in_stream.read(); } else { delimiter.append(Character.toChars(in_stream.read())); } next = in_stream.peek(); String start_delim = delimiter.toString(); String end_delim; if (is_identifier) { end_delim = start_delim; } else { end_delim = String.valueOf((char)valid_close(start_delim.charAt(0))); } StringBuilder result = new StringBuilder(); while (next != -1) { if (next == '\n' && is_identifier) { result.append((char)in_stream.read()); next = in_stream.peek(); StringBuilder sb = new StringBuilder(); for (char c : end_delim.toCharArray()) { if (next == -1) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Unexpected end of stream in delimited string literal"); } else if (next != c) { break; } sb.append(Character.toChars(in_stream.read())); next = in_stream.peek(); } if (next == '"' && sb.toString().equals(end_delim)) { in_stream.read(); break; } result.append(sb); } else if (is_nesting && next == start_delim.charAt(0)) { nests++; } else if (next == end_delim.charAt(0)) { nests--; if (nests < 0) { if (in_stream.peek(2) == '"') { in_stream.read(); in_stream.read(); break; } else { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Improperly delimited string"); } } } result.append(Character.toChars(in_stream.read())); next = in_stream.peek(); } if (next == -1) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Unexpected end of stream in delimited string literal"); } return factory.create(TokenType.LiteralUtf8, start_index, start_line, start_col, in_stream.getPosition(), in_stream.getLine(), in_stream.getCol(), result.toString()); }
LexDelimitedString { public Token read() throws IOException, LexerException { int start_index = in_stream.getPosition(); int start_line = in_stream.getLine(); int start_col = in_stream.getCol(); int next = in_stream.peek(); if (next == -1) { throw new LexerException(start_line, start_col, "Unexpected end of input stream when parsing delimited string literal"); } if (next != 'q') { throw new LexerException(start_line, start_col, "Not a delimited string literal"); } in_stream.read(); next = in_stream.peek(); if (next != '"') { throw new LexerException(start_line, start_col, "Not a delimited string literal"); } in_stream.read(); next = in_stream.peek(); boolean is_identifier = false; boolean is_nesting = false; int nests = 0; StringBuilder delimiter = new StringBuilder(); if (is_nesting_open(next)) { is_nesting = true; delimiter.append(Character.toChars(in_stream.read())); } else if (Character.isLetter(next) || next == '_') { is_identifier = true; delimiter.append(Character.toChars(in_stream.read())); next = in_stream.peek(); while (Character.isLetterOrDigit(next) || next == '_') { delimiter.append(Character.toChars(in_stream.read())); next = in_stream.peek(); } if (next != '\n') { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "String delimiter must be followed by newline"); } in_stream.read(); } else { delimiter.append(Character.toChars(in_stream.read())); } next = in_stream.peek(); String start_delim = delimiter.toString(); String end_delim; if (is_identifier) { end_delim = start_delim; } else { end_delim = String.valueOf((char)valid_close(start_delim.charAt(0))); } StringBuilder result = new StringBuilder(); while (next != -1) { if (next == '\n' && is_identifier) { result.append((char)in_stream.read()); next = in_stream.peek(); StringBuilder sb = new StringBuilder(); for (char c : end_delim.toCharArray()) { if (next == -1) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Unexpected end of stream in delimited string literal"); } else if (next != c) { break; } sb.append(Character.toChars(in_stream.read())); next = in_stream.peek(); } if (next == '"' && sb.toString().equals(end_delim)) { in_stream.read(); break; } result.append(sb); } else if (is_nesting && next == start_delim.charAt(0)) { nests++; } else if (next == end_delim.charAt(0)) { nests--; if (nests < 0) { if (in_stream.peek(2) == '"') { in_stream.read(); in_stream.read(); break; } else { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Improperly delimited string"); } } } result.append(Character.toChars(in_stream.read())); next = in_stream.peek(); } if (next == -1) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Unexpected end of stream in delimited string literal"); } return factory.create(TokenType.LiteralUtf8, start_index, start_line, start_col, in_stream.getPosition(), in_stream.getLine(), in_stream.getCol(), result.toString()); } }
LexDelimitedString { public Token read() throws IOException, LexerException { int start_index = in_stream.getPosition(); int start_line = in_stream.getLine(); int start_col = in_stream.getCol(); int next = in_stream.peek(); if (next == -1) { throw new LexerException(start_line, start_col, "Unexpected end of input stream when parsing delimited string literal"); } if (next != 'q') { throw new LexerException(start_line, start_col, "Not a delimited string literal"); } in_stream.read(); next = in_stream.peek(); if (next != '"') { throw new LexerException(start_line, start_col, "Not a delimited string literal"); } in_stream.read(); next = in_stream.peek(); boolean is_identifier = false; boolean is_nesting = false; int nests = 0; StringBuilder delimiter = new StringBuilder(); if (is_nesting_open(next)) { is_nesting = true; delimiter.append(Character.toChars(in_stream.read())); } else if (Character.isLetter(next) || next == '_') { is_identifier = true; delimiter.append(Character.toChars(in_stream.read())); next = in_stream.peek(); while (Character.isLetterOrDigit(next) || next == '_') { delimiter.append(Character.toChars(in_stream.read())); next = in_stream.peek(); } if (next != '\n') { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "String delimiter must be followed by newline"); } in_stream.read(); } else { delimiter.append(Character.toChars(in_stream.read())); } next = in_stream.peek(); String start_delim = delimiter.toString(); String end_delim; if (is_identifier) { end_delim = start_delim; } else { end_delim = String.valueOf((char)valid_close(start_delim.charAt(0))); } StringBuilder result = new StringBuilder(); while (next != -1) { if (next == '\n' && is_identifier) { result.append((char)in_stream.read()); next = in_stream.peek(); StringBuilder sb = new StringBuilder(); for (char c : end_delim.toCharArray()) { if (next == -1) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Unexpected end of stream in delimited string literal"); } else if (next != c) { break; } sb.append(Character.toChars(in_stream.read())); next = in_stream.peek(); } if (next == '"' && sb.toString().equals(end_delim)) { in_stream.read(); break; } result.append(sb); } else if (is_nesting && next == start_delim.charAt(0)) { nests++; } else if (next == end_delim.charAt(0)) { nests--; if (nests < 0) { if (in_stream.peek(2) == '"') { in_stream.read(); in_stream.read(); break; } else { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Improperly delimited string"); } } } result.append(Character.toChars(in_stream.read())); next = in_stream.peek(); } if (next == -1) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Unexpected end of stream in delimited string literal"); } return factory.create(TokenType.LiteralUtf8, start_index, start_line, start_col, in_stream.getPosition(), in_stream.getLine(), in_stream.getCol(), result.toString()); } LexDelimitedString(TokenFactory factory, LexerStream in_stream); }
LexDelimitedString { public Token read() throws IOException, LexerException { int start_index = in_stream.getPosition(); int start_line = in_stream.getLine(); int start_col = in_stream.getCol(); int next = in_stream.peek(); if (next == -1) { throw new LexerException(start_line, start_col, "Unexpected end of input stream when parsing delimited string literal"); } if (next != 'q') { throw new LexerException(start_line, start_col, "Not a delimited string literal"); } in_stream.read(); next = in_stream.peek(); if (next != '"') { throw new LexerException(start_line, start_col, "Not a delimited string literal"); } in_stream.read(); next = in_stream.peek(); boolean is_identifier = false; boolean is_nesting = false; int nests = 0; StringBuilder delimiter = new StringBuilder(); if (is_nesting_open(next)) { is_nesting = true; delimiter.append(Character.toChars(in_stream.read())); } else if (Character.isLetter(next) || next == '_') { is_identifier = true; delimiter.append(Character.toChars(in_stream.read())); next = in_stream.peek(); while (Character.isLetterOrDigit(next) || next == '_') { delimiter.append(Character.toChars(in_stream.read())); next = in_stream.peek(); } if (next != '\n') { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "String delimiter must be followed by newline"); } in_stream.read(); } else { delimiter.append(Character.toChars(in_stream.read())); } next = in_stream.peek(); String start_delim = delimiter.toString(); String end_delim; if (is_identifier) { end_delim = start_delim; } else { end_delim = String.valueOf((char)valid_close(start_delim.charAt(0))); } StringBuilder result = new StringBuilder(); while (next != -1) { if (next == '\n' && is_identifier) { result.append((char)in_stream.read()); next = in_stream.peek(); StringBuilder sb = new StringBuilder(); for (char c : end_delim.toCharArray()) { if (next == -1) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Unexpected end of stream in delimited string literal"); } else if (next != c) { break; } sb.append(Character.toChars(in_stream.read())); next = in_stream.peek(); } if (next == '"' && sb.toString().equals(end_delim)) { in_stream.read(); break; } result.append(sb); } else if (is_nesting && next == start_delim.charAt(0)) { nests++; } else if (next == end_delim.charAt(0)) { nests--; if (nests < 0) { if (in_stream.peek(2) == '"') { in_stream.read(); in_stream.read(); break; } else { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Improperly delimited string"); } } } result.append(Character.toChars(in_stream.read())); next = in_stream.peek(); } if (next == -1) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Unexpected end of stream in delimited string literal"); } return factory.create(TokenType.LiteralUtf8, start_index, start_line, start_col, in_stream.getPosition(), in_stream.getLine(), in_stream.getCol(), result.toString()); } LexDelimitedString(TokenFactory factory, LexerStream in_stream); Token read(); }
LexDelimitedString { public Token read() throws IOException, LexerException { int start_index = in_stream.getPosition(); int start_line = in_stream.getLine(); int start_col = in_stream.getCol(); int next = in_stream.peek(); if (next == -1) { throw new LexerException(start_line, start_col, "Unexpected end of input stream when parsing delimited string literal"); } if (next != 'q') { throw new LexerException(start_line, start_col, "Not a delimited string literal"); } in_stream.read(); next = in_stream.peek(); if (next != '"') { throw new LexerException(start_line, start_col, "Not a delimited string literal"); } in_stream.read(); next = in_stream.peek(); boolean is_identifier = false; boolean is_nesting = false; int nests = 0; StringBuilder delimiter = new StringBuilder(); if (is_nesting_open(next)) { is_nesting = true; delimiter.append(Character.toChars(in_stream.read())); } else if (Character.isLetter(next) || next == '_') { is_identifier = true; delimiter.append(Character.toChars(in_stream.read())); next = in_stream.peek(); while (Character.isLetterOrDigit(next) || next == '_') { delimiter.append(Character.toChars(in_stream.read())); next = in_stream.peek(); } if (next != '\n') { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "String delimiter must be followed by newline"); } in_stream.read(); } else { delimiter.append(Character.toChars(in_stream.read())); } next = in_stream.peek(); String start_delim = delimiter.toString(); String end_delim; if (is_identifier) { end_delim = start_delim; } else { end_delim = String.valueOf((char)valid_close(start_delim.charAt(0))); } StringBuilder result = new StringBuilder(); while (next != -1) { if (next == '\n' && is_identifier) { result.append((char)in_stream.read()); next = in_stream.peek(); StringBuilder sb = new StringBuilder(); for (char c : end_delim.toCharArray()) { if (next == -1) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Unexpected end of stream in delimited string literal"); } else if (next != c) { break; } sb.append(Character.toChars(in_stream.read())); next = in_stream.peek(); } if (next == '"' && sb.toString().equals(end_delim)) { in_stream.read(); break; } result.append(sb); } else if (is_nesting && next == start_delim.charAt(0)) { nests++; } else if (next == end_delim.charAt(0)) { nests--; if (nests < 0) { if (in_stream.peek(2) == '"') { in_stream.read(); in_stream.read(); break; } else { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Improperly delimited string"); } } } result.append(Character.toChars(in_stream.read())); next = in_stream.peek(); } if (next == -1) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Unexpected end of stream in delimited string literal"); } return factory.create(TokenType.LiteralUtf8, start_index, start_line, start_col, in_stream.getPosition(), in_stream.getLine(), in_stream.getCol(), result.toString()); } LexDelimitedString(TokenFactory factory, LexerStream in_stream); Token read(); }
@Test(expected = LexerException.class) public void also_not() throws Exception { String str = "\"q{}\""; LexerStream ls = new LexerStream(new StringReader(str)); LexDelimitedString lex = new LexDelimitedString(new BaseTokenFactory(), ls); lex.read(); }
public Token read() throws IOException, LexerException { int start_index = in_stream.getPosition(); int start_line = in_stream.getLine(); int start_col = in_stream.getCol(); int next = in_stream.peek(); if (next == -1) { throw new LexerException(start_line, start_col, "Unexpected end of input stream when parsing delimited string literal"); } if (next != 'q') { throw new LexerException(start_line, start_col, "Not a delimited string literal"); } in_stream.read(); next = in_stream.peek(); if (next != '"') { throw new LexerException(start_line, start_col, "Not a delimited string literal"); } in_stream.read(); next = in_stream.peek(); boolean is_identifier = false; boolean is_nesting = false; int nests = 0; StringBuilder delimiter = new StringBuilder(); if (is_nesting_open(next)) { is_nesting = true; delimiter.append(Character.toChars(in_stream.read())); } else if (Character.isLetter(next) || next == '_') { is_identifier = true; delimiter.append(Character.toChars(in_stream.read())); next = in_stream.peek(); while (Character.isLetterOrDigit(next) || next == '_') { delimiter.append(Character.toChars(in_stream.read())); next = in_stream.peek(); } if (next != '\n') { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "String delimiter must be followed by newline"); } in_stream.read(); } else { delimiter.append(Character.toChars(in_stream.read())); } next = in_stream.peek(); String start_delim = delimiter.toString(); String end_delim; if (is_identifier) { end_delim = start_delim; } else { end_delim = String.valueOf((char)valid_close(start_delim.charAt(0))); } StringBuilder result = new StringBuilder(); while (next != -1) { if (next == '\n' && is_identifier) { result.append((char)in_stream.read()); next = in_stream.peek(); StringBuilder sb = new StringBuilder(); for (char c : end_delim.toCharArray()) { if (next == -1) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Unexpected end of stream in delimited string literal"); } else if (next != c) { break; } sb.append(Character.toChars(in_stream.read())); next = in_stream.peek(); } if (next == '"' && sb.toString().equals(end_delim)) { in_stream.read(); break; } result.append(sb); } else if (is_nesting && next == start_delim.charAt(0)) { nests++; } else if (next == end_delim.charAt(0)) { nests--; if (nests < 0) { if (in_stream.peek(2) == '"') { in_stream.read(); in_stream.read(); break; } else { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Improperly delimited string"); } } } result.append(Character.toChars(in_stream.read())); next = in_stream.peek(); } if (next == -1) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Unexpected end of stream in delimited string literal"); } return factory.create(TokenType.LiteralUtf8, start_index, start_line, start_col, in_stream.getPosition(), in_stream.getLine(), in_stream.getCol(), result.toString()); }
LexDelimitedString { public Token read() throws IOException, LexerException { int start_index = in_stream.getPosition(); int start_line = in_stream.getLine(); int start_col = in_stream.getCol(); int next = in_stream.peek(); if (next == -1) { throw new LexerException(start_line, start_col, "Unexpected end of input stream when parsing delimited string literal"); } if (next != 'q') { throw new LexerException(start_line, start_col, "Not a delimited string literal"); } in_stream.read(); next = in_stream.peek(); if (next != '"') { throw new LexerException(start_line, start_col, "Not a delimited string literal"); } in_stream.read(); next = in_stream.peek(); boolean is_identifier = false; boolean is_nesting = false; int nests = 0; StringBuilder delimiter = new StringBuilder(); if (is_nesting_open(next)) { is_nesting = true; delimiter.append(Character.toChars(in_stream.read())); } else if (Character.isLetter(next) || next == '_') { is_identifier = true; delimiter.append(Character.toChars(in_stream.read())); next = in_stream.peek(); while (Character.isLetterOrDigit(next) || next == '_') { delimiter.append(Character.toChars(in_stream.read())); next = in_stream.peek(); } if (next != '\n') { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "String delimiter must be followed by newline"); } in_stream.read(); } else { delimiter.append(Character.toChars(in_stream.read())); } next = in_stream.peek(); String start_delim = delimiter.toString(); String end_delim; if (is_identifier) { end_delim = start_delim; } else { end_delim = String.valueOf((char)valid_close(start_delim.charAt(0))); } StringBuilder result = new StringBuilder(); while (next != -1) { if (next == '\n' && is_identifier) { result.append((char)in_stream.read()); next = in_stream.peek(); StringBuilder sb = new StringBuilder(); for (char c : end_delim.toCharArray()) { if (next == -1) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Unexpected end of stream in delimited string literal"); } else if (next != c) { break; } sb.append(Character.toChars(in_stream.read())); next = in_stream.peek(); } if (next == '"' && sb.toString().equals(end_delim)) { in_stream.read(); break; } result.append(sb); } else if (is_nesting && next == start_delim.charAt(0)) { nests++; } else if (next == end_delim.charAt(0)) { nests--; if (nests < 0) { if (in_stream.peek(2) == '"') { in_stream.read(); in_stream.read(); break; } else { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Improperly delimited string"); } } } result.append(Character.toChars(in_stream.read())); next = in_stream.peek(); } if (next == -1) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Unexpected end of stream in delimited string literal"); } return factory.create(TokenType.LiteralUtf8, start_index, start_line, start_col, in_stream.getPosition(), in_stream.getLine(), in_stream.getCol(), result.toString()); } }
LexDelimitedString { public Token read() throws IOException, LexerException { int start_index = in_stream.getPosition(); int start_line = in_stream.getLine(); int start_col = in_stream.getCol(); int next = in_stream.peek(); if (next == -1) { throw new LexerException(start_line, start_col, "Unexpected end of input stream when parsing delimited string literal"); } if (next != 'q') { throw new LexerException(start_line, start_col, "Not a delimited string literal"); } in_stream.read(); next = in_stream.peek(); if (next != '"') { throw new LexerException(start_line, start_col, "Not a delimited string literal"); } in_stream.read(); next = in_stream.peek(); boolean is_identifier = false; boolean is_nesting = false; int nests = 0; StringBuilder delimiter = new StringBuilder(); if (is_nesting_open(next)) { is_nesting = true; delimiter.append(Character.toChars(in_stream.read())); } else if (Character.isLetter(next) || next == '_') { is_identifier = true; delimiter.append(Character.toChars(in_stream.read())); next = in_stream.peek(); while (Character.isLetterOrDigit(next) || next == '_') { delimiter.append(Character.toChars(in_stream.read())); next = in_stream.peek(); } if (next != '\n') { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "String delimiter must be followed by newline"); } in_stream.read(); } else { delimiter.append(Character.toChars(in_stream.read())); } next = in_stream.peek(); String start_delim = delimiter.toString(); String end_delim; if (is_identifier) { end_delim = start_delim; } else { end_delim = String.valueOf((char)valid_close(start_delim.charAt(0))); } StringBuilder result = new StringBuilder(); while (next != -1) { if (next == '\n' && is_identifier) { result.append((char)in_stream.read()); next = in_stream.peek(); StringBuilder sb = new StringBuilder(); for (char c : end_delim.toCharArray()) { if (next == -1) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Unexpected end of stream in delimited string literal"); } else if (next != c) { break; } sb.append(Character.toChars(in_stream.read())); next = in_stream.peek(); } if (next == '"' && sb.toString().equals(end_delim)) { in_stream.read(); break; } result.append(sb); } else if (is_nesting && next == start_delim.charAt(0)) { nests++; } else if (next == end_delim.charAt(0)) { nests--; if (nests < 0) { if (in_stream.peek(2) == '"') { in_stream.read(); in_stream.read(); break; } else { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Improperly delimited string"); } } } result.append(Character.toChars(in_stream.read())); next = in_stream.peek(); } if (next == -1) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Unexpected end of stream in delimited string literal"); } return factory.create(TokenType.LiteralUtf8, start_index, start_line, start_col, in_stream.getPosition(), in_stream.getLine(), in_stream.getCol(), result.toString()); } LexDelimitedString(TokenFactory factory, LexerStream in_stream); }
LexDelimitedString { public Token read() throws IOException, LexerException { int start_index = in_stream.getPosition(); int start_line = in_stream.getLine(); int start_col = in_stream.getCol(); int next = in_stream.peek(); if (next == -1) { throw new LexerException(start_line, start_col, "Unexpected end of input stream when parsing delimited string literal"); } if (next != 'q') { throw new LexerException(start_line, start_col, "Not a delimited string literal"); } in_stream.read(); next = in_stream.peek(); if (next != '"') { throw new LexerException(start_line, start_col, "Not a delimited string literal"); } in_stream.read(); next = in_stream.peek(); boolean is_identifier = false; boolean is_nesting = false; int nests = 0; StringBuilder delimiter = new StringBuilder(); if (is_nesting_open(next)) { is_nesting = true; delimiter.append(Character.toChars(in_stream.read())); } else if (Character.isLetter(next) || next == '_') { is_identifier = true; delimiter.append(Character.toChars(in_stream.read())); next = in_stream.peek(); while (Character.isLetterOrDigit(next) || next == '_') { delimiter.append(Character.toChars(in_stream.read())); next = in_stream.peek(); } if (next != '\n') { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "String delimiter must be followed by newline"); } in_stream.read(); } else { delimiter.append(Character.toChars(in_stream.read())); } next = in_stream.peek(); String start_delim = delimiter.toString(); String end_delim; if (is_identifier) { end_delim = start_delim; } else { end_delim = String.valueOf((char)valid_close(start_delim.charAt(0))); } StringBuilder result = new StringBuilder(); while (next != -1) { if (next == '\n' && is_identifier) { result.append((char)in_stream.read()); next = in_stream.peek(); StringBuilder sb = new StringBuilder(); for (char c : end_delim.toCharArray()) { if (next == -1) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Unexpected end of stream in delimited string literal"); } else if (next != c) { break; } sb.append(Character.toChars(in_stream.read())); next = in_stream.peek(); } if (next == '"' && sb.toString().equals(end_delim)) { in_stream.read(); break; } result.append(sb); } else if (is_nesting && next == start_delim.charAt(0)) { nests++; } else if (next == end_delim.charAt(0)) { nests--; if (nests < 0) { if (in_stream.peek(2) == '"') { in_stream.read(); in_stream.read(); break; } else { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Improperly delimited string"); } } } result.append(Character.toChars(in_stream.read())); next = in_stream.peek(); } if (next == -1) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Unexpected end of stream in delimited string literal"); } return factory.create(TokenType.LiteralUtf8, start_index, start_line, start_col, in_stream.getPosition(), in_stream.getLine(), in_stream.getCol(), result.toString()); } LexDelimitedString(TokenFactory factory, LexerStream in_stream); Token read(); }
LexDelimitedString { public Token read() throws IOException, LexerException { int start_index = in_stream.getPosition(); int start_line = in_stream.getLine(); int start_col = in_stream.getCol(); int next = in_stream.peek(); if (next == -1) { throw new LexerException(start_line, start_col, "Unexpected end of input stream when parsing delimited string literal"); } if (next != 'q') { throw new LexerException(start_line, start_col, "Not a delimited string literal"); } in_stream.read(); next = in_stream.peek(); if (next != '"') { throw new LexerException(start_line, start_col, "Not a delimited string literal"); } in_stream.read(); next = in_stream.peek(); boolean is_identifier = false; boolean is_nesting = false; int nests = 0; StringBuilder delimiter = new StringBuilder(); if (is_nesting_open(next)) { is_nesting = true; delimiter.append(Character.toChars(in_stream.read())); } else if (Character.isLetter(next) || next == '_') { is_identifier = true; delimiter.append(Character.toChars(in_stream.read())); next = in_stream.peek(); while (Character.isLetterOrDigit(next) || next == '_') { delimiter.append(Character.toChars(in_stream.read())); next = in_stream.peek(); } if (next != '\n') { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "String delimiter must be followed by newline"); } in_stream.read(); } else { delimiter.append(Character.toChars(in_stream.read())); } next = in_stream.peek(); String start_delim = delimiter.toString(); String end_delim; if (is_identifier) { end_delim = start_delim; } else { end_delim = String.valueOf((char)valid_close(start_delim.charAt(0))); } StringBuilder result = new StringBuilder(); while (next != -1) { if (next == '\n' && is_identifier) { result.append((char)in_stream.read()); next = in_stream.peek(); StringBuilder sb = new StringBuilder(); for (char c : end_delim.toCharArray()) { if (next == -1) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Unexpected end of stream in delimited string literal"); } else if (next != c) { break; } sb.append(Character.toChars(in_stream.read())); next = in_stream.peek(); } if (next == '"' && sb.toString().equals(end_delim)) { in_stream.read(); break; } result.append(sb); } else if (is_nesting && next == start_delim.charAt(0)) { nests++; } else if (next == end_delim.charAt(0)) { nests--; if (nests < 0) { if (in_stream.peek(2) == '"') { in_stream.read(); in_stream.read(); break; } else { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Improperly delimited string"); } } } result.append(Character.toChars(in_stream.read())); next = in_stream.peek(); } if (next == -1) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Unexpected end of stream in delimited string literal"); } return factory.create(TokenType.LiteralUtf8, start_index, start_line, start_col, in_stream.getPosition(), in_stream.getLine(), in_stream.getCol(), result.toString()); } LexDelimitedString(TokenFactory factory, LexerStream in_stream); Token read(); }
@Test(expected = LexerException.class) public void unterminated() throws Exception { String str = "q\"{lalala}"; LexerStream ls = new LexerStream(new StringReader(str)); LexDelimitedString lex = new LexDelimitedString(new BaseTokenFactory(), ls); lex.read(); }
public Token read() throws IOException, LexerException { int start_index = in_stream.getPosition(); int start_line = in_stream.getLine(); int start_col = in_stream.getCol(); int next = in_stream.peek(); if (next == -1) { throw new LexerException(start_line, start_col, "Unexpected end of input stream when parsing delimited string literal"); } if (next != 'q') { throw new LexerException(start_line, start_col, "Not a delimited string literal"); } in_stream.read(); next = in_stream.peek(); if (next != '"') { throw new LexerException(start_line, start_col, "Not a delimited string literal"); } in_stream.read(); next = in_stream.peek(); boolean is_identifier = false; boolean is_nesting = false; int nests = 0; StringBuilder delimiter = new StringBuilder(); if (is_nesting_open(next)) { is_nesting = true; delimiter.append(Character.toChars(in_stream.read())); } else if (Character.isLetter(next) || next == '_') { is_identifier = true; delimiter.append(Character.toChars(in_stream.read())); next = in_stream.peek(); while (Character.isLetterOrDigit(next) || next == '_') { delimiter.append(Character.toChars(in_stream.read())); next = in_stream.peek(); } if (next != '\n') { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "String delimiter must be followed by newline"); } in_stream.read(); } else { delimiter.append(Character.toChars(in_stream.read())); } next = in_stream.peek(); String start_delim = delimiter.toString(); String end_delim; if (is_identifier) { end_delim = start_delim; } else { end_delim = String.valueOf((char)valid_close(start_delim.charAt(0))); } StringBuilder result = new StringBuilder(); while (next != -1) { if (next == '\n' && is_identifier) { result.append((char)in_stream.read()); next = in_stream.peek(); StringBuilder sb = new StringBuilder(); for (char c : end_delim.toCharArray()) { if (next == -1) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Unexpected end of stream in delimited string literal"); } else if (next != c) { break; } sb.append(Character.toChars(in_stream.read())); next = in_stream.peek(); } if (next == '"' && sb.toString().equals(end_delim)) { in_stream.read(); break; } result.append(sb); } else if (is_nesting && next == start_delim.charAt(0)) { nests++; } else if (next == end_delim.charAt(0)) { nests--; if (nests < 0) { if (in_stream.peek(2) == '"') { in_stream.read(); in_stream.read(); break; } else { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Improperly delimited string"); } } } result.append(Character.toChars(in_stream.read())); next = in_stream.peek(); } if (next == -1) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Unexpected end of stream in delimited string literal"); } return factory.create(TokenType.LiteralUtf8, start_index, start_line, start_col, in_stream.getPosition(), in_stream.getLine(), in_stream.getCol(), result.toString()); }
LexDelimitedString { public Token read() throws IOException, LexerException { int start_index = in_stream.getPosition(); int start_line = in_stream.getLine(); int start_col = in_stream.getCol(); int next = in_stream.peek(); if (next == -1) { throw new LexerException(start_line, start_col, "Unexpected end of input stream when parsing delimited string literal"); } if (next != 'q') { throw new LexerException(start_line, start_col, "Not a delimited string literal"); } in_stream.read(); next = in_stream.peek(); if (next != '"') { throw new LexerException(start_line, start_col, "Not a delimited string literal"); } in_stream.read(); next = in_stream.peek(); boolean is_identifier = false; boolean is_nesting = false; int nests = 0; StringBuilder delimiter = new StringBuilder(); if (is_nesting_open(next)) { is_nesting = true; delimiter.append(Character.toChars(in_stream.read())); } else if (Character.isLetter(next) || next == '_') { is_identifier = true; delimiter.append(Character.toChars(in_stream.read())); next = in_stream.peek(); while (Character.isLetterOrDigit(next) || next == '_') { delimiter.append(Character.toChars(in_stream.read())); next = in_stream.peek(); } if (next != '\n') { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "String delimiter must be followed by newline"); } in_stream.read(); } else { delimiter.append(Character.toChars(in_stream.read())); } next = in_stream.peek(); String start_delim = delimiter.toString(); String end_delim; if (is_identifier) { end_delim = start_delim; } else { end_delim = String.valueOf((char)valid_close(start_delim.charAt(0))); } StringBuilder result = new StringBuilder(); while (next != -1) { if (next == '\n' && is_identifier) { result.append((char)in_stream.read()); next = in_stream.peek(); StringBuilder sb = new StringBuilder(); for (char c : end_delim.toCharArray()) { if (next == -1) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Unexpected end of stream in delimited string literal"); } else if (next != c) { break; } sb.append(Character.toChars(in_stream.read())); next = in_stream.peek(); } if (next == '"' && sb.toString().equals(end_delim)) { in_stream.read(); break; } result.append(sb); } else if (is_nesting && next == start_delim.charAt(0)) { nests++; } else if (next == end_delim.charAt(0)) { nests--; if (nests < 0) { if (in_stream.peek(2) == '"') { in_stream.read(); in_stream.read(); break; } else { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Improperly delimited string"); } } } result.append(Character.toChars(in_stream.read())); next = in_stream.peek(); } if (next == -1) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Unexpected end of stream in delimited string literal"); } return factory.create(TokenType.LiteralUtf8, start_index, start_line, start_col, in_stream.getPosition(), in_stream.getLine(), in_stream.getCol(), result.toString()); } }
LexDelimitedString { public Token read() throws IOException, LexerException { int start_index = in_stream.getPosition(); int start_line = in_stream.getLine(); int start_col = in_stream.getCol(); int next = in_stream.peek(); if (next == -1) { throw new LexerException(start_line, start_col, "Unexpected end of input stream when parsing delimited string literal"); } if (next != 'q') { throw new LexerException(start_line, start_col, "Not a delimited string literal"); } in_stream.read(); next = in_stream.peek(); if (next != '"') { throw new LexerException(start_line, start_col, "Not a delimited string literal"); } in_stream.read(); next = in_stream.peek(); boolean is_identifier = false; boolean is_nesting = false; int nests = 0; StringBuilder delimiter = new StringBuilder(); if (is_nesting_open(next)) { is_nesting = true; delimiter.append(Character.toChars(in_stream.read())); } else if (Character.isLetter(next) || next == '_') { is_identifier = true; delimiter.append(Character.toChars(in_stream.read())); next = in_stream.peek(); while (Character.isLetterOrDigit(next) || next == '_') { delimiter.append(Character.toChars(in_stream.read())); next = in_stream.peek(); } if (next != '\n') { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "String delimiter must be followed by newline"); } in_stream.read(); } else { delimiter.append(Character.toChars(in_stream.read())); } next = in_stream.peek(); String start_delim = delimiter.toString(); String end_delim; if (is_identifier) { end_delim = start_delim; } else { end_delim = String.valueOf((char)valid_close(start_delim.charAt(0))); } StringBuilder result = new StringBuilder(); while (next != -1) { if (next == '\n' && is_identifier) { result.append((char)in_stream.read()); next = in_stream.peek(); StringBuilder sb = new StringBuilder(); for (char c : end_delim.toCharArray()) { if (next == -1) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Unexpected end of stream in delimited string literal"); } else if (next != c) { break; } sb.append(Character.toChars(in_stream.read())); next = in_stream.peek(); } if (next == '"' && sb.toString().equals(end_delim)) { in_stream.read(); break; } result.append(sb); } else if (is_nesting && next == start_delim.charAt(0)) { nests++; } else if (next == end_delim.charAt(0)) { nests--; if (nests < 0) { if (in_stream.peek(2) == '"') { in_stream.read(); in_stream.read(); break; } else { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Improperly delimited string"); } } } result.append(Character.toChars(in_stream.read())); next = in_stream.peek(); } if (next == -1) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Unexpected end of stream in delimited string literal"); } return factory.create(TokenType.LiteralUtf8, start_index, start_line, start_col, in_stream.getPosition(), in_stream.getLine(), in_stream.getCol(), result.toString()); } LexDelimitedString(TokenFactory factory, LexerStream in_stream); }
LexDelimitedString { public Token read() throws IOException, LexerException { int start_index = in_stream.getPosition(); int start_line = in_stream.getLine(); int start_col = in_stream.getCol(); int next = in_stream.peek(); if (next == -1) { throw new LexerException(start_line, start_col, "Unexpected end of input stream when parsing delimited string literal"); } if (next != 'q') { throw new LexerException(start_line, start_col, "Not a delimited string literal"); } in_stream.read(); next = in_stream.peek(); if (next != '"') { throw new LexerException(start_line, start_col, "Not a delimited string literal"); } in_stream.read(); next = in_stream.peek(); boolean is_identifier = false; boolean is_nesting = false; int nests = 0; StringBuilder delimiter = new StringBuilder(); if (is_nesting_open(next)) { is_nesting = true; delimiter.append(Character.toChars(in_stream.read())); } else if (Character.isLetter(next) || next == '_') { is_identifier = true; delimiter.append(Character.toChars(in_stream.read())); next = in_stream.peek(); while (Character.isLetterOrDigit(next) || next == '_') { delimiter.append(Character.toChars(in_stream.read())); next = in_stream.peek(); } if (next != '\n') { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "String delimiter must be followed by newline"); } in_stream.read(); } else { delimiter.append(Character.toChars(in_stream.read())); } next = in_stream.peek(); String start_delim = delimiter.toString(); String end_delim; if (is_identifier) { end_delim = start_delim; } else { end_delim = String.valueOf((char)valid_close(start_delim.charAt(0))); } StringBuilder result = new StringBuilder(); while (next != -1) { if (next == '\n' && is_identifier) { result.append((char)in_stream.read()); next = in_stream.peek(); StringBuilder sb = new StringBuilder(); for (char c : end_delim.toCharArray()) { if (next == -1) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Unexpected end of stream in delimited string literal"); } else if (next != c) { break; } sb.append(Character.toChars(in_stream.read())); next = in_stream.peek(); } if (next == '"' && sb.toString().equals(end_delim)) { in_stream.read(); break; } result.append(sb); } else if (is_nesting && next == start_delim.charAt(0)) { nests++; } else if (next == end_delim.charAt(0)) { nests--; if (nests < 0) { if (in_stream.peek(2) == '"') { in_stream.read(); in_stream.read(); break; } else { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Improperly delimited string"); } } } result.append(Character.toChars(in_stream.read())); next = in_stream.peek(); } if (next == -1) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Unexpected end of stream in delimited string literal"); } return factory.create(TokenType.LiteralUtf8, start_index, start_line, start_col, in_stream.getPosition(), in_stream.getLine(), in_stream.getCol(), result.toString()); } LexDelimitedString(TokenFactory factory, LexerStream in_stream); Token read(); }
LexDelimitedString { public Token read() throws IOException, LexerException { int start_index = in_stream.getPosition(); int start_line = in_stream.getLine(); int start_col = in_stream.getCol(); int next = in_stream.peek(); if (next == -1) { throw new LexerException(start_line, start_col, "Unexpected end of input stream when parsing delimited string literal"); } if (next != 'q') { throw new LexerException(start_line, start_col, "Not a delimited string literal"); } in_stream.read(); next = in_stream.peek(); if (next != '"') { throw new LexerException(start_line, start_col, "Not a delimited string literal"); } in_stream.read(); next = in_stream.peek(); boolean is_identifier = false; boolean is_nesting = false; int nests = 0; StringBuilder delimiter = new StringBuilder(); if (is_nesting_open(next)) { is_nesting = true; delimiter.append(Character.toChars(in_stream.read())); } else if (Character.isLetter(next) || next == '_') { is_identifier = true; delimiter.append(Character.toChars(in_stream.read())); next = in_stream.peek(); while (Character.isLetterOrDigit(next) || next == '_') { delimiter.append(Character.toChars(in_stream.read())); next = in_stream.peek(); } if (next != '\n') { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "String delimiter must be followed by newline"); } in_stream.read(); } else { delimiter.append(Character.toChars(in_stream.read())); } next = in_stream.peek(); String start_delim = delimiter.toString(); String end_delim; if (is_identifier) { end_delim = start_delim; } else { end_delim = String.valueOf((char)valid_close(start_delim.charAt(0))); } StringBuilder result = new StringBuilder(); while (next != -1) { if (next == '\n' && is_identifier) { result.append((char)in_stream.read()); next = in_stream.peek(); StringBuilder sb = new StringBuilder(); for (char c : end_delim.toCharArray()) { if (next == -1) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Unexpected end of stream in delimited string literal"); } else if (next != c) { break; } sb.append(Character.toChars(in_stream.read())); next = in_stream.peek(); } if (next == '"' && sb.toString().equals(end_delim)) { in_stream.read(); break; } result.append(sb); } else if (is_nesting && next == start_delim.charAt(0)) { nests++; } else if (next == end_delim.charAt(0)) { nests--; if (nests < 0) { if (in_stream.peek(2) == '"') { in_stream.read(); in_stream.read(); break; } else { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Improperly delimited string"); } } } result.append(Character.toChars(in_stream.read())); next = in_stream.peek(); } if (next == -1) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Unexpected end of stream in delimited string literal"); } return factory.create(TokenType.LiteralUtf8, start_index, start_line, start_col, in_stream.getPosition(), in_stream.getLine(), in_stream.getCol(), result.toString()); } LexDelimitedString(TokenFactory factory, LexerStream in_stream); Token read(); }
@Test(expected = LexerException.class) public void partial_delimiter() throws Exception { String str = "q\"/abc/def/\""; LexerStream ls = new LexerStream(new StringReader(str)); LexDelimitedString lex = new LexDelimitedString(new BaseTokenFactory(), ls); lex.read(); }
public Token read() throws IOException, LexerException { int start_index = in_stream.getPosition(); int start_line = in_stream.getLine(); int start_col = in_stream.getCol(); int next = in_stream.peek(); if (next == -1) { throw new LexerException(start_line, start_col, "Unexpected end of input stream when parsing delimited string literal"); } if (next != 'q') { throw new LexerException(start_line, start_col, "Not a delimited string literal"); } in_stream.read(); next = in_stream.peek(); if (next != '"') { throw new LexerException(start_line, start_col, "Not a delimited string literal"); } in_stream.read(); next = in_stream.peek(); boolean is_identifier = false; boolean is_nesting = false; int nests = 0; StringBuilder delimiter = new StringBuilder(); if (is_nesting_open(next)) { is_nesting = true; delimiter.append(Character.toChars(in_stream.read())); } else if (Character.isLetter(next) || next == '_') { is_identifier = true; delimiter.append(Character.toChars(in_stream.read())); next = in_stream.peek(); while (Character.isLetterOrDigit(next) || next == '_') { delimiter.append(Character.toChars(in_stream.read())); next = in_stream.peek(); } if (next != '\n') { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "String delimiter must be followed by newline"); } in_stream.read(); } else { delimiter.append(Character.toChars(in_stream.read())); } next = in_stream.peek(); String start_delim = delimiter.toString(); String end_delim; if (is_identifier) { end_delim = start_delim; } else { end_delim = String.valueOf((char)valid_close(start_delim.charAt(0))); } StringBuilder result = new StringBuilder(); while (next != -1) { if (next == '\n' && is_identifier) { result.append((char)in_stream.read()); next = in_stream.peek(); StringBuilder sb = new StringBuilder(); for (char c : end_delim.toCharArray()) { if (next == -1) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Unexpected end of stream in delimited string literal"); } else if (next != c) { break; } sb.append(Character.toChars(in_stream.read())); next = in_stream.peek(); } if (next == '"' && sb.toString().equals(end_delim)) { in_stream.read(); break; } result.append(sb); } else if (is_nesting && next == start_delim.charAt(0)) { nests++; } else if (next == end_delim.charAt(0)) { nests--; if (nests < 0) { if (in_stream.peek(2) == '"') { in_stream.read(); in_stream.read(); break; } else { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Improperly delimited string"); } } } result.append(Character.toChars(in_stream.read())); next = in_stream.peek(); } if (next == -1) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Unexpected end of stream in delimited string literal"); } return factory.create(TokenType.LiteralUtf8, start_index, start_line, start_col, in_stream.getPosition(), in_stream.getLine(), in_stream.getCol(), result.toString()); }
LexDelimitedString { public Token read() throws IOException, LexerException { int start_index = in_stream.getPosition(); int start_line = in_stream.getLine(); int start_col = in_stream.getCol(); int next = in_stream.peek(); if (next == -1) { throw new LexerException(start_line, start_col, "Unexpected end of input stream when parsing delimited string literal"); } if (next != 'q') { throw new LexerException(start_line, start_col, "Not a delimited string literal"); } in_stream.read(); next = in_stream.peek(); if (next != '"') { throw new LexerException(start_line, start_col, "Not a delimited string literal"); } in_stream.read(); next = in_stream.peek(); boolean is_identifier = false; boolean is_nesting = false; int nests = 0; StringBuilder delimiter = new StringBuilder(); if (is_nesting_open(next)) { is_nesting = true; delimiter.append(Character.toChars(in_stream.read())); } else if (Character.isLetter(next) || next == '_') { is_identifier = true; delimiter.append(Character.toChars(in_stream.read())); next = in_stream.peek(); while (Character.isLetterOrDigit(next) || next == '_') { delimiter.append(Character.toChars(in_stream.read())); next = in_stream.peek(); } if (next != '\n') { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "String delimiter must be followed by newline"); } in_stream.read(); } else { delimiter.append(Character.toChars(in_stream.read())); } next = in_stream.peek(); String start_delim = delimiter.toString(); String end_delim; if (is_identifier) { end_delim = start_delim; } else { end_delim = String.valueOf((char)valid_close(start_delim.charAt(0))); } StringBuilder result = new StringBuilder(); while (next != -1) { if (next == '\n' && is_identifier) { result.append((char)in_stream.read()); next = in_stream.peek(); StringBuilder sb = new StringBuilder(); for (char c : end_delim.toCharArray()) { if (next == -1) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Unexpected end of stream in delimited string literal"); } else if (next != c) { break; } sb.append(Character.toChars(in_stream.read())); next = in_stream.peek(); } if (next == '"' && sb.toString().equals(end_delim)) { in_stream.read(); break; } result.append(sb); } else if (is_nesting && next == start_delim.charAt(0)) { nests++; } else if (next == end_delim.charAt(0)) { nests--; if (nests < 0) { if (in_stream.peek(2) == '"') { in_stream.read(); in_stream.read(); break; } else { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Improperly delimited string"); } } } result.append(Character.toChars(in_stream.read())); next = in_stream.peek(); } if (next == -1) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Unexpected end of stream in delimited string literal"); } return factory.create(TokenType.LiteralUtf8, start_index, start_line, start_col, in_stream.getPosition(), in_stream.getLine(), in_stream.getCol(), result.toString()); } }
LexDelimitedString { public Token read() throws IOException, LexerException { int start_index = in_stream.getPosition(); int start_line = in_stream.getLine(); int start_col = in_stream.getCol(); int next = in_stream.peek(); if (next == -1) { throw new LexerException(start_line, start_col, "Unexpected end of input stream when parsing delimited string literal"); } if (next != 'q') { throw new LexerException(start_line, start_col, "Not a delimited string literal"); } in_stream.read(); next = in_stream.peek(); if (next != '"') { throw new LexerException(start_line, start_col, "Not a delimited string literal"); } in_stream.read(); next = in_stream.peek(); boolean is_identifier = false; boolean is_nesting = false; int nests = 0; StringBuilder delimiter = new StringBuilder(); if (is_nesting_open(next)) { is_nesting = true; delimiter.append(Character.toChars(in_stream.read())); } else if (Character.isLetter(next) || next == '_') { is_identifier = true; delimiter.append(Character.toChars(in_stream.read())); next = in_stream.peek(); while (Character.isLetterOrDigit(next) || next == '_') { delimiter.append(Character.toChars(in_stream.read())); next = in_stream.peek(); } if (next != '\n') { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "String delimiter must be followed by newline"); } in_stream.read(); } else { delimiter.append(Character.toChars(in_stream.read())); } next = in_stream.peek(); String start_delim = delimiter.toString(); String end_delim; if (is_identifier) { end_delim = start_delim; } else { end_delim = String.valueOf((char)valid_close(start_delim.charAt(0))); } StringBuilder result = new StringBuilder(); while (next != -1) { if (next == '\n' && is_identifier) { result.append((char)in_stream.read()); next = in_stream.peek(); StringBuilder sb = new StringBuilder(); for (char c : end_delim.toCharArray()) { if (next == -1) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Unexpected end of stream in delimited string literal"); } else if (next != c) { break; } sb.append(Character.toChars(in_stream.read())); next = in_stream.peek(); } if (next == '"' && sb.toString().equals(end_delim)) { in_stream.read(); break; } result.append(sb); } else if (is_nesting && next == start_delim.charAt(0)) { nests++; } else if (next == end_delim.charAt(0)) { nests--; if (nests < 0) { if (in_stream.peek(2) == '"') { in_stream.read(); in_stream.read(); break; } else { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Improperly delimited string"); } } } result.append(Character.toChars(in_stream.read())); next = in_stream.peek(); } if (next == -1) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Unexpected end of stream in delimited string literal"); } return factory.create(TokenType.LiteralUtf8, start_index, start_line, start_col, in_stream.getPosition(), in_stream.getLine(), in_stream.getCol(), result.toString()); } LexDelimitedString(TokenFactory factory, LexerStream in_stream); }
LexDelimitedString { public Token read() throws IOException, LexerException { int start_index = in_stream.getPosition(); int start_line = in_stream.getLine(); int start_col = in_stream.getCol(); int next = in_stream.peek(); if (next == -1) { throw new LexerException(start_line, start_col, "Unexpected end of input stream when parsing delimited string literal"); } if (next != 'q') { throw new LexerException(start_line, start_col, "Not a delimited string literal"); } in_stream.read(); next = in_stream.peek(); if (next != '"') { throw new LexerException(start_line, start_col, "Not a delimited string literal"); } in_stream.read(); next = in_stream.peek(); boolean is_identifier = false; boolean is_nesting = false; int nests = 0; StringBuilder delimiter = new StringBuilder(); if (is_nesting_open(next)) { is_nesting = true; delimiter.append(Character.toChars(in_stream.read())); } else if (Character.isLetter(next) || next == '_') { is_identifier = true; delimiter.append(Character.toChars(in_stream.read())); next = in_stream.peek(); while (Character.isLetterOrDigit(next) || next == '_') { delimiter.append(Character.toChars(in_stream.read())); next = in_stream.peek(); } if (next != '\n') { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "String delimiter must be followed by newline"); } in_stream.read(); } else { delimiter.append(Character.toChars(in_stream.read())); } next = in_stream.peek(); String start_delim = delimiter.toString(); String end_delim; if (is_identifier) { end_delim = start_delim; } else { end_delim = String.valueOf((char)valid_close(start_delim.charAt(0))); } StringBuilder result = new StringBuilder(); while (next != -1) { if (next == '\n' && is_identifier) { result.append((char)in_stream.read()); next = in_stream.peek(); StringBuilder sb = new StringBuilder(); for (char c : end_delim.toCharArray()) { if (next == -1) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Unexpected end of stream in delimited string literal"); } else if (next != c) { break; } sb.append(Character.toChars(in_stream.read())); next = in_stream.peek(); } if (next == '"' && sb.toString().equals(end_delim)) { in_stream.read(); break; } result.append(sb); } else if (is_nesting && next == start_delim.charAt(0)) { nests++; } else if (next == end_delim.charAt(0)) { nests--; if (nests < 0) { if (in_stream.peek(2) == '"') { in_stream.read(); in_stream.read(); break; } else { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Improperly delimited string"); } } } result.append(Character.toChars(in_stream.read())); next = in_stream.peek(); } if (next == -1) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Unexpected end of stream in delimited string literal"); } return factory.create(TokenType.LiteralUtf8, start_index, start_line, start_col, in_stream.getPosition(), in_stream.getLine(), in_stream.getCol(), result.toString()); } LexDelimitedString(TokenFactory factory, LexerStream in_stream); Token read(); }
LexDelimitedString { public Token read() throws IOException, LexerException { int start_index = in_stream.getPosition(); int start_line = in_stream.getLine(); int start_col = in_stream.getCol(); int next = in_stream.peek(); if (next == -1) { throw new LexerException(start_line, start_col, "Unexpected end of input stream when parsing delimited string literal"); } if (next != 'q') { throw new LexerException(start_line, start_col, "Not a delimited string literal"); } in_stream.read(); next = in_stream.peek(); if (next != '"') { throw new LexerException(start_line, start_col, "Not a delimited string literal"); } in_stream.read(); next = in_stream.peek(); boolean is_identifier = false; boolean is_nesting = false; int nests = 0; StringBuilder delimiter = new StringBuilder(); if (is_nesting_open(next)) { is_nesting = true; delimiter.append(Character.toChars(in_stream.read())); } else if (Character.isLetter(next) || next == '_') { is_identifier = true; delimiter.append(Character.toChars(in_stream.read())); next = in_stream.peek(); while (Character.isLetterOrDigit(next) || next == '_') { delimiter.append(Character.toChars(in_stream.read())); next = in_stream.peek(); } if (next != '\n') { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "String delimiter must be followed by newline"); } in_stream.read(); } else { delimiter.append(Character.toChars(in_stream.read())); } next = in_stream.peek(); String start_delim = delimiter.toString(); String end_delim; if (is_identifier) { end_delim = start_delim; } else { end_delim = String.valueOf((char)valid_close(start_delim.charAt(0))); } StringBuilder result = new StringBuilder(); while (next != -1) { if (next == '\n' && is_identifier) { result.append((char)in_stream.read()); next = in_stream.peek(); StringBuilder sb = new StringBuilder(); for (char c : end_delim.toCharArray()) { if (next == -1) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Unexpected end of stream in delimited string literal"); } else if (next != c) { break; } sb.append(Character.toChars(in_stream.read())); next = in_stream.peek(); } if (next == '"' && sb.toString().equals(end_delim)) { in_stream.read(); break; } result.append(sb); } else if (is_nesting && next == start_delim.charAt(0)) { nests++; } else if (next == end_delim.charAt(0)) { nests--; if (nests < 0) { if (in_stream.peek(2) == '"') { in_stream.read(); in_stream.read(); break; } else { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Improperly delimited string"); } } } result.append(Character.toChars(in_stream.read())); next = in_stream.peek(); } if (next == -1) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Unexpected end of stream in delimited string literal"); } return factory.create(TokenType.LiteralUtf8, start_index, start_line, start_col, in_stream.getPosition(), in_stream.getLine(), in_stream.getCol(), result.toString()); } LexDelimitedString(TokenFactory factory, LexerStream in_stream); Token read(); }
@Test public void readLine() throws Exception { Reader in = new StringReader("line1\nline2\n3line\n\nline5"); LexerStream ls = new LexerStream(in); assertEquals(1, ls.getLine()); assertEquals(1, ls.getCol()); assertEquals('l', (char)ls.read()); assertEquals(1, ls.getLine()); assertEquals(2, ls.getCol()); assertEquals("ine1", ls.readLine()); assertEquals(2, ls.getLine()); assertEquals(1, ls.getCol()); assertEquals("line2", ls.readLine()); assertEquals(3, ls.getLine()); assertEquals(1, ls.getCol()); assertEquals('3', (char)ls.read()); assertEquals(3, ls.getLine()); assertEquals(2, ls.getCol()); assertEquals("line", ls.readLine()); assertEquals(4, ls.getLine()); assertEquals(1, ls.getCol()); assertEquals("", ls.readLine()); assertEquals(5, ls.getLine()); assertEquals(1, ls.getCol()); assertEquals("line5", ls.readLine()); assertEquals(6, ls.getLine()); assertEquals(1, ls.getCol()); assertNull(ls.readLine()); assertEquals(-1, ls.read()); }
@Override public String readLine() throws IOException { String ln = super.readLine(); if (ln != null) { index += ln.length(); } line = super.getLineNumber(); col = 1; return ln; }
LexerStream extends LineNumberReader { @Override public String readLine() throws IOException { String ln = super.readLine(); if (ln != null) { index += ln.length(); } line = super.getLineNumber(); col = 1; return ln; } }
LexerStream extends LineNumberReader { @Override public String readLine() throws IOException { String ln = super.readLine(); if (ln != null) { index += ln.length(); } line = super.getLineNumber(); col = 1; return ln; } LexerStream(Reader in_stream); }
LexerStream extends LineNumberReader { @Override public String readLine() throws IOException { String ln = super.readLine(); if (ln != null) { index += ln.length(); } line = super.getLineNumber(); col = 1; return ln; } LexerStream(Reader in_stream); int getLine(); int getCol(); int getPosition(); @Override int read(); @Override String readLine(); int peek(); int peek(int ahead); @Override int read(char[] cbuf, int off, int len); @Override long skip(long n); @Override void mark(int readAheadLimit); @Override void reset(); }
LexerStream extends LineNumberReader { @Override public String readLine() throws IOException { String ln = super.readLine(); if (ln != null) { index += ln.length(); } line = super.getLineNumber(); col = 1; return ln; } LexerStream(Reader in_stream); int getLine(); int getCol(); int getPosition(); @Override int read(); @Override String readLine(); int peek(); int peek(int ahead); @Override int read(char[] cbuf, int off, int len); @Override long skip(long n); @Override void mark(int readAheadLimit); @Override void reset(); }
@Test(expected = LexerException.class) public void unnested() throws Exception { String str = "q\"{foo][}f}\"bar"; LexerStream ls = new LexerStream(new StringReader(str)); LexDelimitedString lex = new LexDelimitedString(new BaseTokenFactory(), ls); lex.read(); }
public Token read() throws IOException, LexerException { int start_index = in_stream.getPosition(); int start_line = in_stream.getLine(); int start_col = in_stream.getCol(); int next = in_stream.peek(); if (next == -1) { throw new LexerException(start_line, start_col, "Unexpected end of input stream when parsing delimited string literal"); } if (next != 'q') { throw new LexerException(start_line, start_col, "Not a delimited string literal"); } in_stream.read(); next = in_stream.peek(); if (next != '"') { throw new LexerException(start_line, start_col, "Not a delimited string literal"); } in_stream.read(); next = in_stream.peek(); boolean is_identifier = false; boolean is_nesting = false; int nests = 0; StringBuilder delimiter = new StringBuilder(); if (is_nesting_open(next)) { is_nesting = true; delimiter.append(Character.toChars(in_stream.read())); } else if (Character.isLetter(next) || next == '_') { is_identifier = true; delimiter.append(Character.toChars(in_stream.read())); next = in_stream.peek(); while (Character.isLetterOrDigit(next) || next == '_') { delimiter.append(Character.toChars(in_stream.read())); next = in_stream.peek(); } if (next != '\n') { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "String delimiter must be followed by newline"); } in_stream.read(); } else { delimiter.append(Character.toChars(in_stream.read())); } next = in_stream.peek(); String start_delim = delimiter.toString(); String end_delim; if (is_identifier) { end_delim = start_delim; } else { end_delim = String.valueOf((char)valid_close(start_delim.charAt(0))); } StringBuilder result = new StringBuilder(); while (next != -1) { if (next == '\n' && is_identifier) { result.append((char)in_stream.read()); next = in_stream.peek(); StringBuilder sb = new StringBuilder(); for (char c : end_delim.toCharArray()) { if (next == -1) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Unexpected end of stream in delimited string literal"); } else if (next != c) { break; } sb.append(Character.toChars(in_stream.read())); next = in_stream.peek(); } if (next == '"' && sb.toString().equals(end_delim)) { in_stream.read(); break; } result.append(sb); } else if (is_nesting && next == start_delim.charAt(0)) { nests++; } else if (next == end_delim.charAt(0)) { nests--; if (nests < 0) { if (in_stream.peek(2) == '"') { in_stream.read(); in_stream.read(); break; } else { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Improperly delimited string"); } } } result.append(Character.toChars(in_stream.read())); next = in_stream.peek(); } if (next == -1) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Unexpected end of stream in delimited string literal"); } return factory.create(TokenType.LiteralUtf8, start_index, start_line, start_col, in_stream.getPosition(), in_stream.getLine(), in_stream.getCol(), result.toString()); }
LexDelimitedString { public Token read() throws IOException, LexerException { int start_index = in_stream.getPosition(); int start_line = in_stream.getLine(); int start_col = in_stream.getCol(); int next = in_stream.peek(); if (next == -1) { throw new LexerException(start_line, start_col, "Unexpected end of input stream when parsing delimited string literal"); } if (next != 'q') { throw new LexerException(start_line, start_col, "Not a delimited string literal"); } in_stream.read(); next = in_stream.peek(); if (next != '"') { throw new LexerException(start_line, start_col, "Not a delimited string literal"); } in_stream.read(); next = in_stream.peek(); boolean is_identifier = false; boolean is_nesting = false; int nests = 0; StringBuilder delimiter = new StringBuilder(); if (is_nesting_open(next)) { is_nesting = true; delimiter.append(Character.toChars(in_stream.read())); } else if (Character.isLetter(next) || next == '_') { is_identifier = true; delimiter.append(Character.toChars(in_stream.read())); next = in_stream.peek(); while (Character.isLetterOrDigit(next) || next == '_') { delimiter.append(Character.toChars(in_stream.read())); next = in_stream.peek(); } if (next != '\n') { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "String delimiter must be followed by newline"); } in_stream.read(); } else { delimiter.append(Character.toChars(in_stream.read())); } next = in_stream.peek(); String start_delim = delimiter.toString(); String end_delim; if (is_identifier) { end_delim = start_delim; } else { end_delim = String.valueOf((char)valid_close(start_delim.charAt(0))); } StringBuilder result = new StringBuilder(); while (next != -1) { if (next == '\n' && is_identifier) { result.append((char)in_stream.read()); next = in_stream.peek(); StringBuilder sb = new StringBuilder(); for (char c : end_delim.toCharArray()) { if (next == -1) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Unexpected end of stream in delimited string literal"); } else if (next != c) { break; } sb.append(Character.toChars(in_stream.read())); next = in_stream.peek(); } if (next == '"' && sb.toString().equals(end_delim)) { in_stream.read(); break; } result.append(sb); } else if (is_nesting && next == start_delim.charAt(0)) { nests++; } else if (next == end_delim.charAt(0)) { nests--; if (nests < 0) { if (in_stream.peek(2) == '"') { in_stream.read(); in_stream.read(); break; } else { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Improperly delimited string"); } } } result.append(Character.toChars(in_stream.read())); next = in_stream.peek(); } if (next == -1) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Unexpected end of stream in delimited string literal"); } return factory.create(TokenType.LiteralUtf8, start_index, start_line, start_col, in_stream.getPosition(), in_stream.getLine(), in_stream.getCol(), result.toString()); } }
LexDelimitedString { public Token read() throws IOException, LexerException { int start_index = in_stream.getPosition(); int start_line = in_stream.getLine(); int start_col = in_stream.getCol(); int next = in_stream.peek(); if (next == -1) { throw new LexerException(start_line, start_col, "Unexpected end of input stream when parsing delimited string literal"); } if (next != 'q') { throw new LexerException(start_line, start_col, "Not a delimited string literal"); } in_stream.read(); next = in_stream.peek(); if (next != '"') { throw new LexerException(start_line, start_col, "Not a delimited string literal"); } in_stream.read(); next = in_stream.peek(); boolean is_identifier = false; boolean is_nesting = false; int nests = 0; StringBuilder delimiter = new StringBuilder(); if (is_nesting_open(next)) { is_nesting = true; delimiter.append(Character.toChars(in_stream.read())); } else if (Character.isLetter(next) || next == '_') { is_identifier = true; delimiter.append(Character.toChars(in_stream.read())); next = in_stream.peek(); while (Character.isLetterOrDigit(next) || next == '_') { delimiter.append(Character.toChars(in_stream.read())); next = in_stream.peek(); } if (next != '\n') { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "String delimiter must be followed by newline"); } in_stream.read(); } else { delimiter.append(Character.toChars(in_stream.read())); } next = in_stream.peek(); String start_delim = delimiter.toString(); String end_delim; if (is_identifier) { end_delim = start_delim; } else { end_delim = String.valueOf((char)valid_close(start_delim.charAt(0))); } StringBuilder result = new StringBuilder(); while (next != -1) { if (next == '\n' && is_identifier) { result.append((char)in_stream.read()); next = in_stream.peek(); StringBuilder sb = new StringBuilder(); for (char c : end_delim.toCharArray()) { if (next == -1) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Unexpected end of stream in delimited string literal"); } else if (next != c) { break; } sb.append(Character.toChars(in_stream.read())); next = in_stream.peek(); } if (next == '"' && sb.toString().equals(end_delim)) { in_stream.read(); break; } result.append(sb); } else if (is_nesting && next == start_delim.charAt(0)) { nests++; } else if (next == end_delim.charAt(0)) { nests--; if (nests < 0) { if (in_stream.peek(2) == '"') { in_stream.read(); in_stream.read(); break; } else { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Improperly delimited string"); } } } result.append(Character.toChars(in_stream.read())); next = in_stream.peek(); } if (next == -1) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Unexpected end of stream in delimited string literal"); } return factory.create(TokenType.LiteralUtf8, start_index, start_line, start_col, in_stream.getPosition(), in_stream.getLine(), in_stream.getCol(), result.toString()); } LexDelimitedString(TokenFactory factory, LexerStream in_stream); }
LexDelimitedString { public Token read() throws IOException, LexerException { int start_index = in_stream.getPosition(); int start_line = in_stream.getLine(); int start_col = in_stream.getCol(); int next = in_stream.peek(); if (next == -1) { throw new LexerException(start_line, start_col, "Unexpected end of input stream when parsing delimited string literal"); } if (next != 'q') { throw new LexerException(start_line, start_col, "Not a delimited string literal"); } in_stream.read(); next = in_stream.peek(); if (next != '"') { throw new LexerException(start_line, start_col, "Not a delimited string literal"); } in_stream.read(); next = in_stream.peek(); boolean is_identifier = false; boolean is_nesting = false; int nests = 0; StringBuilder delimiter = new StringBuilder(); if (is_nesting_open(next)) { is_nesting = true; delimiter.append(Character.toChars(in_stream.read())); } else if (Character.isLetter(next) || next == '_') { is_identifier = true; delimiter.append(Character.toChars(in_stream.read())); next = in_stream.peek(); while (Character.isLetterOrDigit(next) || next == '_') { delimiter.append(Character.toChars(in_stream.read())); next = in_stream.peek(); } if (next != '\n') { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "String delimiter must be followed by newline"); } in_stream.read(); } else { delimiter.append(Character.toChars(in_stream.read())); } next = in_stream.peek(); String start_delim = delimiter.toString(); String end_delim; if (is_identifier) { end_delim = start_delim; } else { end_delim = String.valueOf((char)valid_close(start_delim.charAt(0))); } StringBuilder result = new StringBuilder(); while (next != -1) { if (next == '\n' && is_identifier) { result.append((char)in_stream.read()); next = in_stream.peek(); StringBuilder sb = new StringBuilder(); for (char c : end_delim.toCharArray()) { if (next == -1) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Unexpected end of stream in delimited string literal"); } else if (next != c) { break; } sb.append(Character.toChars(in_stream.read())); next = in_stream.peek(); } if (next == '"' && sb.toString().equals(end_delim)) { in_stream.read(); break; } result.append(sb); } else if (is_nesting && next == start_delim.charAt(0)) { nests++; } else if (next == end_delim.charAt(0)) { nests--; if (nests < 0) { if (in_stream.peek(2) == '"') { in_stream.read(); in_stream.read(); break; } else { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Improperly delimited string"); } } } result.append(Character.toChars(in_stream.read())); next = in_stream.peek(); } if (next == -1) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Unexpected end of stream in delimited string literal"); } return factory.create(TokenType.LiteralUtf8, start_index, start_line, start_col, in_stream.getPosition(), in_stream.getLine(), in_stream.getCol(), result.toString()); } LexDelimitedString(TokenFactory factory, LexerStream in_stream); Token read(); }
LexDelimitedString { public Token read() throws IOException, LexerException { int start_index = in_stream.getPosition(); int start_line = in_stream.getLine(); int start_col = in_stream.getCol(); int next = in_stream.peek(); if (next == -1) { throw new LexerException(start_line, start_col, "Unexpected end of input stream when parsing delimited string literal"); } if (next != 'q') { throw new LexerException(start_line, start_col, "Not a delimited string literal"); } in_stream.read(); next = in_stream.peek(); if (next != '"') { throw new LexerException(start_line, start_col, "Not a delimited string literal"); } in_stream.read(); next = in_stream.peek(); boolean is_identifier = false; boolean is_nesting = false; int nests = 0; StringBuilder delimiter = new StringBuilder(); if (is_nesting_open(next)) { is_nesting = true; delimiter.append(Character.toChars(in_stream.read())); } else if (Character.isLetter(next) || next == '_') { is_identifier = true; delimiter.append(Character.toChars(in_stream.read())); next = in_stream.peek(); while (Character.isLetterOrDigit(next) || next == '_') { delimiter.append(Character.toChars(in_stream.read())); next = in_stream.peek(); } if (next != '\n') { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "String delimiter must be followed by newline"); } in_stream.read(); } else { delimiter.append(Character.toChars(in_stream.read())); } next = in_stream.peek(); String start_delim = delimiter.toString(); String end_delim; if (is_identifier) { end_delim = start_delim; } else { end_delim = String.valueOf((char)valid_close(start_delim.charAt(0))); } StringBuilder result = new StringBuilder(); while (next != -1) { if (next == '\n' && is_identifier) { result.append((char)in_stream.read()); next = in_stream.peek(); StringBuilder sb = new StringBuilder(); for (char c : end_delim.toCharArray()) { if (next == -1) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Unexpected end of stream in delimited string literal"); } else if (next != c) { break; } sb.append(Character.toChars(in_stream.read())); next = in_stream.peek(); } if (next == '"' && sb.toString().equals(end_delim)) { in_stream.read(); break; } result.append(sb); } else if (is_nesting && next == start_delim.charAt(0)) { nests++; } else if (next == end_delim.charAt(0)) { nests--; if (nests < 0) { if (in_stream.peek(2) == '"') { in_stream.read(); in_stream.read(); break; } else { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Improperly delimited string"); } } } result.append(Character.toChars(in_stream.read())); next = in_stream.peek(); } if (next == -1) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Unexpected end of stream in delimited string literal"); } return factory.create(TokenType.LiteralUtf8, start_index, start_line, start_col, in_stream.getPosition(), in_stream.getLine(), in_stream.getCol(), result.toString()); } LexDelimitedString(TokenFactory factory, LexerStream in_stream); Token read(); }
@Test public void simple() throws Exception { String str = "x\"0A\""; StringBuilder sb = new StringBuilder(); sb.append(Character.toChars(0x0a)); String expected = sb.toString(); LexerStream ls = new LexerStream(new StringReader(str)); LexHexLiteral lex = new LexHexLiteral(new BaseTokenFactory(), ls); Token tok = lex.read(); Assert.assertEquals(TokenType.LiteralHex, tok.getType()); assertTrue(tok.getValue() instanceof String); Assert.assertEquals(expected, tok.getValue()); Assert.assertEquals(1, tok.getLine()); Assert.assertEquals(1, tok.getCol()); Assert.assertEquals(1, tok.getEndLine()); Assert.assertEquals(6, tok.getEndCol()); }
public Token read() throws IOException, LexerException { int start_index = in_stream.getPosition(); int start_line = in_stream.getLine(); int start_col = in_stream.getCol(); int next = in_stream.peek(); if (next == -1) { throw new LexerException(start_line, start_col, "Unexpected end of input stream when parsing string literal"); } if (next != 'x') { throw new LexerException(start_line, start_col, "Not a literal hex string"); } in_stream.read(); next = in_stream.peek(); if (next != '"') { throw new LexerException(start_line, start_col, "Not a literal hex string"); } in_stream.read(); next = in_stream.peek(); StringBuilder sb = new StringBuilder(); boolean pair = false; while (next != -1 && is_valid_hex(next)) { if (!Character.isWhitespace(next)) { sb.append(Character.toChars(next)); if (pair) { sb.append(' '); } pair = !pair; } in_stream.read(); next = in_stream.peek(); } if (next != '"') { throw new LexerException(start_line, start_col, "Unterminated literal hex string"); } in_stream.read(); StringBuilder result = new StringBuilder(); String raw = sb.toString(); for (String bite : raw.split(" ")) { result.append(Character.toChars(Integer.parseInt(bite, 16))); } return factory.create(TokenType.LiteralHex, start_index, start_line, start_col, in_stream.getPosition(), in_stream.getLine(), in_stream.getCol(), result.toString()); }
LexHexLiteral { public Token read() throws IOException, LexerException { int start_index = in_stream.getPosition(); int start_line = in_stream.getLine(); int start_col = in_stream.getCol(); int next = in_stream.peek(); if (next == -1) { throw new LexerException(start_line, start_col, "Unexpected end of input stream when parsing string literal"); } if (next != 'x') { throw new LexerException(start_line, start_col, "Not a literal hex string"); } in_stream.read(); next = in_stream.peek(); if (next != '"') { throw new LexerException(start_line, start_col, "Not a literal hex string"); } in_stream.read(); next = in_stream.peek(); StringBuilder sb = new StringBuilder(); boolean pair = false; while (next != -1 && is_valid_hex(next)) { if (!Character.isWhitespace(next)) { sb.append(Character.toChars(next)); if (pair) { sb.append(' '); } pair = !pair; } in_stream.read(); next = in_stream.peek(); } if (next != '"') { throw new LexerException(start_line, start_col, "Unterminated literal hex string"); } in_stream.read(); StringBuilder result = new StringBuilder(); String raw = sb.toString(); for (String bite : raw.split(" ")) { result.append(Character.toChars(Integer.parseInt(bite, 16))); } return factory.create(TokenType.LiteralHex, start_index, start_line, start_col, in_stream.getPosition(), in_stream.getLine(), in_stream.getCol(), result.toString()); } }
LexHexLiteral { public Token read() throws IOException, LexerException { int start_index = in_stream.getPosition(); int start_line = in_stream.getLine(); int start_col = in_stream.getCol(); int next = in_stream.peek(); if (next == -1) { throw new LexerException(start_line, start_col, "Unexpected end of input stream when parsing string literal"); } if (next != 'x') { throw new LexerException(start_line, start_col, "Not a literal hex string"); } in_stream.read(); next = in_stream.peek(); if (next != '"') { throw new LexerException(start_line, start_col, "Not a literal hex string"); } in_stream.read(); next = in_stream.peek(); StringBuilder sb = new StringBuilder(); boolean pair = false; while (next != -1 && is_valid_hex(next)) { if (!Character.isWhitespace(next)) { sb.append(Character.toChars(next)); if (pair) { sb.append(' '); } pair = !pair; } in_stream.read(); next = in_stream.peek(); } if (next != '"') { throw new LexerException(start_line, start_col, "Unterminated literal hex string"); } in_stream.read(); StringBuilder result = new StringBuilder(); String raw = sb.toString(); for (String bite : raw.split(" ")) { result.append(Character.toChars(Integer.parseInt(bite, 16))); } return factory.create(TokenType.LiteralHex, start_index, start_line, start_col, in_stream.getPosition(), in_stream.getLine(), in_stream.getCol(), result.toString()); } LexHexLiteral(TokenFactory factory, LexerStream in_stream); }
LexHexLiteral { public Token read() throws IOException, LexerException { int start_index = in_stream.getPosition(); int start_line = in_stream.getLine(); int start_col = in_stream.getCol(); int next = in_stream.peek(); if (next == -1) { throw new LexerException(start_line, start_col, "Unexpected end of input stream when parsing string literal"); } if (next != 'x') { throw new LexerException(start_line, start_col, "Not a literal hex string"); } in_stream.read(); next = in_stream.peek(); if (next != '"') { throw new LexerException(start_line, start_col, "Not a literal hex string"); } in_stream.read(); next = in_stream.peek(); StringBuilder sb = new StringBuilder(); boolean pair = false; while (next != -1 && is_valid_hex(next)) { if (!Character.isWhitespace(next)) { sb.append(Character.toChars(next)); if (pair) { sb.append(' '); } pair = !pair; } in_stream.read(); next = in_stream.peek(); } if (next != '"') { throw new LexerException(start_line, start_col, "Unterminated literal hex string"); } in_stream.read(); StringBuilder result = new StringBuilder(); String raw = sb.toString(); for (String bite : raw.split(" ")) { result.append(Character.toChars(Integer.parseInt(bite, 16))); } return factory.create(TokenType.LiteralHex, start_index, start_line, start_col, in_stream.getPosition(), in_stream.getLine(), in_stream.getCol(), result.toString()); } LexHexLiteral(TokenFactory factory, LexerStream in_stream); Token read(); }
LexHexLiteral { public Token read() throws IOException, LexerException { int start_index = in_stream.getPosition(); int start_line = in_stream.getLine(); int start_col = in_stream.getCol(); int next = in_stream.peek(); if (next == -1) { throw new LexerException(start_line, start_col, "Unexpected end of input stream when parsing string literal"); } if (next != 'x') { throw new LexerException(start_line, start_col, "Not a literal hex string"); } in_stream.read(); next = in_stream.peek(); if (next != '"') { throw new LexerException(start_line, start_col, "Not a literal hex string"); } in_stream.read(); next = in_stream.peek(); StringBuilder sb = new StringBuilder(); boolean pair = false; while (next != -1 && is_valid_hex(next)) { if (!Character.isWhitespace(next)) { sb.append(Character.toChars(next)); if (pair) { sb.append(' '); } pair = !pair; } in_stream.read(); next = in_stream.peek(); } if (next != '"') { throw new LexerException(start_line, start_col, "Unterminated literal hex string"); } in_stream.read(); StringBuilder result = new StringBuilder(); String raw = sb.toString(); for (String bite : raw.split(" ")) { result.append(Character.toChars(Integer.parseInt(bite, 16))); } return factory.create(TokenType.LiteralHex, start_index, start_line, start_col, in_stream.getPosition(), in_stream.getLine(), in_stream.getCol(), result.toString()); } LexHexLiteral(TokenFactory factory, LexerStream in_stream); Token read(); }
@Test public void spaces() throws Exception { String str = "x\"00 FbcD 3 2FD 0A \""; StringBuilder sb = new StringBuilder(); sb.append(Character.toChars(0x00)); sb.append(Character.toChars(0xfb)); sb.append(Character.toChars(0xcd)); sb.append(Character.toChars(0x32)); sb.append(Character.toChars(0xfd)); sb.append(Character.toChars(0x0a)); String expected = sb.toString(); LexerStream ls = new LexerStream(new StringReader(str)); LexHexLiteral lex = new LexHexLiteral(new BaseTokenFactory(), ls); Token tok = lex.read(); Assert.assertEquals(TokenType.LiteralHex, tok.getType()); assertTrue(tok.getValue() instanceof String); Assert.assertEquals(expected, tok.getValue()); Assert.assertEquals(1, tok.getLine()); Assert.assertEquals(1, tok.getCol()); Assert.assertEquals(1, tok.getEndLine()); Assert.assertEquals(23, tok.getEndCol()); }
public Token read() throws IOException, LexerException { int start_index = in_stream.getPosition(); int start_line = in_stream.getLine(); int start_col = in_stream.getCol(); int next = in_stream.peek(); if (next == -1) { throw new LexerException(start_line, start_col, "Unexpected end of input stream when parsing string literal"); } if (next != 'x') { throw new LexerException(start_line, start_col, "Not a literal hex string"); } in_stream.read(); next = in_stream.peek(); if (next != '"') { throw new LexerException(start_line, start_col, "Not a literal hex string"); } in_stream.read(); next = in_stream.peek(); StringBuilder sb = new StringBuilder(); boolean pair = false; while (next != -1 && is_valid_hex(next)) { if (!Character.isWhitespace(next)) { sb.append(Character.toChars(next)); if (pair) { sb.append(' '); } pair = !pair; } in_stream.read(); next = in_stream.peek(); } if (next != '"') { throw new LexerException(start_line, start_col, "Unterminated literal hex string"); } in_stream.read(); StringBuilder result = new StringBuilder(); String raw = sb.toString(); for (String bite : raw.split(" ")) { result.append(Character.toChars(Integer.parseInt(bite, 16))); } return factory.create(TokenType.LiteralHex, start_index, start_line, start_col, in_stream.getPosition(), in_stream.getLine(), in_stream.getCol(), result.toString()); }
LexHexLiteral { public Token read() throws IOException, LexerException { int start_index = in_stream.getPosition(); int start_line = in_stream.getLine(); int start_col = in_stream.getCol(); int next = in_stream.peek(); if (next == -1) { throw new LexerException(start_line, start_col, "Unexpected end of input stream when parsing string literal"); } if (next != 'x') { throw new LexerException(start_line, start_col, "Not a literal hex string"); } in_stream.read(); next = in_stream.peek(); if (next != '"') { throw new LexerException(start_line, start_col, "Not a literal hex string"); } in_stream.read(); next = in_stream.peek(); StringBuilder sb = new StringBuilder(); boolean pair = false; while (next != -1 && is_valid_hex(next)) { if (!Character.isWhitespace(next)) { sb.append(Character.toChars(next)); if (pair) { sb.append(' '); } pair = !pair; } in_stream.read(); next = in_stream.peek(); } if (next != '"') { throw new LexerException(start_line, start_col, "Unterminated literal hex string"); } in_stream.read(); StringBuilder result = new StringBuilder(); String raw = sb.toString(); for (String bite : raw.split(" ")) { result.append(Character.toChars(Integer.parseInt(bite, 16))); } return factory.create(TokenType.LiteralHex, start_index, start_line, start_col, in_stream.getPosition(), in_stream.getLine(), in_stream.getCol(), result.toString()); } }
LexHexLiteral { public Token read() throws IOException, LexerException { int start_index = in_stream.getPosition(); int start_line = in_stream.getLine(); int start_col = in_stream.getCol(); int next = in_stream.peek(); if (next == -1) { throw new LexerException(start_line, start_col, "Unexpected end of input stream when parsing string literal"); } if (next != 'x') { throw new LexerException(start_line, start_col, "Not a literal hex string"); } in_stream.read(); next = in_stream.peek(); if (next != '"') { throw new LexerException(start_line, start_col, "Not a literal hex string"); } in_stream.read(); next = in_stream.peek(); StringBuilder sb = new StringBuilder(); boolean pair = false; while (next != -1 && is_valid_hex(next)) { if (!Character.isWhitespace(next)) { sb.append(Character.toChars(next)); if (pair) { sb.append(' '); } pair = !pair; } in_stream.read(); next = in_stream.peek(); } if (next != '"') { throw new LexerException(start_line, start_col, "Unterminated literal hex string"); } in_stream.read(); StringBuilder result = new StringBuilder(); String raw = sb.toString(); for (String bite : raw.split(" ")) { result.append(Character.toChars(Integer.parseInt(bite, 16))); } return factory.create(TokenType.LiteralHex, start_index, start_line, start_col, in_stream.getPosition(), in_stream.getLine(), in_stream.getCol(), result.toString()); } LexHexLiteral(TokenFactory factory, LexerStream in_stream); }
LexHexLiteral { public Token read() throws IOException, LexerException { int start_index = in_stream.getPosition(); int start_line = in_stream.getLine(); int start_col = in_stream.getCol(); int next = in_stream.peek(); if (next == -1) { throw new LexerException(start_line, start_col, "Unexpected end of input stream when parsing string literal"); } if (next != 'x') { throw new LexerException(start_line, start_col, "Not a literal hex string"); } in_stream.read(); next = in_stream.peek(); if (next != '"') { throw new LexerException(start_line, start_col, "Not a literal hex string"); } in_stream.read(); next = in_stream.peek(); StringBuilder sb = new StringBuilder(); boolean pair = false; while (next != -1 && is_valid_hex(next)) { if (!Character.isWhitespace(next)) { sb.append(Character.toChars(next)); if (pair) { sb.append(' '); } pair = !pair; } in_stream.read(); next = in_stream.peek(); } if (next != '"') { throw new LexerException(start_line, start_col, "Unterminated literal hex string"); } in_stream.read(); StringBuilder result = new StringBuilder(); String raw = sb.toString(); for (String bite : raw.split(" ")) { result.append(Character.toChars(Integer.parseInt(bite, 16))); } return factory.create(TokenType.LiteralHex, start_index, start_line, start_col, in_stream.getPosition(), in_stream.getLine(), in_stream.getCol(), result.toString()); } LexHexLiteral(TokenFactory factory, LexerStream in_stream); Token read(); }
LexHexLiteral { public Token read() throws IOException, LexerException { int start_index = in_stream.getPosition(); int start_line = in_stream.getLine(); int start_col = in_stream.getCol(); int next = in_stream.peek(); if (next == -1) { throw new LexerException(start_line, start_col, "Unexpected end of input stream when parsing string literal"); } if (next != 'x') { throw new LexerException(start_line, start_col, "Not a literal hex string"); } in_stream.read(); next = in_stream.peek(); if (next != '"') { throw new LexerException(start_line, start_col, "Not a literal hex string"); } in_stream.read(); next = in_stream.peek(); StringBuilder sb = new StringBuilder(); boolean pair = false; while (next != -1 && is_valid_hex(next)) { if (!Character.isWhitespace(next)) { sb.append(Character.toChars(next)); if (pair) { sb.append(' '); } pair = !pair; } in_stream.read(); next = in_stream.peek(); } if (next != '"') { throw new LexerException(start_line, start_col, "Unterminated literal hex string"); } in_stream.read(); StringBuilder result = new StringBuilder(); String raw = sb.toString(); for (String bite : raw.split(" ")) { result.append(Character.toChars(Integer.parseInt(bite, 16))); } return factory.create(TokenType.LiteralHex, start_index, start_line, start_col, in_stream.getPosition(), in_stream.getLine(), in_stream.getCol(), result.toString()); } LexHexLiteral(TokenFactory factory, LexerStream in_stream); Token read(); }
@Test(expected = LexerException.class) public void empty() throws Exception { String str = ""; LexerStream ls = new LexerStream(new StringReader(str)); LexHexLiteral lex = new LexHexLiteral(new BaseTokenFactory(), ls); lex.read(); }
public Token read() throws IOException, LexerException { int start_index = in_stream.getPosition(); int start_line = in_stream.getLine(); int start_col = in_stream.getCol(); int next = in_stream.peek(); if (next == -1) { throw new LexerException(start_line, start_col, "Unexpected end of input stream when parsing string literal"); } if (next != 'x') { throw new LexerException(start_line, start_col, "Not a literal hex string"); } in_stream.read(); next = in_stream.peek(); if (next != '"') { throw new LexerException(start_line, start_col, "Not a literal hex string"); } in_stream.read(); next = in_stream.peek(); StringBuilder sb = new StringBuilder(); boolean pair = false; while (next != -1 && is_valid_hex(next)) { if (!Character.isWhitespace(next)) { sb.append(Character.toChars(next)); if (pair) { sb.append(' '); } pair = !pair; } in_stream.read(); next = in_stream.peek(); } if (next != '"') { throw new LexerException(start_line, start_col, "Unterminated literal hex string"); } in_stream.read(); StringBuilder result = new StringBuilder(); String raw = sb.toString(); for (String bite : raw.split(" ")) { result.append(Character.toChars(Integer.parseInt(bite, 16))); } return factory.create(TokenType.LiteralHex, start_index, start_line, start_col, in_stream.getPosition(), in_stream.getLine(), in_stream.getCol(), result.toString()); }
LexHexLiteral { public Token read() throws IOException, LexerException { int start_index = in_stream.getPosition(); int start_line = in_stream.getLine(); int start_col = in_stream.getCol(); int next = in_stream.peek(); if (next == -1) { throw new LexerException(start_line, start_col, "Unexpected end of input stream when parsing string literal"); } if (next != 'x') { throw new LexerException(start_line, start_col, "Not a literal hex string"); } in_stream.read(); next = in_stream.peek(); if (next != '"') { throw new LexerException(start_line, start_col, "Not a literal hex string"); } in_stream.read(); next = in_stream.peek(); StringBuilder sb = new StringBuilder(); boolean pair = false; while (next != -1 && is_valid_hex(next)) { if (!Character.isWhitespace(next)) { sb.append(Character.toChars(next)); if (pair) { sb.append(' '); } pair = !pair; } in_stream.read(); next = in_stream.peek(); } if (next != '"') { throw new LexerException(start_line, start_col, "Unterminated literal hex string"); } in_stream.read(); StringBuilder result = new StringBuilder(); String raw = sb.toString(); for (String bite : raw.split(" ")) { result.append(Character.toChars(Integer.parseInt(bite, 16))); } return factory.create(TokenType.LiteralHex, start_index, start_line, start_col, in_stream.getPosition(), in_stream.getLine(), in_stream.getCol(), result.toString()); } }
LexHexLiteral { public Token read() throws IOException, LexerException { int start_index = in_stream.getPosition(); int start_line = in_stream.getLine(); int start_col = in_stream.getCol(); int next = in_stream.peek(); if (next == -1) { throw new LexerException(start_line, start_col, "Unexpected end of input stream when parsing string literal"); } if (next != 'x') { throw new LexerException(start_line, start_col, "Not a literal hex string"); } in_stream.read(); next = in_stream.peek(); if (next != '"') { throw new LexerException(start_line, start_col, "Not a literal hex string"); } in_stream.read(); next = in_stream.peek(); StringBuilder sb = new StringBuilder(); boolean pair = false; while (next != -1 && is_valid_hex(next)) { if (!Character.isWhitespace(next)) { sb.append(Character.toChars(next)); if (pair) { sb.append(' '); } pair = !pair; } in_stream.read(); next = in_stream.peek(); } if (next != '"') { throw new LexerException(start_line, start_col, "Unterminated literal hex string"); } in_stream.read(); StringBuilder result = new StringBuilder(); String raw = sb.toString(); for (String bite : raw.split(" ")) { result.append(Character.toChars(Integer.parseInt(bite, 16))); } return factory.create(TokenType.LiteralHex, start_index, start_line, start_col, in_stream.getPosition(), in_stream.getLine(), in_stream.getCol(), result.toString()); } LexHexLiteral(TokenFactory factory, LexerStream in_stream); }
LexHexLiteral { public Token read() throws IOException, LexerException { int start_index = in_stream.getPosition(); int start_line = in_stream.getLine(); int start_col = in_stream.getCol(); int next = in_stream.peek(); if (next == -1) { throw new LexerException(start_line, start_col, "Unexpected end of input stream when parsing string literal"); } if (next != 'x') { throw new LexerException(start_line, start_col, "Not a literal hex string"); } in_stream.read(); next = in_stream.peek(); if (next != '"') { throw new LexerException(start_line, start_col, "Not a literal hex string"); } in_stream.read(); next = in_stream.peek(); StringBuilder sb = new StringBuilder(); boolean pair = false; while (next != -1 && is_valid_hex(next)) { if (!Character.isWhitespace(next)) { sb.append(Character.toChars(next)); if (pair) { sb.append(' '); } pair = !pair; } in_stream.read(); next = in_stream.peek(); } if (next != '"') { throw new LexerException(start_line, start_col, "Unterminated literal hex string"); } in_stream.read(); StringBuilder result = new StringBuilder(); String raw = sb.toString(); for (String bite : raw.split(" ")) { result.append(Character.toChars(Integer.parseInt(bite, 16))); } return factory.create(TokenType.LiteralHex, start_index, start_line, start_col, in_stream.getPosition(), in_stream.getLine(), in_stream.getCol(), result.toString()); } LexHexLiteral(TokenFactory factory, LexerStream in_stream); Token read(); }
LexHexLiteral { public Token read() throws IOException, LexerException { int start_index = in_stream.getPosition(); int start_line = in_stream.getLine(); int start_col = in_stream.getCol(); int next = in_stream.peek(); if (next == -1) { throw new LexerException(start_line, start_col, "Unexpected end of input stream when parsing string literal"); } if (next != 'x') { throw new LexerException(start_line, start_col, "Not a literal hex string"); } in_stream.read(); next = in_stream.peek(); if (next != '"') { throw new LexerException(start_line, start_col, "Not a literal hex string"); } in_stream.read(); next = in_stream.peek(); StringBuilder sb = new StringBuilder(); boolean pair = false; while (next != -1 && is_valid_hex(next)) { if (!Character.isWhitespace(next)) { sb.append(Character.toChars(next)); if (pair) { sb.append(' '); } pair = !pair; } in_stream.read(); next = in_stream.peek(); } if (next != '"') { throw new LexerException(start_line, start_col, "Unterminated literal hex string"); } in_stream.read(); StringBuilder result = new StringBuilder(); String raw = sb.toString(); for (String bite : raw.split(" ")) { result.append(Character.toChars(Integer.parseInt(bite, 16))); } return factory.create(TokenType.LiteralHex, start_index, start_line, start_col, in_stream.getPosition(), in_stream.getLine(), in_stream.getCol(), result.toString()); } LexHexLiteral(TokenFactory factory, LexerStream in_stream); Token read(); }
@Test(expected = LexerException.class) public void not() throws Exception { String str = "hex string"; LexerStream ls = new LexerStream(new StringReader(str)); LexHexLiteral lex = new LexHexLiteral(new BaseTokenFactory(), ls); lex.read(); }
public Token read() throws IOException, LexerException { int start_index = in_stream.getPosition(); int start_line = in_stream.getLine(); int start_col = in_stream.getCol(); int next = in_stream.peek(); if (next == -1) { throw new LexerException(start_line, start_col, "Unexpected end of input stream when parsing string literal"); } if (next != 'x') { throw new LexerException(start_line, start_col, "Not a literal hex string"); } in_stream.read(); next = in_stream.peek(); if (next != '"') { throw new LexerException(start_line, start_col, "Not a literal hex string"); } in_stream.read(); next = in_stream.peek(); StringBuilder sb = new StringBuilder(); boolean pair = false; while (next != -1 && is_valid_hex(next)) { if (!Character.isWhitespace(next)) { sb.append(Character.toChars(next)); if (pair) { sb.append(' '); } pair = !pair; } in_stream.read(); next = in_stream.peek(); } if (next != '"') { throw new LexerException(start_line, start_col, "Unterminated literal hex string"); } in_stream.read(); StringBuilder result = new StringBuilder(); String raw = sb.toString(); for (String bite : raw.split(" ")) { result.append(Character.toChars(Integer.parseInt(bite, 16))); } return factory.create(TokenType.LiteralHex, start_index, start_line, start_col, in_stream.getPosition(), in_stream.getLine(), in_stream.getCol(), result.toString()); }
LexHexLiteral { public Token read() throws IOException, LexerException { int start_index = in_stream.getPosition(); int start_line = in_stream.getLine(); int start_col = in_stream.getCol(); int next = in_stream.peek(); if (next == -1) { throw new LexerException(start_line, start_col, "Unexpected end of input stream when parsing string literal"); } if (next != 'x') { throw new LexerException(start_line, start_col, "Not a literal hex string"); } in_stream.read(); next = in_stream.peek(); if (next != '"') { throw new LexerException(start_line, start_col, "Not a literal hex string"); } in_stream.read(); next = in_stream.peek(); StringBuilder sb = new StringBuilder(); boolean pair = false; while (next != -1 && is_valid_hex(next)) { if (!Character.isWhitespace(next)) { sb.append(Character.toChars(next)); if (pair) { sb.append(' '); } pair = !pair; } in_stream.read(); next = in_stream.peek(); } if (next != '"') { throw new LexerException(start_line, start_col, "Unterminated literal hex string"); } in_stream.read(); StringBuilder result = new StringBuilder(); String raw = sb.toString(); for (String bite : raw.split(" ")) { result.append(Character.toChars(Integer.parseInt(bite, 16))); } return factory.create(TokenType.LiteralHex, start_index, start_line, start_col, in_stream.getPosition(), in_stream.getLine(), in_stream.getCol(), result.toString()); } }
LexHexLiteral { public Token read() throws IOException, LexerException { int start_index = in_stream.getPosition(); int start_line = in_stream.getLine(); int start_col = in_stream.getCol(); int next = in_stream.peek(); if (next == -1) { throw new LexerException(start_line, start_col, "Unexpected end of input stream when parsing string literal"); } if (next != 'x') { throw new LexerException(start_line, start_col, "Not a literal hex string"); } in_stream.read(); next = in_stream.peek(); if (next != '"') { throw new LexerException(start_line, start_col, "Not a literal hex string"); } in_stream.read(); next = in_stream.peek(); StringBuilder sb = new StringBuilder(); boolean pair = false; while (next != -1 && is_valid_hex(next)) { if (!Character.isWhitespace(next)) { sb.append(Character.toChars(next)); if (pair) { sb.append(' '); } pair = !pair; } in_stream.read(); next = in_stream.peek(); } if (next != '"') { throw new LexerException(start_line, start_col, "Unterminated literal hex string"); } in_stream.read(); StringBuilder result = new StringBuilder(); String raw = sb.toString(); for (String bite : raw.split(" ")) { result.append(Character.toChars(Integer.parseInt(bite, 16))); } return factory.create(TokenType.LiteralHex, start_index, start_line, start_col, in_stream.getPosition(), in_stream.getLine(), in_stream.getCol(), result.toString()); } LexHexLiteral(TokenFactory factory, LexerStream in_stream); }
LexHexLiteral { public Token read() throws IOException, LexerException { int start_index = in_stream.getPosition(); int start_line = in_stream.getLine(); int start_col = in_stream.getCol(); int next = in_stream.peek(); if (next == -1) { throw new LexerException(start_line, start_col, "Unexpected end of input stream when parsing string literal"); } if (next != 'x') { throw new LexerException(start_line, start_col, "Not a literal hex string"); } in_stream.read(); next = in_stream.peek(); if (next != '"') { throw new LexerException(start_line, start_col, "Not a literal hex string"); } in_stream.read(); next = in_stream.peek(); StringBuilder sb = new StringBuilder(); boolean pair = false; while (next != -1 && is_valid_hex(next)) { if (!Character.isWhitespace(next)) { sb.append(Character.toChars(next)); if (pair) { sb.append(' '); } pair = !pair; } in_stream.read(); next = in_stream.peek(); } if (next != '"') { throw new LexerException(start_line, start_col, "Unterminated literal hex string"); } in_stream.read(); StringBuilder result = new StringBuilder(); String raw = sb.toString(); for (String bite : raw.split(" ")) { result.append(Character.toChars(Integer.parseInt(bite, 16))); } return factory.create(TokenType.LiteralHex, start_index, start_line, start_col, in_stream.getPosition(), in_stream.getLine(), in_stream.getCol(), result.toString()); } LexHexLiteral(TokenFactory factory, LexerStream in_stream); Token read(); }
LexHexLiteral { public Token read() throws IOException, LexerException { int start_index = in_stream.getPosition(); int start_line = in_stream.getLine(); int start_col = in_stream.getCol(); int next = in_stream.peek(); if (next == -1) { throw new LexerException(start_line, start_col, "Unexpected end of input stream when parsing string literal"); } if (next != 'x') { throw new LexerException(start_line, start_col, "Not a literal hex string"); } in_stream.read(); next = in_stream.peek(); if (next != '"') { throw new LexerException(start_line, start_col, "Not a literal hex string"); } in_stream.read(); next = in_stream.peek(); StringBuilder sb = new StringBuilder(); boolean pair = false; while (next != -1 && is_valid_hex(next)) { if (!Character.isWhitespace(next)) { sb.append(Character.toChars(next)); if (pair) { sb.append(' '); } pair = !pair; } in_stream.read(); next = in_stream.peek(); } if (next != '"') { throw new LexerException(start_line, start_col, "Unterminated literal hex string"); } in_stream.read(); StringBuilder result = new StringBuilder(); String raw = sb.toString(); for (String bite : raw.split(" ")) { result.append(Character.toChars(Integer.parseInt(bite, 16))); } return factory.create(TokenType.LiteralHex, start_index, start_line, start_col, in_stream.getPosition(), in_stream.getLine(), in_stream.getCol(), result.toString()); } LexHexLiteral(TokenFactory factory, LexerStream in_stream); Token read(); }
@Test(expected = LexerException.class) public void also_not() throws Exception { String str = "x00AB"; LexerStream ls = new LexerStream(new StringReader(str)); LexHexLiteral lex = new LexHexLiteral(new BaseTokenFactory(), ls); lex.read(); }
public Token read() throws IOException, LexerException { int start_index = in_stream.getPosition(); int start_line = in_stream.getLine(); int start_col = in_stream.getCol(); int next = in_stream.peek(); if (next == -1) { throw new LexerException(start_line, start_col, "Unexpected end of input stream when parsing string literal"); } if (next != 'x') { throw new LexerException(start_line, start_col, "Not a literal hex string"); } in_stream.read(); next = in_stream.peek(); if (next != '"') { throw new LexerException(start_line, start_col, "Not a literal hex string"); } in_stream.read(); next = in_stream.peek(); StringBuilder sb = new StringBuilder(); boolean pair = false; while (next != -1 && is_valid_hex(next)) { if (!Character.isWhitespace(next)) { sb.append(Character.toChars(next)); if (pair) { sb.append(' '); } pair = !pair; } in_stream.read(); next = in_stream.peek(); } if (next != '"') { throw new LexerException(start_line, start_col, "Unterminated literal hex string"); } in_stream.read(); StringBuilder result = new StringBuilder(); String raw = sb.toString(); for (String bite : raw.split(" ")) { result.append(Character.toChars(Integer.parseInt(bite, 16))); } return factory.create(TokenType.LiteralHex, start_index, start_line, start_col, in_stream.getPosition(), in_stream.getLine(), in_stream.getCol(), result.toString()); }
LexHexLiteral { public Token read() throws IOException, LexerException { int start_index = in_stream.getPosition(); int start_line = in_stream.getLine(); int start_col = in_stream.getCol(); int next = in_stream.peek(); if (next == -1) { throw new LexerException(start_line, start_col, "Unexpected end of input stream when parsing string literal"); } if (next != 'x') { throw new LexerException(start_line, start_col, "Not a literal hex string"); } in_stream.read(); next = in_stream.peek(); if (next != '"') { throw new LexerException(start_line, start_col, "Not a literal hex string"); } in_stream.read(); next = in_stream.peek(); StringBuilder sb = new StringBuilder(); boolean pair = false; while (next != -1 && is_valid_hex(next)) { if (!Character.isWhitespace(next)) { sb.append(Character.toChars(next)); if (pair) { sb.append(' '); } pair = !pair; } in_stream.read(); next = in_stream.peek(); } if (next != '"') { throw new LexerException(start_line, start_col, "Unterminated literal hex string"); } in_stream.read(); StringBuilder result = new StringBuilder(); String raw = sb.toString(); for (String bite : raw.split(" ")) { result.append(Character.toChars(Integer.parseInt(bite, 16))); } return factory.create(TokenType.LiteralHex, start_index, start_line, start_col, in_stream.getPosition(), in_stream.getLine(), in_stream.getCol(), result.toString()); } }
LexHexLiteral { public Token read() throws IOException, LexerException { int start_index = in_stream.getPosition(); int start_line = in_stream.getLine(); int start_col = in_stream.getCol(); int next = in_stream.peek(); if (next == -1) { throw new LexerException(start_line, start_col, "Unexpected end of input stream when parsing string literal"); } if (next != 'x') { throw new LexerException(start_line, start_col, "Not a literal hex string"); } in_stream.read(); next = in_stream.peek(); if (next != '"') { throw new LexerException(start_line, start_col, "Not a literal hex string"); } in_stream.read(); next = in_stream.peek(); StringBuilder sb = new StringBuilder(); boolean pair = false; while (next != -1 && is_valid_hex(next)) { if (!Character.isWhitespace(next)) { sb.append(Character.toChars(next)); if (pair) { sb.append(' '); } pair = !pair; } in_stream.read(); next = in_stream.peek(); } if (next != '"') { throw new LexerException(start_line, start_col, "Unterminated literal hex string"); } in_stream.read(); StringBuilder result = new StringBuilder(); String raw = sb.toString(); for (String bite : raw.split(" ")) { result.append(Character.toChars(Integer.parseInt(bite, 16))); } return factory.create(TokenType.LiteralHex, start_index, start_line, start_col, in_stream.getPosition(), in_stream.getLine(), in_stream.getCol(), result.toString()); } LexHexLiteral(TokenFactory factory, LexerStream in_stream); }
LexHexLiteral { public Token read() throws IOException, LexerException { int start_index = in_stream.getPosition(); int start_line = in_stream.getLine(); int start_col = in_stream.getCol(); int next = in_stream.peek(); if (next == -1) { throw new LexerException(start_line, start_col, "Unexpected end of input stream when parsing string literal"); } if (next != 'x') { throw new LexerException(start_line, start_col, "Not a literal hex string"); } in_stream.read(); next = in_stream.peek(); if (next != '"') { throw new LexerException(start_line, start_col, "Not a literal hex string"); } in_stream.read(); next = in_stream.peek(); StringBuilder sb = new StringBuilder(); boolean pair = false; while (next != -1 && is_valid_hex(next)) { if (!Character.isWhitespace(next)) { sb.append(Character.toChars(next)); if (pair) { sb.append(' '); } pair = !pair; } in_stream.read(); next = in_stream.peek(); } if (next != '"') { throw new LexerException(start_line, start_col, "Unterminated literal hex string"); } in_stream.read(); StringBuilder result = new StringBuilder(); String raw = sb.toString(); for (String bite : raw.split(" ")) { result.append(Character.toChars(Integer.parseInt(bite, 16))); } return factory.create(TokenType.LiteralHex, start_index, start_line, start_col, in_stream.getPosition(), in_stream.getLine(), in_stream.getCol(), result.toString()); } LexHexLiteral(TokenFactory factory, LexerStream in_stream); Token read(); }
LexHexLiteral { public Token read() throws IOException, LexerException { int start_index = in_stream.getPosition(); int start_line = in_stream.getLine(); int start_col = in_stream.getCol(); int next = in_stream.peek(); if (next == -1) { throw new LexerException(start_line, start_col, "Unexpected end of input stream when parsing string literal"); } if (next != 'x') { throw new LexerException(start_line, start_col, "Not a literal hex string"); } in_stream.read(); next = in_stream.peek(); if (next != '"') { throw new LexerException(start_line, start_col, "Not a literal hex string"); } in_stream.read(); next = in_stream.peek(); StringBuilder sb = new StringBuilder(); boolean pair = false; while (next != -1 && is_valid_hex(next)) { if (!Character.isWhitespace(next)) { sb.append(Character.toChars(next)); if (pair) { sb.append(' '); } pair = !pair; } in_stream.read(); next = in_stream.peek(); } if (next != '"') { throw new LexerException(start_line, start_col, "Unterminated literal hex string"); } in_stream.read(); StringBuilder result = new StringBuilder(); String raw = sb.toString(); for (String bite : raw.split(" ")) { result.append(Character.toChars(Integer.parseInt(bite, 16))); } return factory.create(TokenType.LiteralHex, start_index, start_line, start_col, in_stream.getPosition(), in_stream.getLine(), in_stream.getCol(), result.toString()); } LexHexLiteral(TokenFactory factory, LexerStream in_stream); Token read(); }
@Test(expected = LexerException.class) public void unterminated() throws Exception { String str = "x\"AB CD"; LexerStream ls = new LexerStream(new StringReader(str)); LexHexLiteral lex = new LexHexLiteral(new BaseTokenFactory(), ls); lex.read(); }
public Token read() throws IOException, LexerException { int start_index = in_stream.getPosition(); int start_line = in_stream.getLine(); int start_col = in_stream.getCol(); int next = in_stream.peek(); if (next == -1) { throw new LexerException(start_line, start_col, "Unexpected end of input stream when parsing string literal"); } if (next != 'x') { throw new LexerException(start_line, start_col, "Not a literal hex string"); } in_stream.read(); next = in_stream.peek(); if (next != '"') { throw new LexerException(start_line, start_col, "Not a literal hex string"); } in_stream.read(); next = in_stream.peek(); StringBuilder sb = new StringBuilder(); boolean pair = false; while (next != -1 && is_valid_hex(next)) { if (!Character.isWhitespace(next)) { sb.append(Character.toChars(next)); if (pair) { sb.append(' '); } pair = !pair; } in_stream.read(); next = in_stream.peek(); } if (next != '"') { throw new LexerException(start_line, start_col, "Unterminated literal hex string"); } in_stream.read(); StringBuilder result = new StringBuilder(); String raw = sb.toString(); for (String bite : raw.split(" ")) { result.append(Character.toChars(Integer.parseInt(bite, 16))); } return factory.create(TokenType.LiteralHex, start_index, start_line, start_col, in_stream.getPosition(), in_stream.getLine(), in_stream.getCol(), result.toString()); }
LexHexLiteral { public Token read() throws IOException, LexerException { int start_index = in_stream.getPosition(); int start_line = in_stream.getLine(); int start_col = in_stream.getCol(); int next = in_stream.peek(); if (next == -1) { throw new LexerException(start_line, start_col, "Unexpected end of input stream when parsing string literal"); } if (next != 'x') { throw new LexerException(start_line, start_col, "Not a literal hex string"); } in_stream.read(); next = in_stream.peek(); if (next != '"') { throw new LexerException(start_line, start_col, "Not a literal hex string"); } in_stream.read(); next = in_stream.peek(); StringBuilder sb = new StringBuilder(); boolean pair = false; while (next != -1 && is_valid_hex(next)) { if (!Character.isWhitespace(next)) { sb.append(Character.toChars(next)); if (pair) { sb.append(' '); } pair = !pair; } in_stream.read(); next = in_stream.peek(); } if (next != '"') { throw new LexerException(start_line, start_col, "Unterminated literal hex string"); } in_stream.read(); StringBuilder result = new StringBuilder(); String raw = sb.toString(); for (String bite : raw.split(" ")) { result.append(Character.toChars(Integer.parseInt(bite, 16))); } return factory.create(TokenType.LiteralHex, start_index, start_line, start_col, in_stream.getPosition(), in_stream.getLine(), in_stream.getCol(), result.toString()); } }
LexHexLiteral { public Token read() throws IOException, LexerException { int start_index = in_stream.getPosition(); int start_line = in_stream.getLine(); int start_col = in_stream.getCol(); int next = in_stream.peek(); if (next == -1) { throw new LexerException(start_line, start_col, "Unexpected end of input stream when parsing string literal"); } if (next != 'x') { throw new LexerException(start_line, start_col, "Not a literal hex string"); } in_stream.read(); next = in_stream.peek(); if (next != '"') { throw new LexerException(start_line, start_col, "Not a literal hex string"); } in_stream.read(); next = in_stream.peek(); StringBuilder sb = new StringBuilder(); boolean pair = false; while (next != -1 && is_valid_hex(next)) { if (!Character.isWhitespace(next)) { sb.append(Character.toChars(next)); if (pair) { sb.append(' '); } pair = !pair; } in_stream.read(); next = in_stream.peek(); } if (next != '"') { throw new LexerException(start_line, start_col, "Unterminated literal hex string"); } in_stream.read(); StringBuilder result = new StringBuilder(); String raw = sb.toString(); for (String bite : raw.split(" ")) { result.append(Character.toChars(Integer.parseInt(bite, 16))); } return factory.create(TokenType.LiteralHex, start_index, start_line, start_col, in_stream.getPosition(), in_stream.getLine(), in_stream.getCol(), result.toString()); } LexHexLiteral(TokenFactory factory, LexerStream in_stream); }
LexHexLiteral { public Token read() throws IOException, LexerException { int start_index = in_stream.getPosition(); int start_line = in_stream.getLine(); int start_col = in_stream.getCol(); int next = in_stream.peek(); if (next == -1) { throw new LexerException(start_line, start_col, "Unexpected end of input stream when parsing string literal"); } if (next != 'x') { throw new LexerException(start_line, start_col, "Not a literal hex string"); } in_stream.read(); next = in_stream.peek(); if (next != '"') { throw new LexerException(start_line, start_col, "Not a literal hex string"); } in_stream.read(); next = in_stream.peek(); StringBuilder sb = new StringBuilder(); boolean pair = false; while (next != -1 && is_valid_hex(next)) { if (!Character.isWhitespace(next)) { sb.append(Character.toChars(next)); if (pair) { sb.append(' '); } pair = !pair; } in_stream.read(); next = in_stream.peek(); } if (next != '"') { throw new LexerException(start_line, start_col, "Unterminated literal hex string"); } in_stream.read(); StringBuilder result = new StringBuilder(); String raw = sb.toString(); for (String bite : raw.split(" ")) { result.append(Character.toChars(Integer.parseInt(bite, 16))); } return factory.create(TokenType.LiteralHex, start_index, start_line, start_col, in_stream.getPosition(), in_stream.getLine(), in_stream.getCol(), result.toString()); } LexHexLiteral(TokenFactory factory, LexerStream in_stream); Token read(); }
LexHexLiteral { public Token read() throws IOException, LexerException { int start_index = in_stream.getPosition(); int start_line = in_stream.getLine(); int start_col = in_stream.getCol(); int next = in_stream.peek(); if (next == -1) { throw new LexerException(start_line, start_col, "Unexpected end of input stream when parsing string literal"); } if (next != 'x') { throw new LexerException(start_line, start_col, "Not a literal hex string"); } in_stream.read(); next = in_stream.peek(); if (next != '"') { throw new LexerException(start_line, start_col, "Not a literal hex string"); } in_stream.read(); next = in_stream.peek(); StringBuilder sb = new StringBuilder(); boolean pair = false; while (next != -1 && is_valid_hex(next)) { if (!Character.isWhitespace(next)) { sb.append(Character.toChars(next)); if (pair) { sb.append(' '); } pair = !pair; } in_stream.read(); next = in_stream.peek(); } if (next != '"') { throw new LexerException(start_line, start_col, "Unterminated literal hex string"); } in_stream.read(); StringBuilder result = new StringBuilder(); String raw = sb.toString(); for (String bite : raw.split(" ")) { result.append(Character.toChars(Integer.parseInt(bite, 16))); } return factory.create(TokenType.LiteralHex, start_index, start_line, start_col, in_stream.getPosition(), in_stream.getLine(), in_stream.getCol(), result.toString()); } LexHexLiteral(TokenFactory factory, LexerStream in_stream); Token read(); }
@Test public void not_op() throws Exception { String str = "foo"; LexerStream ls = new LexerStream(new StringReader(str)); LexOperator lex = new LexOperator(new BaseTokenFactory(), ls); Token tok = lex.read(); assertNull(tok); lex = new LexOperator(new BaseTokenFactory(), new LexerStream(new StringReader(""))); try { tok = lex.read(); } catch (LexerException e) { return; } fail(); }
public Token read() throws IOException, LexerException { int index = in_stream.getPosition(); int line = in_stream.getLine(); int col = in_stream.getCol(); int next = in_stream.peek(); if (next == -1) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Unexpected end of input stream when parsing operator"); } if (next == '.') { int tmp = in_stream.peek(2); if (tmp > 0 && Character.isDigit(tmp)) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Literal float encountered when operator '.' expected"); } } char cur = (char)in_stream.read(); next = in_stream.peek(); switch (cur) { case '+': switch (next) { case '+': in_stream.read(); return factory.create(TokenType.Increment, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.PlusAssign, index, line, col, 2); } return factory.create(TokenType.Plus, index, line, col); case '-': switch (next) { case '-': in_stream.read(); return factory.create(TokenType.Decrement, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.MinusAssign, index, line, col, 2); } return factory.create(TokenType.Minus, index, line, col); case '*': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.TimesAssign, index, line, col, 2); default: break; } return factory.create(TokenType.Times, index, line, col); case '/': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.DivAssign, index, line, col, 2); } return factory.create(TokenType.Div, index, line, col); case '%': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.ModAssign, index, line, col, 2); } return factory.create(TokenType.Mod, index, line, col); case '&': switch (next) { case '&': in_stream.read(); return factory.create(TokenType.LogicalAnd, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.BitwiseAndAssign, index, line, col, 2); } return factory.create(TokenType.BitwiseAnd, index, line, col); case '|': switch (next) { case '|': in_stream.read(); return factory.create(TokenType.LogicalOr, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.BitwiseOrAssign, index, line, col, 2); } return factory.create(TokenType.BitwiseOr, index, line, col); case '^': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.XorAssign, index, line, col, 2); case '^': in_stream.read(); if (in_stream.peek() == '=') { in_stream.read(); return factory.create(TokenType.PowAssign, index, line, col, 3); } return factory.create(TokenType.Pow, index, line, col, 2); } return factory.create(TokenType.Xor, index, line, col); case '!': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.NotEqual, index, line, col, 2); case '<': in_stream.read(); switch (in_stream.peek()) { case '=': in_stream.read(); return factory.create(TokenType.UnorderedOrGreater, index, line, col, 3); case '>': in_stream.read(); switch (in_stream.peek()) { case '=': in_stream.read(); return factory.create(TokenType.Unordered, index, line, col, 4); } return factory.create(TokenType.UnorderedOrEqual, index, line, col, 3); } return factory.create(TokenType.UnorderedGreaterOrEqual, index, line, col, 2); case '>': in_stream.read(); switch (in_stream.peek()) { case '=': in_stream.read(); return factory.create(TokenType.UnorderedOrLess, index, line, col, 3); default: break; } return factory.create(TokenType.UnorderedLessOrEqual, index, line, col, 2); } return factory.create(TokenType.Not, index, line, col); case '~': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.TildeAssign, index, line, col, 2); } return factory.create(TokenType.Tilde, index, line, col); case '=': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.Equal, index, line, col, 2); case '>': in_stream.read(); return factory.create(TokenType.GoesTo, index, line, col, 2); } return factory.create(TokenType.Assign, index, line, col); case '<': switch (next) { case '<': in_stream.read(); switch (in_stream.peek()) { case '=': in_stream.read(); return factory.create(TokenType.ShiftLeftAssign, index, line, col, 3); default: break; } return factory.create(TokenType.ShiftLeft, index, line, col, 2); case '>': in_stream.read(); switch (in_stream.peek()) { case '=': in_stream.read(); return factory.create(TokenType.LessEqualOrGreater, index, line, col, 3); default: break; } return factory.create(TokenType.LessOrGreater, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.LessEqual, index, line, col, 2); } return factory.create(TokenType.LessThan, index, line, col); case '>': switch (next) { case '>': in_stream.read(); int p = in_stream.peek(); if (p != -1) { switch ((char)p) { case '=': in_stream.read(); return factory.create(TokenType.ShiftRightAssign, index, line, col, 3); case '>': in_stream.read(); int q = in_stream.peek(); if (q != -1 && q == '=') { in_stream.read(); return factory.create(TokenType.TripleRightShiftAssign, index, line, col, 4); } return factory.create(TokenType.ShiftRightUnsigned, index, line, col, 3); } } return factory.create(TokenType.ShiftRight, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.GreaterEqual, index, line, col, 2); } return factory.create(TokenType.GreaterThan, index, line, col); case '?': return factory.create(TokenType.Question, index, line, col); case '$': return factory.create(TokenType.Dollar, index, line, col); case ';': return factory.create(TokenType.Semicolon, index, line, col); case ':': return factory.create(TokenType.Colon, index, line, col); case ',': return factory.create(TokenType.Comma, index, line, col); case '.': if (next == '.') { in_stream.read(); int p = in_stream.peek(); if (p != -1 && p == '.') { in_stream.read(); return factory.create(TokenType.TripleDot, index, line, col, 3); } return factory.create(TokenType.DoubleDot, index, line, col, 2); } return factory.create(TokenType.Dot, index, line, col); case ')': return factory.create(TokenType.CloseParenthesis, index, line, col); case '(': return factory.create(TokenType.OpenParenthesis, index, line, col); case ']': return factory.create(TokenType.CloseSquareBracket, index, line, col); case '[': return factory.create(TokenType.OpenSquareBracket, index, line, col); case '}': return factory.create(TokenType.CloseCurlyBrace, index, line, col); case '{': return factory.create(TokenType.OpenCurlyBrace, index, line, col); case '#': return factory.create(TokenType.Hash, index, line, col); default: return null; } }
LexOperator { public Token read() throws IOException, LexerException { int index = in_stream.getPosition(); int line = in_stream.getLine(); int col = in_stream.getCol(); int next = in_stream.peek(); if (next == -1) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Unexpected end of input stream when parsing operator"); } if (next == '.') { int tmp = in_stream.peek(2); if (tmp > 0 && Character.isDigit(tmp)) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Literal float encountered when operator '.' expected"); } } char cur = (char)in_stream.read(); next = in_stream.peek(); switch (cur) { case '+': switch (next) { case '+': in_stream.read(); return factory.create(TokenType.Increment, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.PlusAssign, index, line, col, 2); } return factory.create(TokenType.Plus, index, line, col); case '-': switch (next) { case '-': in_stream.read(); return factory.create(TokenType.Decrement, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.MinusAssign, index, line, col, 2); } return factory.create(TokenType.Minus, index, line, col); case '*': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.TimesAssign, index, line, col, 2); default: break; } return factory.create(TokenType.Times, index, line, col); case '/': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.DivAssign, index, line, col, 2); } return factory.create(TokenType.Div, index, line, col); case '%': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.ModAssign, index, line, col, 2); } return factory.create(TokenType.Mod, index, line, col); case '&': switch (next) { case '&': in_stream.read(); return factory.create(TokenType.LogicalAnd, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.BitwiseAndAssign, index, line, col, 2); } return factory.create(TokenType.BitwiseAnd, index, line, col); case '|': switch (next) { case '|': in_stream.read(); return factory.create(TokenType.LogicalOr, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.BitwiseOrAssign, index, line, col, 2); } return factory.create(TokenType.BitwiseOr, index, line, col); case '^': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.XorAssign, index, line, col, 2); case '^': in_stream.read(); if (in_stream.peek() == '=') { in_stream.read(); return factory.create(TokenType.PowAssign, index, line, col, 3); } return factory.create(TokenType.Pow, index, line, col, 2); } return factory.create(TokenType.Xor, index, line, col); case '!': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.NotEqual, index, line, col, 2); case '<': in_stream.read(); switch (in_stream.peek()) { case '=': in_stream.read(); return factory.create(TokenType.UnorderedOrGreater, index, line, col, 3); case '>': in_stream.read(); switch (in_stream.peek()) { case '=': in_stream.read(); return factory.create(TokenType.Unordered, index, line, col, 4); } return factory.create(TokenType.UnorderedOrEqual, index, line, col, 3); } return factory.create(TokenType.UnorderedGreaterOrEqual, index, line, col, 2); case '>': in_stream.read(); switch (in_stream.peek()) { case '=': in_stream.read(); return factory.create(TokenType.UnorderedOrLess, index, line, col, 3); default: break; } return factory.create(TokenType.UnorderedLessOrEqual, index, line, col, 2); } return factory.create(TokenType.Not, index, line, col); case '~': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.TildeAssign, index, line, col, 2); } return factory.create(TokenType.Tilde, index, line, col); case '=': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.Equal, index, line, col, 2); case '>': in_stream.read(); return factory.create(TokenType.GoesTo, index, line, col, 2); } return factory.create(TokenType.Assign, index, line, col); case '<': switch (next) { case '<': in_stream.read(); switch (in_stream.peek()) { case '=': in_stream.read(); return factory.create(TokenType.ShiftLeftAssign, index, line, col, 3); default: break; } return factory.create(TokenType.ShiftLeft, index, line, col, 2); case '>': in_stream.read(); switch (in_stream.peek()) { case '=': in_stream.read(); return factory.create(TokenType.LessEqualOrGreater, index, line, col, 3); default: break; } return factory.create(TokenType.LessOrGreater, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.LessEqual, index, line, col, 2); } return factory.create(TokenType.LessThan, index, line, col); case '>': switch (next) { case '>': in_stream.read(); int p = in_stream.peek(); if (p != -1) { switch ((char)p) { case '=': in_stream.read(); return factory.create(TokenType.ShiftRightAssign, index, line, col, 3); case '>': in_stream.read(); int q = in_stream.peek(); if (q != -1 && q == '=') { in_stream.read(); return factory.create(TokenType.TripleRightShiftAssign, index, line, col, 4); } return factory.create(TokenType.ShiftRightUnsigned, index, line, col, 3); } } return factory.create(TokenType.ShiftRight, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.GreaterEqual, index, line, col, 2); } return factory.create(TokenType.GreaterThan, index, line, col); case '?': return factory.create(TokenType.Question, index, line, col); case '$': return factory.create(TokenType.Dollar, index, line, col); case ';': return factory.create(TokenType.Semicolon, index, line, col); case ':': return factory.create(TokenType.Colon, index, line, col); case ',': return factory.create(TokenType.Comma, index, line, col); case '.': if (next == '.') { in_stream.read(); int p = in_stream.peek(); if (p != -1 && p == '.') { in_stream.read(); return factory.create(TokenType.TripleDot, index, line, col, 3); } return factory.create(TokenType.DoubleDot, index, line, col, 2); } return factory.create(TokenType.Dot, index, line, col); case ')': return factory.create(TokenType.CloseParenthesis, index, line, col); case '(': return factory.create(TokenType.OpenParenthesis, index, line, col); case ']': return factory.create(TokenType.CloseSquareBracket, index, line, col); case '[': return factory.create(TokenType.OpenSquareBracket, index, line, col); case '}': return factory.create(TokenType.CloseCurlyBrace, index, line, col); case '{': return factory.create(TokenType.OpenCurlyBrace, index, line, col); case '#': return factory.create(TokenType.Hash, index, line, col); default: return null; } } }
LexOperator { public Token read() throws IOException, LexerException { int index = in_stream.getPosition(); int line = in_stream.getLine(); int col = in_stream.getCol(); int next = in_stream.peek(); if (next == -1) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Unexpected end of input stream when parsing operator"); } if (next == '.') { int tmp = in_stream.peek(2); if (tmp > 0 && Character.isDigit(tmp)) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Literal float encountered when operator '.' expected"); } } char cur = (char)in_stream.read(); next = in_stream.peek(); switch (cur) { case '+': switch (next) { case '+': in_stream.read(); return factory.create(TokenType.Increment, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.PlusAssign, index, line, col, 2); } return factory.create(TokenType.Plus, index, line, col); case '-': switch (next) { case '-': in_stream.read(); return factory.create(TokenType.Decrement, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.MinusAssign, index, line, col, 2); } return factory.create(TokenType.Minus, index, line, col); case '*': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.TimesAssign, index, line, col, 2); default: break; } return factory.create(TokenType.Times, index, line, col); case '/': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.DivAssign, index, line, col, 2); } return factory.create(TokenType.Div, index, line, col); case '%': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.ModAssign, index, line, col, 2); } return factory.create(TokenType.Mod, index, line, col); case '&': switch (next) { case '&': in_stream.read(); return factory.create(TokenType.LogicalAnd, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.BitwiseAndAssign, index, line, col, 2); } return factory.create(TokenType.BitwiseAnd, index, line, col); case '|': switch (next) { case '|': in_stream.read(); return factory.create(TokenType.LogicalOr, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.BitwiseOrAssign, index, line, col, 2); } return factory.create(TokenType.BitwiseOr, index, line, col); case '^': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.XorAssign, index, line, col, 2); case '^': in_stream.read(); if (in_stream.peek() == '=') { in_stream.read(); return factory.create(TokenType.PowAssign, index, line, col, 3); } return factory.create(TokenType.Pow, index, line, col, 2); } return factory.create(TokenType.Xor, index, line, col); case '!': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.NotEqual, index, line, col, 2); case '<': in_stream.read(); switch (in_stream.peek()) { case '=': in_stream.read(); return factory.create(TokenType.UnorderedOrGreater, index, line, col, 3); case '>': in_stream.read(); switch (in_stream.peek()) { case '=': in_stream.read(); return factory.create(TokenType.Unordered, index, line, col, 4); } return factory.create(TokenType.UnorderedOrEqual, index, line, col, 3); } return factory.create(TokenType.UnorderedGreaterOrEqual, index, line, col, 2); case '>': in_stream.read(); switch (in_stream.peek()) { case '=': in_stream.read(); return factory.create(TokenType.UnorderedOrLess, index, line, col, 3); default: break; } return factory.create(TokenType.UnorderedLessOrEqual, index, line, col, 2); } return factory.create(TokenType.Not, index, line, col); case '~': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.TildeAssign, index, line, col, 2); } return factory.create(TokenType.Tilde, index, line, col); case '=': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.Equal, index, line, col, 2); case '>': in_stream.read(); return factory.create(TokenType.GoesTo, index, line, col, 2); } return factory.create(TokenType.Assign, index, line, col); case '<': switch (next) { case '<': in_stream.read(); switch (in_stream.peek()) { case '=': in_stream.read(); return factory.create(TokenType.ShiftLeftAssign, index, line, col, 3); default: break; } return factory.create(TokenType.ShiftLeft, index, line, col, 2); case '>': in_stream.read(); switch (in_stream.peek()) { case '=': in_stream.read(); return factory.create(TokenType.LessEqualOrGreater, index, line, col, 3); default: break; } return factory.create(TokenType.LessOrGreater, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.LessEqual, index, line, col, 2); } return factory.create(TokenType.LessThan, index, line, col); case '>': switch (next) { case '>': in_stream.read(); int p = in_stream.peek(); if (p != -1) { switch ((char)p) { case '=': in_stream.read(); return factory.create(TokenType.ShiftRightAssign, index, line, col, 3); case '>': in_stream.read(); int q = in_stream.peek(); if (q != -1 && q == '=') { in_stream.read(); return factory.create(TokenType.TripleRightShiftAssign, index, line, col, 4); } return factory.create(TokenType.ShiftRightUnsigned, index, line, col, 3); } } return factory.create(TokenType.ShiftRight, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.GreaterEqual, index, line, col, 2); } return factory.create(TokenType.GreaterThan, index, line, col); case '?': return factory.create(TokenType.Question, index, line, col); case '$': return factory.create(TokenType.Dollar, index, line, col); case ';': return factory.create(TokenType.Semicolon, index, line, col); case ':': return factory.create(TokenType.Colon, index, line, col); case ',': return factory.create(TokenType.Comma, index, line, col); case '.': if (next == '.') { in_stream.read(); int p = in_stream.peek(); if (p != -1 && p == '.') { in_stream.read(); return factory.create(TokenType.TripleDot, index, line, col, 3); } return factory.create(TokenType.DoubleDot, index, line, col, 2); } return factory.create(TokenType.Dot, index, line, col); case ')': return factory.create(TokenType.CloseParenthesis, index, line, col); case '(': return factory.create(TokenType.OpenParenthesis, index, line, col); case ']': return factory.create(TokenType.CloseSquareBracket, index, line, col); case '[': return factory.create(TokenType.OpenSquareBracket, index, line, col); case '}': return factory.create(TokenType.CloseCurlyBrace, index, line, col); case '{': return factory.create(TokenType.OpenCurlyBrace, index, line, col); case '#': return factory.create(TokenType.Hash, index, line, col); default: return null; } } LexOperator(TokenFactory factory, LexerStream in_stream); }
LexOperator { public Token read() throws IOException, LexerException { int index = in_stream.getPosition(); int line = in_stream.getLine(); int col = in_stream.getCol(); int next = in_stream.peek(); if (next == -1) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Unexpected end of input stream when parsing operator"); } if (next == '.') { int tmp = in_stream.peek(2); if (tmp > 0 && Character.isDigit(tmp)) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Literal float encountered when operator '.' expected"); } } char cur = (char)in_stream.read(); next = in_stream.peek(); switch (cur) { case '+': switch (next) { case '+': in_stream.read(); return factory.create(TokenType.Increment, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.PlusAssign, index, line, col, 2); } return factory.create(TokenType.Plus, index, line, col); case '-': switch (next) { case '-': in_stream.read(); return factory.create(TokenType.Decrement, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.MinusAssign, index, line, col, 2); } return factory.create(TokenType.Minus, index, line, col); case '*': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.TimesAssign, index, line, col, 2); default: break; } return factory.create(TokenType.Times, index, line, col); case '/': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.DivAssign, index, line, col, 2); } return factory.create(TokenType.Div, index, line, col); case '%': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.ModAssign, index, line, col, 2); } return factory.create(TokenType.Mod, index, line, col); case '&': switch (next) { case '&': in_stream.read(); return factory.create(TokenType.LogicalAnd, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.BitwiseAndAssign, index, line, col, 2); } return factory.create(TokenType.BitwiseAnd, index, line, col); case '|': switch (next) { case '|': in_stream.read(); return factory.create(TokenType.LogicalOr, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.BitwiseOrAssign, index, line, col, 2); } return factory.create(TokenType.BitwiseOr, index, line, col); case '^': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.XorAssign, index, line, col, 2); case '^': in_stream.read(); if (in_stream.peek() == '=') { in_stream.read(); return factory.create(TokenType.PowAssign, index, line, col, 3); } return factory.create(TokenType.Pow, index, line, col, 2); } return factory.create(TokenType.Xor, index, line, col); case '!': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.NotEqual, index, line, col, 2); case '<': in_stream.read(); switch (in_stream.peek()) { case '=': in_stream.read(); return factory.create(TokenType.UnorderedOrGreater, index, line, col, 3); case '>': in_stream.read(); switch (in_stream.peek()) { case '=': in_stream.read(); return factory.create(TokenType.Unordered, index, line, col, 4); } return factory.create(TokenType.UnorderedOrEqual, index, line, col, 3); } return factory.create(TokenType.UnorderedGreaterOrEqual, index, line, col, 2); case '>': in_stream.read(); switch (in_stream.peek()) { case '=': in_stream.read(); return factory.create(TokenType.UnorderedOrLess, index, line, col, 3); default: break; } return factory.create(TokenType.UnorderedLessOrEqual, index, line, col, 2); } return factory.create(TokenType.Not, index, line, col); case '~': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.TildeAssign, index, line, col, 2); } return factory.create(TokenType.Tilde, index, line, col); case '=': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.Equal, index, line, col, 2); case '>': in_stream.read(); return factory.create(TokenType.GoesTo, index, line, col, 2); } return factory.create(TokenType.Assign, index, line, col); case '<': switch (next) { case '<': in_stream.read(); switch (in_stream.peek()) { case '=': in_stream.read(); return factory.create(TokenType.ShiftLeftAssign, index, line, col, 3); default: break; } return factory.create(TokenType.ShiftLeft, index, line, col, 2); case '>': in_stream.read(); switch (in_stream.peek()) { case '=': in_stream.read(); return factory.create(TokenType.LessEqualOrGreater, index, line, col, 3); default: break; } return factory.create(TokenType.LessOrGreater, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.LessEqual, index, line, col, 2); } return factory.create(TokenType.LessThan, index, line, col); case '>': switch (next) { case '>': in_stream.read(); int p = in_stream.peek(); if (p != -1) { switch ((char)p) { case '=': in_stream.read(); return factory.create(TokenType.ShiftRightAssign, index, line, col, 3); case '>': in_stream.read(); int q = in_stream.peek(); if (q != -1 && q == '=') { in_stream.read(); return factory.create(TokenType.TripleRightShiftAssign, index, line, col, 4); } return factory.create(TokenType.ShiftRightUnsigned, index, line, col, 3); } } return factory.create(TokenType.ShiftRight, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.GreaterEqual, index, line, col, 2); } return factory.create(TokenType.GreaterThan, index, line, col); case '?': return factory.create(TokenType.Question, index, line, col); case '$': return factory.create(TokenType.Dollar, index, line, col); case ';': return factory.create(TokenType.Semicolon, index, line, col); case ':': return factory.create(TokenType.Colon, index, line, col); case ',': return factory.create(TokenType.Comma, index, line, col); case '.': if (next == '.') { in_stream.read(); int p = in_stream.peek(); if (p != -1 && p == '.') { in_stream.read(); return factory.create(TokenType.TripleDot, index, line, col, 3); } return factory.create(TokenType.DoubleDot, index, line, col, 2); } return factory.create(TokenType.Dot, index, line, col); case ')': return factory.create(TokenType.CloseParenthesis, index, line, col); case '(': return factory.create(TokenType.OpenParenthesis, index, line, col); case ']': return factory.create(TokenType.CloseSquareBracket, index, line, col); case '[': return factory.create(TokenType.OpenSquareBracket, index, line, col); case '}': return factory.create(TokenType.CloseCurlyBrace, index, line, col); case '{': return factory.create(TokenType.OpenCurlyBrace, index, line, col); case '#': return factory.create(TokenType.Hash, index, line, col); default: return null; } } LexOperator(TokenFactory factory, LexerStream in_stream); Token read(); }
LexOperator { public Token read() throws IOException, LexerException { int index = in_stream.getPosition(); int line = in_stream.getLine(); int col = in_stream.getCol(); int next = in_stream.peek(); if (next == -1) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Unexpected end of input stream when parsing operator"); } if (next == '.') { int tmp = in_stream.peek(2); if (tmp > 0 && Character.isDigit(tmp)) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Literal float encountered when operator '.' expected"); } } char cur = (char)in_stream.read(); next = in_stream.peek(); switch (cur) { case '+': switch (next) { case '+': in_stream.read(); return factory.create(TokenType.Increment, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.PlusAssign, index, line, col, 2); } return factory.create(TokenType.Plus, index, line, col); case '-': switch (next) { case '-': in_stream.read(); return factory.create(TokenType.Decrement, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.MinusAssign, index, line, col, 2); } return factory.create(TokenType.Minus, index, line, col); case '*': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.TimesAssign, index, line, col, 2); default: break; } return factory.create(TokenType.Times, index, line, col); case '/': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.DivAssign, index, line, col, 2); } return factory.create(TokenType.Div, index, line, col); case '%': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.ModAssign, index, line, col, 2); } return factory.create(TokenType.Mod, index, line, col); case '&': switch (next) { case '&': in_stream.read(); return factory.create(TokenType.LogicalAnd, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.BitwiseAndAssign, index, line, col, 2); } return factory.create(TokenType.BitwiseAnd, index, line, col); case '|': switch (next) { case '|': in_stream.read(); return factory.create(TokenType.LogicalOr, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.BitwiseOrAssign, index, line, col, 2); } return factory.create(TokenType.BitwiseOr, index, line, col); case '^': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.XorAssign, index, line, col, 2); case '^': in_stream.read(); if (in_stream.peek() == '=') { in_stream.read(); return factory.create(TokenType.PowAssign, index, line, col, 3); } return factory.create(TokenType.Pow, index, line, col, 2); } return factory.create(TokenType.Xor, index, line, col); case '!': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.NotEqual, index, line, col, 2); case '<': in_stream.read(); switch (in_stream.peek()) { case '=': in_stream.read(); return factory.create(TokenType.UnorderedOrGreater, index, line, col, 3); case '>': in_stream.read(); switch (in_stream.peek()) { case '=': in_stream.read(); return factory.create(TokenType.Unordered, index, line, col, 4); } return factory.create(TokenType.UnorderedOrEqual, index, line, col, 3); } return factory.create(TokenType.UnorderedGreaterOrEqual, index, line, col, 2); case '>': in_stream.read(); switch (in_stream.peek()) { case '=': in_stream.read(); return factory.create(TokenType.UnorderedOrLess, index, line, col, 3); default: break; } return factory.create(TokenType.UnorderedLessOrEqual, index, line, col, 2); } return factory.create(TokenType.Not, index, line, col); case '~': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.TildeAssign, index, line, col, 2); } return factory.create(TokenType.Tilde, index, line, col); case '=': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.Equal, index, line, col, 2); case '>': in_stream.read(); return factory.create(TokenType.GoesTo, index, line, col, 2); } return factory.create(TokenType.Assign, index, line, col); case '<': switch (next) { case '<': in_stream.read(); switch (in_stream.peek()) { case '=': in_stream.read(); return factory.create(TokenType.ShiftLeftAssign, index, line, col, 3); default: break; } return factory.create(TokenType.ShiftLeft, index, line, col, 2); case '>': in_stream.read(); switch (in_stream.peek()) { case '=': in_stream.read(); return factory.create(TokenType.LessEqualOrGreater, index, line, col, 3); default: break; } return factory.create(TokenType.LessOrGreater, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.LessEqual, index, line, col, 2); } return factory.create(TokenType.LessThan, index, line, col); case '>': switch (next) { case '>': in_stream.read(); int p = in_stream.peek(); if (p != -1) { switch ((char)p) { case '=': in_stream.read(); return factory.create(TokenType.ShiftRightAssign, index, line, col, 3); case '>': in_stream.read(); int q = in_stream.peek(); if (q != -1 && q == '=') { in_stream.read(); return factory.create(TokenType.TripleRightShiftAssign, index, line, col, 4); } return factory.create(TokenType.ShiftRightUnsigned, index, line, col, 3); } } return factory.create(TokenType.ShiftRight, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.GreaterEqual, index, line, col, 2); } return factory.create(TokenType.GreaterThan, index, line, col); case '?': return factory.create(TokenType.Question, index, line, col); case '$': return factory.create(TokenType.Dollar, index, line, col); case ';': return factory.create(TokenType.Semicolon, index, line, col); case ':': return factory.create(TokenType.Colon, index, line, col); case ',': return factory.create(TokenType.Comma, index, line, col); case '.': if (next == '.') { in_stream.read(); int p = in_stream.peek(); if (p != -1 && p == '.') { in_stream.read(); return factory.create(TokenType.TripleDot, index, line, col, 3); } return factory.create(TokenType.DoubleDot, index, line, col, 2); } return factory.create(TokenType.Dot, index, line, col); case ')': return factory.create(TokenType.CloseParenthesis, index, line, col); case '(': return factory.create(TokenType.OpenParenthesis, index, line, col); case ']': return factory.create(TokenType.CloseSquareBracket, index, line, col); case '[': return factory.create(TokenType.OpenSquareBracket, index, line, col); case '}': return factory.create(TokenType.CloseCurlyBrace, index, line, col); case '{': return factory.create(TokenType.OpenCurlyBrace, index, line, col); case '#': return factory.create(TokenType.Hash, index, line, col); default: return null; } } LexOperator(TokenFactory factory, LexerStream in_stream); Token read(); }
@Test public void assign() throws Exception { String str = "a=b"; LexerStream ls = new LexerStream(new StringReader(str)); LexOperator lex = new LexOperator(new BaseTokenFactory(), ls); ls.read(); Token tok = lex.read(); assertEquals(TokenType.Assign, tok.getType()); assertEquals("=", tok.getValue()); assertEquals(1, tok.getLine()); assertEquals(2, tok.getCol()); assertEquals(1, tok.getEndLine()); assertEquals(3, tok.getEndCol()); }
public Token read() throws IOException, LexerException { int index = in_stream.getPosition(); int line = in_stream.getLine(); int col = in_stream.getCol(); int next = in_stream.peek(); if (next == -1) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Unexpected end of input stream when parsing operator"); } if (next == '.') { int tmp = in_stream.peek(2); if (tmp > 0 && Character.isDigit(tmp)) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Literal float encountered when operator '.' expected"); } } char cur = (char)in_stream.read(); next = in_stream.peek(); switch (cur) { case '+': switch (next) { case '+': in_stream.read(); return factory.create(TokenType.Increment, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.PlusAssign, index, line, col, 2); } return factory.create(TokenType.Plus, index, line, col); case '-': switch (next) { case '-': in_stream.read(); return factory.create(TokenType.Decrement, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.MinusAssign, index, line, col, 2); } return factory.create(TokenType.Minus, index, line, col); case '*': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.TimesAssign, index, line, col, 2); default: break; } return factory.create(TokenType.Times, index, line, col); case '/': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.DivAssign, index, line, col, 2); } return factory.create(TokenType.Div, index, line, col); case '%': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.ModAssign, index, line, col, 2); } return factory.create(TokenType.Mod, index, line, col); case '&': switch (next) { case '&': in_stream.read(); return factory.create(TokenType.LogicalAnd, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.BitwiseAndAssign, index, line, col, 2); } return factory.create(TokenType.BitwiseAnd, index, line, col); case '|': switch (next) { case '|': in_stream.read(); return factory.create(TokenType.LogicalOr, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.BitwiseOrAssign, index, line, col, 2); } return factory.create(TokenType.BitwiseOr, index, line, col); case '^': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.XorAssign, index, line, col, 2); case '^': in_stream.read(); if (in_stream.peek() == '=') { in_stream.read(); return factory.create(TokenType.PowAssign, index, line, col, 3); } return factory.create(TokenType.Pow, index, line, col, 2); } return factory.create(TokenType.Xor, index, line, col); case '!': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.NotEqual, index, line, col, 2); case '<': in_stream.read(); switch (in_stream.peek()) { case '=': in_stream.read(); return factory.create(TokenType.UnorderedOrGreater, index, line, col, 3); case '>': in_stream.read(); switch (in_stream.peek()) { case '=': in_stream.read(); return factory.create(TokenType.Unordered, index, line, col, 4); } return factory.create(TokenType.UnorderedOrEqual, index, line, col, 3); } return factory.create(TokenType.UnorderedGreaterOrEqual, index, line, col, 2); case '>': in_stream.read(); switch (in_stream.peek()) { case '=': in_stream.read(); return factory.create(TokenType.UnorderedOrLess, index, line, col, 3); default: break; } return factory.create(TokenType.UnorderedLessOrEqual, index, line, col, 2); } return factory.create(TokenType.Not, index, line, col); case '~': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.TildeAssign, index, line, col, 2); } return factory.create(TokenType.Tilde, index, line, col); case '=': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.Equal, index, line, col, 2); case '>': in_stream.read(); return factory.create(TokenType.GoesTo, index, line, col, 2); } return factory.create(TokenType.Assign, index, line, col); case '<': switch (next) { case '<': in_stream.read(); switch (in_stream.peek()) { case '=': in_stream.read(); return factory.create(TokenType.ShiftLeftAssign, index, line, col, 3); default: break; } return factory.create(TokenType.ShiftLeft, index, line, col, 2); case '>': in_stream.read(); switch (in_stream.peek()) { case '=': in_stream.read(); return factory.create(TokenType.LessEqualOrGreater, index, line, col, 3); default: break; } return factory.create(TokenType.LessOrGreater, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.LessEqual, index, line, col, 2); } return factory.create(TokenType.LessThan, index, line, col); case '>': switch (next) { case '>': in_stream.read(); int p = in_stream.peek(); if (p != -1) { switch ((char)p) { case '=': in_stream.read(); return factory.create(TokenType.ShiftRightAssign, index, line, col, 3); case '>': in_stream.read(); int q = in_stream.peek(); if (q != -1 && q == '=') { in_stream.read(); return factory.create(TokenType.TripleRightShiftAssign, index, line, col, 4); } return factory.create(TokenType.ShiftRightUnsigned, index, line, col, 3); } } return factory.create(TokenType.ShiftRight, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.GreaterEqual, index, line, col, 2); } return factory.create(TokenType.GreaterThan, index, line, col); case '?': return factory.create(TokenType.Question, index, line, col); case '$': return factory.create(TokenType.Dollar, index, line, col); case ';': return factory.create(TokenType.Semicolon, index, line, col); case ':': return factory.create(TokenType.Colon, index, line, col); case ',': return factory.create(TokenType.Comma, index, line, col); case '.': if (next == '.') { in_stream.read(); int p = in_stream.peek(); if (p != -1 && p == '.') { in_stream.read(); return factory.create(TokenType.TripleDot, index, line, col, 3); } return factory.create(TokenType.DoubleDot, index, line, col, 2); } return factory.create(TokenType.Dot, index, line, col); case ')': return factory.create(TokenType.CloseParenthesis, index, line, col); case '(': return factory.create(TokenType.OpenParenthesis, index, line, col); case ']': return factory.create(TokenType.CloseSquareBracket, index, line, col); case '[': return factory.create(TokenType.OpenSquareBracket, index, line, col); case '}': return factory.create(TokenType.CloseCurlyBrace, index, line, col); case '{': return factory.create(TokenType.OpenCurlyBrace, index, line, col); case '#': return factory.create(TokenType.Hash, index, line, col); default: return null; } }
LexOperator { public Token read() throws IOException, LexerException { int index = in_stream.getPosition(); int line = in_stream.getLine(); int col = in_stream.getCol(); int next = in_stream.peek(); if (next == -1) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Unexpected end of input stream when parsing operator"); } if (next == '.') { int tmp = in_stream.peek(2); if (tmp > 0 && Character.isDigit(tmp)) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Literal float encountered when operator '.' expected"); } } char cur = (char)in_stream.read(); next = in_stream.peek(); switch (cur) { case '+': switch (next) { case '+': in_stream.read(); return factory.create(TokenType.Increment, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.PlusAssign, index, line, col, 2); } return factory.create(TokenType.Plus, index, line, col); case '-': switch (next) { case '-': in_stream.read(); return factory.create(TokenType.Decrement, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.MinusAssign, index, line, col, 2); } return factory.create(TokenType.Minus, index, line, col); case '*': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.TimesAssign, index, line, col, 2); default: break; } return factory.create(TokenType.Times, index, line, col); case '/': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.DivAssign, index, line, col, 2); } return factory.create(TokenType.Div, index, line, col); case '%': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.ModAssign, index, line, col, 2); } return factory.create(TokenType.Mod, index, line, col); case '&': switch (next) { case '&': in_stream.read(); return factory.create(TokenType.LogicalAnd, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.BitwiseAndAssign, index, line, col, 2); } return factory.create(TokenType.BitwiseAnd, index, line, col); case '|': switch (next) { case '|': in_stream.read(); return factory.create(TokenType.LogicalOr, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.BitwiseOrAssign, index, line, col, 2); } return factory.create(TokenType.BitwiseOr, index, line, col); case '^': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.XorAssign, index, line, col, 2); case '^': in_stream.read(); if (in_stream.peek() == '=') { in_stream.read(); return factory.create(TokenType.PowAssign, index, line, col, 3); } return factory.create(TokenType.Pow, index, line, col, 2); } return factory.create(TokenType.Xor, index, line, col); case '!': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.NotEqual, index, line, col, 2); case '<': in_stream.read(); switch (in_stream.peek()) { case '=': in_stream.read(); return factory.create(TokenType.UnorderedOrGreater, index, line, col, 3); case '>': in_stream.read(); switch (in_stream.peek()) { case '=': in_stream.read(); return factory.create(TokenType.Unordered, index, line, col, 4); } return factory.create(TokenType.UnorderedOrEqual, index, line, col, 3); } return factory.create(TokenType.UnorderedGreaterOrEqual, index, line, col, 2); case '>': in_stream.read(); switch (in_stream.peek()) { case '=': in_stream.read(); return factory.create(TokenType.UnorderedOrLess, index, line, col, 3); default: break; } return factory.create(TokenType.UnorderedLessOrEqual, index, line, col, 2); } return factory.create(TokenType.Not, index, line, col); case '~': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.TildeAssign, index, line, col, 2); } return factory.create(TokenType.Tilde, index, line, col); case '=': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.Equal, index, line, col, 2); case '>': in_stream.read(); return factory.create(TokenType.GoesTo, index, line, col, 2); } return factory.create(TokenType.Assign, index, line, col); case '<': switch (next) { case '<': in_stream.read(); switch (in_stream.peek()) { case '=': in_stream.read(); return factory.create(TokenType.ShiftLeftAssign, index, line, col, 3); default: break; } return factory.create(TokenType.ShiftLeft, index, line, col, 2); case '>': in_stream.read(); switch (in_stream.peek()) { case '=': in_stream.read(); return factory.create(TokenType.LessEqualOrGreater, index, line, col, 3); default: break; } return factory.create(TokenType.LessOrGreater, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.LessEqual, index, line, col, 2); } return factory.create(TokenType.LessThan, index, line, col); case '>': switch (next) { case '>': in_stream.read(); int p = in_stream.peek(); if (p != -1) { switch ((char)p) { case '=': in_stream.read(); return factory.create(TokenType.ShiftRightAssign, index, line, col, 3); case '>': in_stream.read(); int q = in_stream.peek(); if (q != -1 && q == '=') { in_stream.read(); return factory.create(TokenType.TripleRightShiftAssign, index, line, col, 4); } return factory.create(TokenType.ShiftRightUnsigned, index, line, col, 3); } } return factory.create(TokenType.ShiftRight, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.GreaterEqual, index, line, col, 2); } return factory.create(TokenType.GreaterThan, index, line, col); case '?': return factory.create(TokenType.Question, index, line, col); case '$': return factory.create(TokenType.Dollar, index, line, col); case ';': return factory.create(TokenType.Semicolon, index, line, col); case ':': return factory.create(TokenType.Colon, index, line, col); case ',': return factory.create(TokenType.Comma, index, line, col); case '.': if (next == '.') { in_stream.read(); int p = in_stream.peek(); if (p != -1 && p == '.') { in_stream.read(); return factory.create(TokenType.TripleDot, index, line, col, 3); } return factory.create(TokenType.DoubleDot, index, line, col, 2); } return factory.create(TokenType.Dot, index, line, col); case ')': return factory.create(TokenType.CloseParenthesis, index, line, col); case '(': return factory.create(TokenType.OpenParenthesis, index, line, col); case ']': return factory.create(TokenType.CloseSquareBracket, index, line, col); case '[': return factory.create(TokenType.OpenSquareBracket, index, line, col); case '}': return factory.create(TokenType.CloseCurlyBrace, index, line, col); case '{': return factory.create(TokenType.OpenCurlyBrace, index, line, col); case '#': return factory.create(TokenType.Hash, index, line, col); default: return null; } } }
LexOperator { public Token read() throws IOException, LexerException { int index = in_stream.getPosition(); int line = in_stream.getLine(); int col = in_stream.getCol(); int next = in_stream.peek(); if (next == -1) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Unexpected end of input stream when parsing operator"); } if (next == '.') { int tmp = in_stream.peek(2); if (tmp > 0 && Character.isDigit(tmp)) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Literal float encountered when operator '.' expected"); } } char cur = (char)in_stream.read(); next = in_stream.peek(); switch (cur) { case '+': switch (next) { case '+': in_stream.read(); return factory.create(TokenType.Increment, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.PlusAssign, index, line, col, 2); } return factory.create(TokenType.Plus, index, line, col); case '-': switch (next) { case '-': in_stream.read(); return factory.create(TokenType.Decrement, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.MinusAssign, index, line, col, 2); } return factory.create(TokenType.Minus, index, line, col); case '*': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.TimesAssign, index, line, col, 2); default: break; } return factory.create(TokenType.Times, index, line, col); case '/': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.DivAssign, index, line, col, 2); } return factory.create(TokenType.Div, index, line, col); case '%': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.ModAssign, index, line, col, 2); } return factory.create(TokenType.Mod, index, line, col); case '&': switch (next) { case '&': in_stream.read(); return factory.create(TokenType.LogicalAnd, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.BitwiseAndAssign, index, line, col, 2); } return factory.create(TokenType.BitwiseAnd, index, line, col); case '|': switch (next) { case '|': in_stream.read(); return factory.create(TokenType.LogicalOr, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.BitwiseOrAssign, index, line, col, 2); } return factory.create(TokenType.BitwiseOr, index, line, col); case '^': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.XorAssign, index, line, col, 2); case '^': in_stream.read(); if (in_stream.peek() == '=') { in_stream.read(); return factory.create(TokenType.PowAssign, index, line, col, 3); } return factory.create(TokenType.Pow, index, line, col, 2); } return factory.create(TokenType.Xor, index, line, col); case '!': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.NotEqual, index, line, col, 2); case '<': in_stream.read(); switch (in_stream.peek()) { case '=': in_stream.read(); return factory.create(TokenType.UnorderedOrGreater, index, line, col, 3); case '>': in_stream.read(); switch (in_stream.peek()) { case '=': in_stream.read(); return factory.create(TokenType.Unordered, index, line, col, 4); } return factory.create(TokenType.UnorderedOrEqual, index, line, col, 3); } return factory.create(TokenType.UnorderedGreaterOrEqual, index, line, col, 2); case '>': in_stream.read(); switch (in_stream.peek()) { case '=': in_stream.read(); return factory.create(TokenType.UnorderedOrLess, index, line, col, 3); default: break; } return factory.create(TokenType.UnorderedLessOrEqual, index, line, col, 2); } return factory.create(TokenType.Not, index, line, col); case '~': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.TildeAssign, index, line, col, 2); } return factory.create(TokenType.Tilde, index, line, col); case '=': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.Equal, index, line, col, 2); case '>': in_stream.read(); return factory.create(TokenType.GoesTo, index, line, col, 2); } return factory.create(TokenType.Assign, index, line, col); case '<': switch (next) { case '<': in_stream.read(); switch (in_stream.peek()) { case '=': in_stream.read(); return factory.create(TokenType.ShiftLeftAssign, index, line, col, 3); default: break; } return factory.create(TokenType.ShiftLeft, index, line, col, 2); case '>': in_stream.read(); switch (in_stream.peek()) { case '=': in_stream.read(); return factory.create(TokenType.LessEqualOrGreater, index, line, col, 3); default: break; } return factory.create(TokenType.LessOrGreater, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.LessEqual, index, line, col, 2); } return factory.create(TokenType.LessThan, index, line, col); case '>': switch (next) { case '>': in_stream.read(); int p = in_stream.peek(); if (p != -1) { switch ((char)p) { case '=': in_stream.read(); return factory.create(TokenType.ShiftRightAssign, index, line, col, 3); case '>': in_stream.read(); int q = in_stream.peek(); if (q != -1 && q == '=') { in_stream.read(); return factory.create(TokenType.TripleRightShiftAssign, index, line, col, 4); } return factory.create(TokenType.ShiftRightUnsigned, index, line, col, 3); } } return factory.create(TokenType.ShiftRight, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.GreaterEqual, index, line, col, 2); } return factory.create(TokenType.GreaterThan, index, line, col); case '?': return factory.create(TokenType.Question, index, line, col); case '$': return factory.create(TokenType.Dollar, index, line, col); case ';': return factory.create(TokenType.Semicolon, index, line, col); case ':': return factory.create(TokenType.Colon, index, line, col); case ',': return factory.create(TokenType.Comma, index, line, col); case '.': if (next == '.') { in_stream.read(); int p = in_stream.peek(); if (p != -1 && p == '.') { in_stream.read(); return factory.create(TokenType.TripleDot, index, line, col, 3); } return factory.create(TokenType.DoubleDot, index, line, col, 2); } return factory.create(TokenType.Dot, index, line, col); case ')': return factory.create(TokenType.CloseParenthesis, index, line, col); case '(': return factory.create(TokenType.OpenParenthesis, index, line, col); case ']': return factory.create(TokenType.CloseSquareBracket, index, line, col); case '[': return factory.create(TokenType.OpenSquareBracket, index, line, col); case '}': return factory.create(TokenType.CloseCurlyBrace, index, line, col); case '{': return factory.create(TokenType.OpenCurlyBrace, index, line, col); case '#': return factory.create(TokenType.Hash, index, line, col); default: return null; } } LexOperator(TokenFactory factory, LexerStream in_stream); }
LexOperator { public Token read() throws IOException, LexerException { int index = in_stream.getPosition(); int line = in_stream.getLine(); int col = in_stream.getCol(); int next = in_stream.peek(); if (next == -1) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Unexpected end of input stream when parsing operator"); } if (next == '.') { int tmp = in_stream.peek(2); if (tmp > 0 && Character.isDigit(tmp)) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Literal float encountered when operator '.' expected"); } } char cur = (char)in_stream.read(); next = in_stream.peek(); switch (cur) { case '+': switch (next) { case '+': in_stream.read(); return factory.create(TokenType.Increment, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.PlusAssign, index, line, col, 2); } return factory.create(TokenType.Plus, index, line, col); case '-': switch (next) { case '-': in_stream.read(); return factory.create(TokenType.Decrement, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.MinusAssign, index, line, col, 2); } return factory.create(TokenType.Minus, index, line, col); case '*': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.TimesAssign, index, line, col, 2); default: break; } return factory.create(TokenType.Times, index, line, col); case '/': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.DivAssign, index, line, col, 2); } return factory.create(TokenType.Div, index, line, col); case '%': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.ModAssign, index, line, col, 2); } return factory.create(TokenType.Mod, index, line, col); case '&': switch (next) { case '&': in_stream.read(); return factory.create(TokenType.LogicalAnd, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.BitwiseAndAssign, index, line, col, 2); } return factory.create(TokenType.BitwiseAnd, index, line, col); case '|': switch (next) { case '|': in_stream.read(); return factory.create(TokenType.LogicalOr, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.BitwiseOrAssign, index, line, col, 2); } return factory.create(TokenType.BitwiseOr, index, line, col); case '^': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.XorAssign, index, line, col, 2); case '^': in_stream.read(); if (in_stream.peek() == '=') { in_stream.read(); return factory.create(TokenType.PowAssign, index, line, col, 3); } return factory.create(TokenType.Pow, index, line, col, 2); } return factory.create(TokenType.Xor, index, line, col); case '!': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.NotEqual, index, line, col, 2); case '<': in_stream.read(); switch (in_stream.peek()) { case '=': in_stream.read(); return factory.create(TokenType.UnorderedOrGreater, index, line, col, 3); case '>': in_stream.read(); switch (in_stream.peek()) { case '=': in_stream.read(); return factory.create(TokenType.Unordered, index, line, col, 4); } return factory.create(TokenType.UnorderedOrEqual, index, line, col, 3); } return factory.create(TokenType.UnorderedGreaterOrEqual, index, line, col, 2); case '>': in_stream.read(); switch (in_stream.peek()) { case '=': in_stream.read(); return factory.create(TokenType.UnorderedOrLess, index, line, col, 3); default: break; } return factory.create(TokenType.UnorderedLessOrEqual, index, line, col, 2); } return factory.create(TokenType.Not, index, line, col); case '~': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.TildeAssign, index, line, col, 2); } return factory.create(TokenType.Tilde, index, line, col); case '=': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.Equal, index, line, col, 2); case '>': in_stream.read(); return factory.create(TokenType.GoesTo, index, line, col, 2); } return factory.create(TokenType.Assign, index, line, col); case '<': switch (next) { case '<': in_stream.read(); switch (in_stream.peek()) { case '=': in_stream.read(); return factory.create(TokenType.ShiftLeftAssign, index, line, col, 3); default: break; } return factory.create(TokenType.ShiftLeft, index, line, col, 2); case '>': in_stream.read(); switch (in_stream.peek()) { case '=': in_stream.read(); return factory.create(TokenType.LessEqualOrGreater, index, line, col, 3); default: break; } return factory.create(TokenType.LessOrGreater, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.LessEqual, index, line, col, 2); } return factory.create(TokenType.LessThan, index, line, col); case '>': switch (next) { case '>': in_stream.read(); int p = in_stream.peek(); if (p != -1) { switch ((char)p) { case '=': in_stream.read(); return factory.create(TokenType.ShiftRightAssign, index, line, col, 3); case '>': in_stream.read(); int q = in_stream.peek(); if (q != -1 && q == '=') { in_stream.read(); return factory.create(TokenType.TripleRightShiftAssign, index, line, col, 4); } return factory.create(TokenType.ShiftRightUnsigned, index, line, col, 3); } } return factory.create(TokenType.ShiftRight, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.GreaterEqual, index, line, col, 2); } return factory.create(TokenType.GreaterThan, index, line, col); case '?': return factory.create(TokenType.Question, index, line, col); case '$': return factory.create(TokenType.Dollar, index, line, col); case ';': return factory.create(TokenType.Semicolon, index, line, col); case ':': return factory.create(TokenType.Colon, index, line, col); case ',': return factory.create(TokenType.Comma, index, line, col); case '.': if (next == '.') { in_stream.read(); int p = in_stream.peek(); if (p != -1 && p == '.') { in_stream.read(); return factory.create(TokenType.TripleDot, index, line, col, 3); } return factory.create(TokenType.DoubleDot, index, line, col, 2); } return factory.create(TokenType.Dot, index, line, col); case ')': return factory.create(TokenType.CloseParenthesis, index, line, col); case '(': return factory.create(TokenType.OpenParenthesis, index, line, col); case ']': return factory.create(TokenType.CloseSquareBracket, index, line, col); case '[': return factory.create(TokenType.OpenSquareBracket, index, line, col); case '}': return factory.create(TokenType.CloseCurlyBrace, index, line, col); case '{': return factory.create(TokenType.OpenCurlyBrace, index, line, col); case '#': return factory.create(TokenType.Hash, index, line, col); default: return null; } } LexOperator(TokenFactory factory, LexerStream in_stream); Token read(); }
LexOperator { public Token read() throws IOException, LexerException { int index = in_stream.getPosition(); int line = in_stream.getLine(); int col = in_stream.getCol(); int next = in_stream.peek(); if (next == -1) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Unexpected end of input stream when parsing operator"); } if (next == '.') { int tmp = in_stream.peek(2); if (tmp > 0 && Character.isDigit(tmp)) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Literal float encountered when operator '.' expected"); } } char cur = (char)in_stream.read(); next = in_stream.peek(); switch (cur) { case '+': switch (next) { case '+': in_stream.read(); return factory.create(TokenType.Increment, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.PlusAssign, index, line, col, 2); } return factory.create(TokenType.Plus, index, line, col); case '-': switch (next) { case '-': in_stream.read(); return factory.create(TokenType.Decrement, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.MinusAssign, index, line, col, 2); } return factory.create(TokenType.Minus, index, line, col); case '*': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.TimesAssign, index, line, col, 2); default: break; } return factory.create(TokenType.Times, index, line, col); case '/': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.DivAssign, index, line, col, 2); } return factory.create(TokenType.Div, index, line, col); case '%': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.ModAssign, index, line, col, 2); } return factory.create(TokenType.Mod, index, line, col); case '&': switch (next) { case '&': in_stream.read(); return factory.create(TokenType.LogicalAnd, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.BitwiseAndAssign, index, line, col, 2); } return factory.create(TokenType.BitwiseAnd, index, line, col); case '|': switch (next) { case '|': in_stream.read(); return factory.create(TokenType.LogicalOr, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.BitwiseOrAssign, index, line, col, 2); } return factory.create(TokenType.BitwiseOr, index, line, col); case '^': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.XorAssign, index, line, col, 2); case '^': in_stream.read(); if (in_stream.peek() == '=') { in_stream.read(); return factory.create(TokenType.PowAssign, index, line, col, 3); } return factory.create(TokenType.Pow, index, line, col, 2); } return factory.create(TokenType.Xor, index, line, col); case '!': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.NotEqual, index, line, col, 2); case '<': in_stream.read(); switch (in_stream.peek()) { case '=': in_stream.read(); return factory.create(TokenType.UnorderedOrGreater, index, line, col, 3); case '>': in_stream.read(); switch (in_stream.peek()) { case '=': in_stream.read(); return factory.create(TokenType.Unordered, index, line, col, 4); } return factory.create(TokenType.UnorderedOrEqual, index, line, col, 3); } return factory.create(TokenType.UnorderedGreaterOrEqual, index, line, col, 2); case '>': in_stream.read(); switch (in_stream.peek()) { case '=': in_stream.read(); return factory.create(TokenType.UnorderedOrLess, index, line, col, 3); default: break; } return factory.create(TokenType.UnorderedLessOrEqual, index, line, col, 2); } return factory.create(TokenType.Not, index, line, col); case '~': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.TildeAssign, index, line, col, 2); } return factory.create(TokenType.Tilde, index, line, col); case '=': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.Equal, index, line, col, 2); case '>': in_stream.read(); return factory.create(TokenType.GoesTo, index, line, col, 2); } return factory.create(TokenType.Assign, index, line, col); case '<': switch (next) { case '<': in_stream.read(); switch (in_stream.peek()) { case '=': in_stream.read(); return factory.create(TokenType.ShiftLeftAssign, index, line, col, 3); default: break; } return factory.create(TokenType.ShiftLeft, index, line, col, 2); case '>': in_stream.read(); switch (in_stream.peek()) { case '=': in_stream.read(); return factory.create(TokenType.LessEqualOrGreater, index, line, col, 3); default: break; } return factory.create(TokenType.LessOrGreater, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.LessEqual, index, line, col, 2); } return factory.create(TokenType.LessThan, index, line, col); case '>': switch (next) { case '>': in_stream.read(); int p = in_stream.peek(); if (p != -1) { switch ((char)p) { case '=': in_stream.read(); return factory.create(TokenType.ShiftRightAssign, index, line, col, 3); case '>': in_stream.read(); int q = in_stream.peek(); if (q != -1 && q == '=') { in_stream.read(); return factory.create(TokenType.TripleRightShiftAssign, index, line, col, 4); } return factory.create(TokenType.ShiftRightUnsigned, index, line, col, 3); } } return factory.create(TokenType.ShiftRight, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.GreaterEqual, index, line, col, 2); } return factory.create(TokenType.GreaterThan, index, line, col); case '?': return factory.create(TokenType.Question, index, line, col); case '$': return factory.create(TokenType.Dollar, index, line, col); case ';': return factory.create(TokenType.Semicolon, index, line, col); case ':': return factory.create(TokenType.Colon, index, line, col); case ',': return factory.create(TokenType.Comma, index, line, col); case '.': if (next == '.') { in_stream.read(); int p = in_stream.peek(); if (p != -1 && p == '.') { in_stream.read(); return factory.create(TokenType.TripleDot, index, line, col, 3); } return factory.create(TokenType.DoubleDot, index, line, col, 2); } return factory.create(TokenType.Dot, index, line, col); case ')': return factory.create(TokenType.CloseParenthesis, index, line, col); case '(': return factory.create(TokenType.OpenParenthesis, index, line, col); case ']': return factory.create(TokenType.CloseSquareBracket, index, line, col); case '[': return factory.create(TokenType.OpenSquareBracket, index, line, col); case '}': return factory.create(TokenType.CloseCurlyBrace, index, line, col); case '{': return factory.create(TokenType.OpenCurlyBrace, index, line, col); case '#': return factory.create(TokenType.Hash, index, line, col); default: return null; } } LexOperator(TokenFactory factory, LexerStream in_stream); Token read(); }
@Test public void equality() throws Exception { String str = "a==b != c"; LexerStream ls = new LexerStream(new StringReader(str)); LexOperator lex = new LexOperator(new BaseTokenFactory(), ls); ls.read(); Token tok = lex.read(); assertEquals(TokenType.Equal, tok.getType()); assertEquals("==", tok.getValue()); assertEquals(1, tok.getLine()); assertEquals(2, tok.getCol()); assertEquals(1, tok.getEndLine()); assertEquals(4, tok.getEndCol()); ls.read(); ls.read(); tok = lex.read(); assertEquals(TokenType.NotEqual, tok.getType()); assertEquals("!=", tok.getValue()); assertEquals(1, tok.getLine()); assertEquals(6, tok.getCol()); assertEquals(1, tok.getEndLine()); assertEquals(8, tok.getEndCol()); }
public Token read() throws IOException, LexerException { int index = in_stream.getPosition(); int line = in_stream.getLine(); int col = in_stream.getCol(); int next = in_stream.peek(); if (next == -1) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Unexpected end of input stream when parsing operator"); } if (next == '.') { int tmp = in_stream.peek(2); if (tmp > 0 && Character.isDigit(tmp)) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Literal float encountered when operator '.' expected"); } } char cur = (char)in_stream.read(); next = in_stream.peek(); switch (cur) { case '+': switch (next) { case '+': in_stream.read(); return factory.create(TokenType.Increment, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.PlusAssign, index, line, col, 2); } return factory.create(TokenType.Plus, index, line, col); case '-': switch (next) { case '-': in_stream.read(); return factory.create(TokenType.Decrement, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.MinusAssign, index, line, col, 2); } return factory.create(TokenType.Minus, index, line, col); case '*': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.TimesAssign, index, line, col, 2); default: break; } return factory.create(TokenType.Times, index, line, col); case '/': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.DivAssign, index, line, col, 2); } return factory.create(TokenType.Div, index, line, col); case '%': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.ModAssign, index, line, col, 2); } return factory.create(TokenType.Mod, index, line, col); case '&': switch (next) { case '&': in_stream.read(); return factory.create(TokenType.LogicalAnd, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.BitwiseAndAssign, index, line, col, 2); } return factory.create(TokenType.BitwiseAnd, index, line, col); case '|': switch (next) { case '|': in_stream.read(); return factory.create(TokenType.LogicalOr, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.BitwiseOrAssign, index, line, col, 2); } return factory.create(TokenType.BitwiseOr, index, line, col); case '^': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.XorAssign, index, line, col, 2); case '^': in_stream.read(); if (in_stream.peek() == '=') { in_stream.read(); return factory.create(TokenType.PowAssign, index, line, col, 3); } return factory.create(TokenType.Pow, index, line, col, 2); } return factory.create(TokenType.Xor, index, line, col); case '!': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.NotEqual, index, line, col, 2); case '<': in_stream.read(); switch (in_stream.peek()) { case '=': in_stream.read(); return factory.create(TokenType.UnorderedOrGreater, index, line, col, 3); case '>': in_stream.read(); switch (in_stream.peek()) { case '=': in_stream.read(); return factory.create(TokenType.Unordered, index, line, col, 4); } return factory.create(TokenType.UnorderedOrEqual, index, line, col, 3); } return factory.create(TokenType.UnorderedGreaterOrEqual, index, line, col, 2); case '>': in_stream.read(); switch (in_stream.peek()) { case '=': in_stream.read(); return factory.create(TokenType.UnorderedOrLess, index, line, col, 3); default: break; } return factory.create(TokenType.UnorderedLessOrEqual, index, line, col, 2); } return factory.create(TokenType.Not, index, line, col); case '~': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.TildeAssign, index, line, col, 2); } return factory.create(TokenType.Tilde, index, line, col); case '=': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.Equal, index, line, col, 2); case '>': in_stream.read(); return factory.create(TokenType.GoesTo, index, line, col, 2); } return factory.create(TokenType.Assign, index, line, col); case '<': switch (next) { case '<': in_stream.read(); switch (in_stream.peek()) { case '=': in_stream.read(); return factory.create(TokenType.ShiftLeftAssign, index, line, col, 3); default: break; } return factory.create(TokenType.ShiftLeft, index, line, col, 2); case '>': in_stream.read(); switch (in_stream.peek()) { case '=': in_stream.read(); return factory.create(TokenType.LessEqualOrGreater, index, line, col, 3); default: break; } return factory.create(TokenType.LessOrGreater, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.LessEqual, index, line, col, 2); } return factory.create(TokenType.LessThan, index, line, col); case '>': switch (next) { case '>': in_stream.read(); int p = in_stream.peek(); if (p != -1) { switch ((char)p) { case '=': in_stream.read(); return factory.create(TokenType.ShiftRightAssign, index, line, col, 3); case '>': in_stream.read(); int q = in_stream.peek(); if (q != -1 && q == '=') { in_stream.read(); return factory.create(TokenType.TripleRightShiftAssign, index, line, col, 4); } return factory.create(TokenType.ShiftRightUnsigned, index, line, col, 3); } } return factory.create(TokenType.ShiftRight, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.GreaterEqual, index, line, col, 2); } return factory.create(TokenType.GreaterThan, index, line, col); case '?': return factory.create(TokenType.Question, index, line, col); case '$': return factory.create(TokenType.Dollar, index, line, col); case ';': return factory.create(TokenType.Semicolon, index, line, col); case ':': return factory.create(TokenType.Colon, index, line, col); case ',': return factory.create(TokenType.Comma, index, line, col); case '.': if (next == '.') { in_stream.read(); int p = in_stream.peek(); if (p != -1 && p == '.') { in_stream.read(); return factory.create(TokenType.TripleDot, index, line, col, 3); } return factory.create(TokenType.DoubleDot, index, line, col, 2); } return factory.create(TokenType.Dot, index, line, col); case ')': return factory.create(TokenType.CloseParenthesis, index, line, col); case '(': return factory.create(TokenType.OpenParenthesis, index, line, col); case ']': return factory.create(TokenType.CloseSquareBracket, index, line, col); case '[': return factory.create(TokenType.OpenSquareBracket, index, line, col); case '}': return factory.create(TokenType.CloseCurlyBrace, index, line, col); case '{': return factory.create(TokenType.OpenCurlyBrace, index, line, col); case '#': return factory.create(TokenType.Hash, index, line, col); default: return null; } }
LexOperator { public Token read() throws IOException, LexerException { int index = in_stream.getPosition(); int line = in_stream.getLine(); int col = in_stream.getCol(); int next = in_stream.peek(); if (next == -1) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Unexpected end of input stream when parsing operator"); } if (next == '.') { int tmp = in_stream.peek(2); if (tmp > 0 && Character.isDigit(tmp)) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Literal float encountered when operator '.' expected"); } } char cur = (char)in_stream.read(); next = in_stream.peek(); switch (cur) { case '+': switch (next) { case '+': in_stream.read(); return factory.create(TokenType.Increment, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.PlusAssign, index, line, col, 2); } return factory.create(TokenType.Plus, index, line, col); case '-': switch (next) { case '-': in_stream.read(); return factory.create(TokenType.Decrement, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.MinusAssign, index, line, col, 2); } return factory.create(TokenType.Minus, index, line, col); case '*': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.TimesAssign, index, line, col, 2); default: break; } return factory.create(TokenType.Times, index, line, col); case '/': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.DivAssign, index, line, col, 2); } return factory.create(TokenType.Div, index, line, col); case '%': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.ModAssign, index, line, col, 2); } return factory.create(TokenType.Mod, index, line, col); case '&': switch (next) { case '&': in_stream.read(); return factory.create(TokenType.LogicalAnd, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.BitwiseAndAssign, index, line, col, 2); } return factory.create(TokenType.BitwiseAnd, index, line, col); case '|': switch (next) { case '|': in_stream.read(); return factory.create(TokenType.LogicalOr, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.BitwiseOrAssign, index, line, col, 2); } return factory.create(TokenType.BitwiseOr, index, line, col); case '^': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.XorAssign, index, line, col, 2); case '^': in_stream.read(); if (in_stream.peek() == '=') { in_stream.read(); return factory.create(TokenType.PowAssign, index, line, col, 3); } return factory.create(TokenType.Pow, index, line, col, 2); } return factory.create(TokenType.Xor, index, line, col); case '!': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.NotEqual, index, line, col, 2); case '<': in_stream.read(); switch (in_stream.peek()) { case '=': in_stream.read(); return factory.create(TokenType.UnorderedOrGreater, index, line, col, 3); case '>': in_stream.read(); switch (in_stream.peek()) { case '=': in_stream.read(); return factory.create(TokenType.Unordered, index, line, col, 4); } return factory.create(TokenType.UnorderedOrEqual, index, line, col, 3); } return factory.create(TokenType.UnorderedGreaterOrEqual, index, line, col, 2); case '>': in_stream.read(); switch (in_stream.peek()) { case '=': in_stream.read(); return factory.create(TokenType.UnorderedOrLess, index, line, col, 3); default: break; } return factory.create(TokenType.UnorderedLessOrEqual, index, line, col, 2); } return factory.create(TokenType.Not, index, line, col); case '~': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.TildeAssign, index, line, col, 2); } return factory.create(TokenType.Tilde, index, line, col); case '=': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.Equal, index, line, col, 2); case '>': in_stream.read(); return factory.create(TokenType.GoesTo, index, line, col, 2); } return factory.create(TokenType.Assign, index, line, col); case '<': switch (next) { case '<': in_stream.read(); switch (in_stream.peek()) { case '=': in_stream.read(); return factory.create(TokenType.ShiftLeftAssign, index, line, col, 3); default: break; } return factory.create(TokenType.ShiftLeft, index, line, col, 2); case '>': in_stream.read(); switch (in_stream.peek()) { case '=': in_stream.read(); return factory.create(TokenType.LessEqualOrGreater, index, line, col, 3); default: break; } return factory.create(TokenType.LessOrGreater, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.LessEqual, index, line, col, 2); } return factory.create(TokenType.LessThan, index, line, col); case '>': switch (next) { case '>': in_stream.read(); int p = in_stream.peek(); if (p != -1) { switch ((char)p) { case '=': in_stream.read(); return factory.create(TokenType.ShiftRightAssign, index, line, col, 3); case '>': in_stream.read(); int q = in_stream.peek(); if (q != -1 && q == '=') { in_stream.read(); return factory.create(TokenType.TripleRightShiftAssign, index, line, col, 4); } return factory.create(TokenType.ShiftRightUnsigned, index, line, col, 3); } } return factory.create(TokenType.ShiftRight, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.GreaterEqual, index, line, col, 2); } return factory.create(TokenType.GreaterThan, index, line, col); case '?': return factory.create(TokenType.Question, index, line, col); case '$': return factory.create(TokenType.Dollar, index, line, col); case ';': return factory.create(TokenType.Semicolon, index, line, col); case ':': return factory.create(TokenType.Colon, index, line, col); case ',': return factory.create(TokenType.Comma, index, line, col); case '.': if (next == '.') { in_stream.read(); int p = in_stream.peek(); if (p != -1 && p == '.') { in_stream.read(); return factory.create(TokenType.TripleDot, index, line, col, 3); } return factory.create(TokenType.DoubleDot, index, line, col, 2); } return factory.create(TokenType.Dot, index, line, col); case ')': return factory.create(TokenType.CloseParenthesis, index, line, col); case '(': return factory.create(TokenType.OpenParenthesis, index, line, col); case ']': return factory.create(TokenType.CloseSquareBracket, index, line, col); case '[': return factory.create(TokenType.OpenSquareBracket, index, line, col); case '}': return factory.create(TokenType.CloseCurlyBrace, index, line, col); case '{': return factory.create(TokenType.OpenCurlyBrace, index, line, col); case '#': return factory.create(TokenType.Hash, index, line, col); default: return null; } } }
LexOperator { public Token read() throws IOException, LexerException { int index = in_stream.getPosition(); int line = in_stream.getLine(); int col = in_stream.getCol(); int next = in_stream.peek(); if (next == -1) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Unexpected end of input stream when parsing operator"); } if (next == '.') { int tmp = in_stream.peek(2); if (tmp > 0 && Character.isDigit(tmp)) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Literal float encountered when operator '.' expected"); } } char cur = (char)in_stream.read(); next = in_stream.peek(); switch (cur) { case '+': switch (next) { case '+': in_stream.read(); return factory.create(TokenType.Increment, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.PlusAssign, index, line, col, 2); } return factory.create(TokenType.Plus, index, line, col); case '-': switch (next) { case '-': in_stream.read(); return factory.create(TokenType.Decrement, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.MinusAssign, index, line, col, 2); } return factory.create(TokenType.Minus, index, line, col); case '*': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.TimesAssign, index, line, col, 2); default: break; } return factory.create(TokenType.Times, index, line, col); case '/': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.DivAssign, index, line, col, 2); } return factory.create(TokenType.Div, index, line, col); case '%': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.ModAssign, index, line, col, 2); } return factory.create(TokenType.Mod, index, line, col); case '&': switch (next) { case '&': in_stream.read(); return factory.create(TokenType.LogicalAnd, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.BitwiseAndAssign, index, line, col, 2); } return factory.create(TokenType.BitwiseAnd, index, line, col); case '|': switch (next) { case '|': in_stream.read(); return factory.create(TokenType.LogicalOr, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.BitwiseOrAssign, index, line, col, 2); } return factory.create(TokenType.BitwiseOr, index, line, col); case '^': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.XorAssign, index, line, col, 2); case '^': in_stream.read(); if (in_stream.peek() == '=') { in_stream.read(); return factory.create(TokenType.PowAssign, index, line, col, 3); } return factory.create(TokenType.Pow, index, line, col, 2); } return factory.create(TokenType.Xor, index, line, col); case '!': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.NotEqual, index, line, col, 2); case '<': in_stream.read(); switch (in_stream.peek()) { case '=': in_stream.read(); return factory.create(TokenType.UnorderedOrGreater, index, line, col, 3); case '>': in_stream.read(); switch (in_stream.peek()) { case '=': in_stream.read(); return factory.create(TokenType.Unordered, index, line, col, 4); } return factory.create(TokenType.UnorderedOrEqual, index, line, col, 3); } return factory.create(TokenType.UnorderedGreaterOrEqual, index, line, col, 2); case '>': in_stream.read(); switch (in_stream.peek()) { case '=': in_stream.read(); return factory.create(TokenType.UnorderedOrLess, index, line, col, 3); default: break; } return factory.create(TokenType.UnorderedLessOrEqual, index, line, col, 2); } return factory.create(TokenType.Not, index, line, col); case '~': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.TildeAssign, index, line, col, 2); } return factory.create(TokenType.Tilde, index, line, col); case '=': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.Equal, index, line, col, 2); case '>': in_stream.read(); return factory.create(TokenType.GoesTo, index, line, col, 2); } return factory.create(TokenType.Assign, index, line, col); case '<': switch (next) { case '<': in_stream.read(); switch (in_stream.peek()) { case '=': in_stream.read(); return factory.create(TokenType.ShiftLeftAssign, index, line, col, 3); default: break; } return factory.create(TokenType.ShiftLeft, index, line, col, 2); case '>': in_stream.read(); switch (in_stream.peek()) { case '=': in_stream.read(); return factory.create(TokenType.LessEqualOrGreater, index, line, col, 3); default: break; } return factory.create(TokenType.LessOrGreater, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.LessEqual, index, line, col, 2); } return factory.create(TokenType.LessThan, index, line, col); case '>': switch (next) { case '>': in_stream.read(); int p = in_stream.peek(); if (p != -1) { switch ((char)p) { case '=': in_stream.read(); return factory.create(TokenType.ShiftRightAssign, index, line, col, 3); case '>': in_stream.read(); int q = in_stream.peek(); if (q != -1 && q == '=') { in_stream.read(); return factory.create(TokenType.TripleRightShiftAssign, index, line, col, 4); } return factory.create(TokenType.ShiftRightUnsigned, index, line, col, 3); } } return factory.create(TokenType.ShiftRight, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.GreaterEqual, index, line, col, 2); } return factory.create(TokenType.GreaterThan, index, line, col); case '?': return factory.create(TokenType.Question, index, line, col); case '$': return factory.create(TokenType.Dollar, index, line, col); case ';': return factory.create(TokenType.Semicolon, index, line, col); case ':': return factory.create(TokenType.Colon, index, line, col); case ',': return factory.create(TokenType.Comma, index, line, col); case '.': if (next == '.') { in_stream.read(); int p = in_stream.peek(); if (p != -1 && p == '.') { in_stream.read(); return factory.create(TokenType.TripleDot, index, line, col, 3); } return factory.create(TokenType.DoubleDot, index, line, col, 2); } return factory.create(TokenType.Dot, index, line, col); case ')': return factory.create(TokenType.CloseParenthesis, index, line, col); case '(': return factory.create(TokenType.OpenParenthesis, index, line, col); case ']': return factory.create(TokenType.CloseSquareBracket, index, line, col); case '[': return factory.create(TokenType.OpenSquareBracket, index, line, col); case '}': return factory.create(TokenType.CloseCurlyBrace, index, line, col); case '{': return factory.create(TokenType.OpenCurlyBrace, index, line, col); case '#': return factory.create(TokenType.Hash, index, line, col); default: return null; } } LexOperator(TokenFactory factory, LexerStream in_stream); }
LexOperator { public Token read() throws IOException, LexerException { int index = in_stream.getPosition(); int line = in_stream.getLine(); int col = in_stream.getCol(); int next = in_stream.peek(); if (next == -1) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Unexpected end of input stream when parsing operator"); } if (next == '.') { int tmp = in_stream.peek(2); if (tmp > 0 && Character.isDigit(tmp)) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Literal float encountered when operator '.' expected"); } } char cur = (char)in_stream.read(); next = in_stream.peek(); switch (cur) { case '+': switch (next) { case '+': in_stream.read(); return factory.create(TokenType.Increment, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.PlusAssign, index, line, col, 2); } return factory.create(TokenType.Plus, index, line, col); case '-': switch (next) { case '-': in_stream.read(); return factory.create(TokenType.Decrement, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.MinusAssign, index, line, col, 2); } return factory.create(TokenType.Minus, index, line, col); case '*': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.TimesAssign, index, line, col, 2); default: break; } return factory.create(TokenType.Times, index, line, col); case '/': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.DivAssign, index, line, col, 2); } return factory.create(TokenType.Div, index, line, col); case '%': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.ModAssign, index, line, col, 2); } return factory.create(TokenType.Mod, index, line, col); case '&': switch (next) { case '&': in_stream.read(); return factory.create(TokenType.LogicalAnd, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.BitwiseAndAssign, index, line, col, 2); } return factory.create(TokenType.BitwiseAnd, index, line, col); case '|': switch (next) { case '|': in_stream.read(); return factory.create(TokenType.LogicalOr, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.BitwiseOrAssign, index, line, col, 2); } return factory.create(TokenType.BitwiseOr, index, line, col); case '^': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.XorAssign, index, line, col, 2); case '^': in_stream.read(); if (in_stream.peek() == '=') { in_stream.read(); return factory.create(TokenType.PowAssign, index, line, col, 3); } return factory.create(TokenType.Pow, index, line, col, 2); } return factory.create(TokenType.Xor, index, line, col); case '!': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.NotEqual, index, line, col, 2); case '<': in_stream.read(); switch (in_stream.peek()) { case '=': in_stream.read(); return factory.create(TokenType.UnorderedOrGreater, index, line, col, 3); case '>': in_stream.read(); switch (in_stream.peek()) { case '=': in_stream.read(); return factory.create(TokenType.Unordered, index, line, col, 4); } return factory.create(TokenType.UnorderedOrEqual, index, line, col, 3); } return factory.create(TokenType.UnorderedGreaterOrEqual, index, line, col, 2); case '>': in_stream.read(); switch (in_stream.peek()) { case '=': in_stream.read(); return factory.create(TokenType.UnorderedOrLess, index, line, col, 3); default: break; } return factory.create(TokenType.UnorderedLessOrEqual, index, line, col, 2); } return factory.create(TokenType.Not, index, line, col); case '~': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.TildeAssign, index, line, col, 2); } return factory.create(TokenType.Tilde, index, line, col); case '=': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.Equal, index, line, col, 2); case '>': in_stream.read(); return factory.create(TokenType.GoesTo, index, line, col, 2); } return factory.create(TokenType.Assign, index, line, col); case '<': switch (next) { case '<': in_stream.read(); switch (in_stream.peek()) { case '=': in_stream.read(); return factory.create(TokenType.ShiftLeftAssign, index, line, col, 3); default: break; } return factory.create(TokenType.ShiftLeft, index, line, col, 2); case '>': in_stream.read(); switch (in_stream.peek()) { case '=': in_stream.read(); return factory.create(TokenType.LessEqualOrGreater, index, line, col, 3); default: break; } return factory.create(TokenType.LessOrGreater, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.LessEqual, index, line, col, 2); } return factory.create(TokenType.LessThan, index, line, col); case '>': switch (next) { case '>': in_stream.read(); int p = in_stream.peek(); if (p != -1) { switch ((char)p) { case '=': in_stream.read(); return factory.create(TokenType.ShiftRightAssign, index, line, col, 3); case '>': in_stream.read(); int q = in_stream.peek(); if (q != -1 && q == '=') { in_stream.read(); return factory.create(TokenType.TripleRightShiftAssign, index, line, col, 4); } return factory.create(TokenType.ShiftRightUnsigned, index, line, col, 3); } } return factory.create(TokenType.ShiftRight, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.GreaterEqual, index, line, col, 2); } return factory.create(TokenType.GreaterThan, index, line, col); case '?': return factory.create(TokenType.Question, index, line, col); case '$': return factory.create(TokenType.Dollar, index, line, col); case ';': return factory.create(TokenType.Semicolon, index, line, col); case ':': return factory.create(TokenType.Colon, index, line, col); case ',': return factory.create(TokenType.Comma, index, line, col); case '.': if (next == '.') { in_stream.read(); int p = in_stream.peek(); if (p != -1 && p == '.') { in_stream.read(); return factory.create(TokenType.TripleDot, index, line, col, 3); } return factory.create(TokenType.DoubleDot, index, line, col, 2); } return factory.create(TokenType.Dot, index, line, col); case ')': return factory.create(TokenType.CloseParenthesis, index, line, col); case '(': return factory.create(TokenType.OpenParenthesis, index, line, col); case ']': return factory.create(TokenType.CloseSquareBracket, index, line, col); case '[': return factory.create(TokenType.OpenSquareBracket, index, line, col); case '}': return factory.create(TokenType.CloseCurlyBrace, index, line, col); case '{': return factory.create(TokenType.OpenCurlyBrace, index, line, col); case '#': return factory.create(TokenType.Hash, index, line, col); default: return null; } } LexOperator(TokenFactory factory, LexerStream in_stream); Token read(); }
LexOperator { public Token read() throws IOException, LexerException { int index = in_stream.getPosition(); int line = in_stream.getLine(); int col = in_stream.getCol(); int next = in_stream.peek(); if (next == -1) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Unexpected end of input stream when parsing operator"); } if (next == '.') { int tmp = in_stream.peek(2); if (tmp > 0 && Character.isDigit(tmp)) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Literal float encountered when operator '.' expected"); } } char cur = (char)in_stream.read(); next = in_stream.peek(); switch (cur) { case '+': switch (next) { case '+': in_stream.read(); return factory.create(TokenType.Increment, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.PlusAssign, index, line, col, 2); } return factory.create(TokenType.Plus, index, line, col); case '-': switch (next) { case '-': in_stream.read(); return factory.create(TokenType.Decrement, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.MinusAssign, index, line, col, 2); } return factory.create(TokenType.Minus, index, line, col); case '*': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.TimesAssign, index, line, col, 2); default: break; } return factory.create(TokenType.Times, index, line, col); case '/': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.DivAssign, index, line, col, 2); } return factory.create(TokenType.Div, index, line, col); case '%': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.ModAssign, index, line, col, 2); } return factory.create(TokenType.Mod, index, line, col); case '&': switch (next) { case '&': in_stream.read(); return factory.create(TokenType.LogicalAnd, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.BitwiseAndAssign, index, line, col, 2); } return factory.create(TokenType.BitwiseAnd, index, line, col); case '|': switch (next) { case '|': in_stream.read(); return factory.create(TokenType.LogicalOr, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.BitwiseOrAssign, index, line, col, 2); } return factory.create(TokenType.BitwiseOr, index, line, col); case '^': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.XorAssign, index, line, col, 2); case '^': in_stream.read(); if (in_stream.peek() == '=') { in_stream.read(); return factory.create(TokenType.PowAssign, index, line, col, 3); } return factory.create(TokenType.Pow, index, line, col, 2); } return factory.create(TokenType.Xor, index, line, col); case '!': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.NotEqual, index, line, col, 2); case '<': in_stream.read(); switch (in_stream.peek()) { case '=': in_stream.read(); return factory.create(TokenType.UnorderedOrGreater, index, line, col, 3); case '>': in_stream.read(); switch (in_stream.peek()) { case '=': in_stream.read(); return factory.create(TokenType.Unordered, index, line, col, 4); } return factory.create(TokenType.UnorderedOrEqual, index, line, col, 3); } return factory.create(TokenType.UnorderedGreaterOrEqual, index, line, col, 2); case '>': in_stream.read(); switch (in_stream.peek()) { case '=': in_stream.read(); return factory.create(TokenType.UnorderedOrLess, index, line, col, 3); default: break; } return factory.create(TokenType.UnorderedLessOrEqual, index, line, col, 2); } return factory.create(TokenType.Not, index, line, col); case '~': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.TildeAssign, index, line, col, 2); } return factory.create(TokenType.Tilde, index, line, col); case '=': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.Equal, index, line, col, 2); case '>': in_stream.read(); return factory.create(TokenType.GoesTo, index, line, col, 2); } return factory.create(TokenType.Assign, index, line, col); case '<': switch (next) { case '<': in_stream.read(); switch (in_stream.peek()) { case '=': in_stream.read(); return factory.create(TokenType.ShiftLeftAssign, index, line, col, 3); default: break; } return factory.create(TokenType.ShiftLeft, index, line, col, 2); case '>': in_stream.read(); switch (in_stream.peek()) { case '=': in_stream.read(); return factory.create(TokenType.LessEqualOrGreater, index, line, col, 3); default: break; } return factory.create(TokenType.LessOrGreater, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.LessEqual, index, line, col, 2); } return factory.create(TokenType.LessThan, index, line, col); case '>': switch (next) { case '>': in_stream.read(); int p = in_stream.peek(); if (p != -1) { switch ((char)p) { case '=': in_stream.read(); return factory.create(TokenType.ShiftRightAssign, index, line, col, 3); case '>': in_stream.read(); int q = in_stream.peek(); if (q != -1 && q == '=') { in_stream.read(); return factory.create(TokenType.TripleRightShiftAssign, index, line, col, 4); } return factory.create(TokenType.ShiftRightUnsigned, index, line, col, 3); } } return factory.create(TokenType.ShiftRight, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.GreaterEqual, index, line, col, 2); } return factory.create(TokenType.GreaterThan, index, line, col); case '?': return factory.create(TokenType.Question, index, line, col); case '$': return factory.create(TokenType.Dollar, index, line, col); case ';': return factory.create(TokenType.Semicolon, index, line, col); case ':': return factory.create(TokenType.Colon, index, line, col); case ',': return factory.create(TokenType.Comma, index, line, col); case '.': if (next == '.') { in_stream.read(); int p = in_stream.peek(); if (p != -1 && p == '.') { in_stream.read(); return factory.create(TokenType.TripleDot, index, line, col, 3); } return factory.create(TokenType.DoubleDot, index, line, col, 2); } return factory.create(TokenType.Dot, index, line, col); case ')': return factory.create(TokenType.CloseParenthesis, index, line, col); case '(': return factory.create(TokenType.OpenParenthesis, index, line, col); case ']': return factory.create(TokenType.CloseSquareBracket, index, line, col); case '[': return factory.create(TokenType.OpenSquareBracket, index, line, col); case '}': return factory.create(TokenType.CloseCurlyBrace, index, line, col); case '{': return factory.create(TokenType.OpenCurlyBrace, index, line, col); case '#': return factory.create(TokenType.Hash, index, line, col); default: return null; } } LexOperator(TokenFactory factory, LexerStream in_stream); Token read(); }
@Test public void keywords() throws Exception { StringBuilder words = new StringBuilder(); for (String word : keywords) { words.append(word).append(" "); } LexerStream ls = new LexerStream(new StringReader(words.toString())); LexIdentifier lex = new LexIdentifier(new BaseTokenFactory(), ls); for (String word : keywords) { Token tok = lex.read(); assertEquals(word, tok.getValue()); assertEquals(TokenType.forValue(word), tok.getType()); ls.read(); } assertEquals(-1, ls.read()); }
public Token read() throws IOException, LexerException { int start_index = in_stream.getPosition(); int start_line = in_stream.getLine(); int start_col = in_stream.getCol(); int next = in_stream.peek(); if (next == -1) { throw new LexerException(start_line, start_col, "Unexpected end of input stream when parsing identifier"); } if (!Character.isLetter(next) && next != '_') { throw new LexerException(start_line, start_col, "Invalid character for start of identifier: "+(char)next); } StringBuilder sb = new StringBuilder(); while (valid_ident_char(next)) { sb.append(Character.toChars(in_stream.read())); next = in_stream.peek(); } String result = sb.toString(); TokenType type = TokenType.forValue(result); if (type == null) { type = TokenType.Identifier; } if (type == TokenType.Identifier && result.startsWith("__")) { throw new LexerException(start_line, start_col, "Unknown reserved identifier"); } return factory.create(type, start_index, start_line, start_col, in_stream.getPosition(), in_stream.getLine(), in_stream.getCol(), result); }
LexIdentifier { public Token read() throws IOException, LexerException { int start_index = in_stream.getPosition(); int start_line = in_stream.getLine(); int start_col = in_stream.getCol(); int next = in_stream.peek(); if (next == -1) { throw new LexerException(start_line, start_col, "Unexpected end of input stream when parsing identifier"); } if (!Character.isLetter(next) && next != '_') { throw new LexerException(start_line, start_col, "Invalid character for start of identifier: "+(char)next); } StringBuilder sb = new StringBuilder(); while (valid_ident_char(next)) { sb.append(Character.toChars(in_stream.read())); next = in_stream.peek(); } String result = sb.toString(); TokenType type = TokenType.forValue(result); if (type == null) { type = TokenType.Identifier; } if (type == TokenType.Identifier && result.startsWith("__")) { throw new LexerException(start_line, start_col, "Unknown reserved identifier"); } return factory.create(type, start_index, start_line, start_col, in_stream.getPosition(), in_stream.getLine(), in_stream.getCol(), result); } }
LexIdentifier { public Token read() throws IOException, LexerException { int start_index = in_stream.getPosition(); int start_line = in_stream.getLine(); int start_col = in_stream.getCol(); int next = in_stream.peek(); if (next == -1) { throw new LexerException(start_line, start_col, "Unexpected end of input stream when parsing identifier"); } if (!Character.isLetter(next) && next != '_') { throw new LexerException(start_line, start_col, "Invalid character for start of identifier: "+(char)next); } StringBuilder sb = new StringBuilder(); while (valid_ident_char(next)) { sb.append(Character.toChars(in_stream.read())); next = in_stream.peek(); } String result = sb.toString(); TokenType type = TokenType.forValue(result); if (type == null) { type = TokenType.Identifier; } if (type == TokenType.Identifier && result.startsWith("__")) { throw new LexerException(start_line, start_col, "Unknown reserved identifier"); } return factory.create(type, start_index, start_line, start_col, in_stream.getPosition(), in_stream.getLine(), in_stream.getCol(), result); } LexIdentifier(TokenFactory factory, LexerStream in_stream); }
LexIdentifier { public Token read() throws IOException, LexerException { int start_index = in_stream.getPosition(); int start_line = in_stream.getLine(); int start_col = in_stream.getCol(); int next = in_stream.peek(); if (next == -1) { throw new LexerException(start_line, start_col, "Unexpected end of input stream when parsing identifier"); } if (!Character.isLetter(next) && next != '_') { throw new LexerException(start_line, start_col, "Invalid character for start of identifier: "+(char)next); } StringBuilder sb = new StringBuilder(); while (valid_ident_char(next)) { sb.append(Character.toChars(in_stream.read())); next = in_stream.peek(); } String result = sb.toString(); TokenType type = TokenType.forValue(result); if (type == null) { type = TokenType.Identifier; } if (type == TokenType.Identifier && result.startsWith("__")) { throw new LexerException(start_line, start_col, "Unknown reserved identifier"); } return factory.create(type, start_index, start_line, start_col, in_stream.getPosition(), in_stream.getLine(), in_stream.getCol(), result); } LexIdentifier(TokenFactory factory, LexerStream in_stream); Token read(); }
LexIdentifier { public Token read() throws IOException, LexerException { int start_index = in_stream.getPosition(); int start_line = in_stream.getLine(); int start_col = in_stream.getCol(); int next = in_stream.peek(); if (next == -1) { throw new LexerException(start_line, start_col, "Unexpected end of input stream when parsing identifier"); } if (!Character.isLetter(next) && next != '_') { throw new LexerException(start_line, start_col, "Invalid character for start of identifier: "+(char)next); } StringBuilder sb = new StringBuilder(); while (valid_ident_char(next)) { sb.append(Character.toChars(in_stream.read())); next = in_stream.peek(); } String result = sb.toString(); TokenType type = TokenType.forValue(result); if (type == null) { type = TokenType.Identifier; } if (type == TokenType.Identifier && result.startsWith("__")) { throw new LexerException(start_line, start_col, "Unknown reserved identifier"); } return factory.create(type, start_index, start_line, start_col, in_stream.getPosition(), in_stream.getLine(), in_stream.getCol(), result); } LexIdentifier(TokenFactory factory, LexerStream in_stream); Token read(); }
@Test public void inequality() throws Exception { String str = "a<b>c<=d>=e<>d<>=e"; LexerStream ls = new LexerStream(new StringReader(str)); LexOperator lex = new LexOperator(new BaseTokenFactory(), ls); ls.read(); Token tok = lex.read(); assertEquals(TokenType.LessThan, tok.getType()); assertEquals("<", tok.getValue()); assertTrue(tok.getLine() == 1 && tok.getEndLine() == 1); assertEquals(2, tok.getCol()); assertEquals(3, tok.getEndCol()); ls.read(); tok = lex.read(); assertEquals(TokenType.GreaterThan, tok.getType()); assertEquals(">", tok.getValue()); assertTrue(tok.getLine() == 1 && tok.getEndLine() == 1); assertEquals(4, tok.getCol()); assertEquals(5, tok.getEndCol()); ls.read(); tok = lex.read(); assertEquals(TokenType.LessEqual, tok.getType()); assertTrue(tok.getLine() == 1 && tok.getEndLine() == 1); assertEquals(6, tok.getCol()); assertEquals(8, tok.getEndCol()); ls.read(); tok = lex.read(); assertEquals(TokenType.GreaterEqual, tok.getType()); assertTrue(tok.getLine() == 1 && tok.getEndLine() == 1); assertEquals(9, tok.getCol()); assertEquals(11, tok.getEndCol()); ls.read(); tok = lex.read(); assertEquals(TokenType.LessOrGreater, tok.getType()); assertTrue(tok.getLine() == 1 && tok.getEndLine() == 1); assertEquals(12, tok.getCol()); assertEquals(14, tok.getEndCol()); ls.read(); tok = lex.read(); assertEquals(TokenType.LessEqualOrGreater, tok.getType()); assertTrue(tok.getLine() == 1 && tok.getEndLine() == 1); assertEquals(15, tok.getCol()); assertEquals(18, tok.getEndCol()); }
public Token read() throws IOException, LexerException { int index = in_stream.getPosition(); int line = in_stream.getLine(); int col = in_stream.getCol(); int next = in_stream.peek(); if (next == -1) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Unexpected end of input stream when parsing operator"); } if (next == '.') { int tmp = in_stream.peek(2); if (tmp > 0 && Character.isDigit(tmp)) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Literal float encountered when operator '.' expected"); } } char cur = (char)in_stream.read(); next = in_stream.peek(); switch (cur) { case '+': switch (next) { case '+': in_stream.read(); return factory.create(TokenType.Increment, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.PlusAssign, index, line, col, 2); } return factory.create(TokenType.Plus, index, line, col); case '-': switch (next) { case '-': in_stream.read(); return factory.create(TokenType.Decrement, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.MinusAssign, index, line, col, 2); } return factory.create(TokenType.Minus, index, line, col); case '*': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.TimesAssign, index, line, col, 2); default: break; } return factory.create(TokenType.Times, index, line, col); case '/': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.DivAssign, index, line, col, 2); } return factory.create(TokenType.Div, index, line, col); case '%': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.ModAssign, index, line, col, 2); } return factory.create(TokenType.Mod, index, line, col); case '&': switch (next) { case '&': in_stream.read(); return factory.create(TokenType.LogicalAnd, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.BitwiseAndAssign, index, line, col, 2); } return factory.create(TokenType.BitwiseAnd, index, line, col); case '|': switch (next) { case '|': in_stream.read(); return factory.create(TokenType.LogicalOr, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.BitwiseOrAssign, index, line, col, 2); } return factory.create(TokenType.BitwiseOr, index, line, col); case '^': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.XorAssign, index, line, col, 2); case '^': in_stream.read(); if (in_stream.peek() == '=') { in_stream.read(); return factory.create(TokenType.PowAssign, index, line, col, 3); } return factory.create(TokenType.Pow, index, line, col, 2); } return factory.create(TokenType.Xor, index, line, col); case '!': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.NotEqual, index, line, col, 2); case '<': in_stream.read(); switch (in_stream.peek()) { case '=': in_stream.read(); return factory.create(TokenType.UnorderedOrGreater, index, line, col, 3); case '>': in_stream.read(); switch (in_stream.peek()) { case '=': in_stream.read(); return factory.create(TokenType.Unordered, index, line, col, 4); } return factory.create(TokenType.UnorderedOrEqual, index, line, col, 3); } return factory.create(TokenType.UnorderedGreaterOrEqual, index, line, col, 2); case '>': in_stream.read(); switch (in_stream.peek()) { case '=': in_stream.read(); return factory.create(TokenType.UnorderedOrLess, index, line, col, 3); default: break; } return factory.create(TokenType.UnorderedLessOrEqual, index, line, col, 2); } return factory.create(TokenType.Not, index, line, col); case '~': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.TildeAssign, index, line, col, 2); } return factory.create(TokenType.Tilde, index, line, col); case '=': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.Equal, index, line, col, 2); case '>': in_stream.read(); return factory.create(TokenType.GoesTo, index, line, col, 2); } return factory.create(TokenType.Assign, index, line, col); case '<': switch (next) { case '<': in_stream.read(); switch (in_stream.peek()) { case '=': in_stream.read(); return factory.create(TokenType.ShiftLeftAssign, index, line, col, 3); default: break; } return factory.create(TokenType.ShiftLeft, index, line, col, 2); case '>': in_stream.read(); switch (in_stream.peek()) { case '=': in_stream.read(); return factory.create(TokenType.LessEqualOrGreater, index, line, col, 3); default: break; } return factory.create(TokenType.LessOrGreater, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.LessEqual, index, line, col, 2); } return factory.create(TokenType.LessThan, index, line, col); case '>': switch (next) { case '>': in_stream.read(); int p = in_stream.peek(); if (p != -1) { switch ((char)p) { case '=': in_stream.read(); return factory.create(TokenType.ShiftRightAssign, index, line, col, 3); case '>': in_stream.read(); int q = in_stream.peek(); if (q != -1 && q == '=') { in_stream.read(); return factory.create(TokenType.TripleRightShiftAssign, index, line, col, 4); } return factory.create(TokenType.ShiftRightUnsigned, index, line, col, 3); } } return factory.create(TokenType.ShiftRight, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.GreaterEqual, index, line, col, 2); } return factory.create(TokenType.GreaterThan, index, line, col); case '?': return factory.create(TokenType.Question, index, line, col); case '$': return factory.create(TokenType.Dollar, index, line, col); case ';': return factory.create(TokenType.Semicolon, index, line, col); case ':': return factory.create(TokenType.Colon, index, line, col); case ',': return factory.create(TokenType.Comma, index, line, col); case '.': if (next == '.') { in_stream.read(); int p = in_stream.peek(); if (p != -1 && p == '.') { in_stream.read(); return factory.create(TokenType.TripleDot, index, line, col, 3); } return factory.create(TokenType.DoubleDot, index, line, col, 2); } return factory.create(TokenType.Dot, index, line, col); case ')': return factory.create(TokenType.CloseParenthesis, index, line, col); case '(': return factory.create(TokenType.OpenParenthesis, index, line, col); case ']': return factory.create(TokenType.CloseSquareBracket, index, line, col); case '[': return factory.create(TokenType.OpenSquareBracket, index, line, col); case '}': return factory.create(TokenType.CloseCurlyBrace, index, line, col); case '{': return factory.create(TokenType.OpenCurlyBrace, index, line, col); case '#': return factory.create(TokenType.Hash, index, line, col); default: return null; } }
LexOperator { public Token read() throws IOException, LexerException { int index = in_stream.getPosition(); int line = in_stream.getLine(); int col = in_stream.getCol(); int next = in_stream.peek(); if (next == -1) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Unexpected end of input stream when parsing operator"); } if (next == '.') { int tmp = in_stream.peek(2); if (tmp > 0 && Character.isDigit(tmp)) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Literal float encountered when operator '.' expected"); } } char cur = (char)in_stream.read(); next = in_stream.peek(); switch (cur) { case '+': switch (next) { case '+': in_stream.read(); return factory.create(TokenType.Increment, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.PlusAssign, index, line, col, 2); } return factory.create(TokenType.Plus, index, line, col); case '-': switch (next) { case '-': in_stream.read(); return factory.create(TokenType.Decrement, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.MinusAssign, index, line, col, 2); } return factory.create(TokenType.Minus, index, line, col); case '*': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.TimesAssign, index, line, col, 2); default: break; } return factory.create(TokenType.Times, index, line, col); case '/': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.DivAssign, index, line, col, 2); } return factory.create(TokenType.Div, index, line, col); case '%': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.ModAssign, index, line, col, 2); } return factory.create(TokenType.Mod, index, line, col); case '&': switch (next) { case '&': in_stream.read(); return factory.create(TokenType.LogicalAnd, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.BitwiseAndAssign, index, line, col, 2); } return factory.create(TokenType.BitwiseAnd, index, line, col); case '|': switch (next) { case '|': in_stream.read(); return factory.create(TokenType.LogicalOr, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.BitwiseOrAssign, index, line, col, 2); } return factory.create(TokenType.BitwiseOr, index, line, col); case '^': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.XorAssign, index, line, col, 2); case '^': in_stream.read(); if (in_stream.peek() == '=') { in_stream.read(); return factory.create(TokenType.PowAssign, index, line, col, 3); } return factory.create(TokenType.Pow, index, line, col, 2); } return factory.create(TokenType.Xor, index, line, col); case '!': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.NotEqual, index, line, col, 2); case '<': in_stream.read(); switch (in_stream.peek()) { case '=': in_stream.read(); return factory.create(TokenType.UnorderedOrGreater, index, line, col, 3); case '>': in_stream.read(); switch (in_stream.peek()) { case '=': in_stream.read(); return factory.create(TokenType.Unordered, index, line, col, 4); } return factory.create(TokenType.UnorderedOrEqual, index, line, col, 3); } return factory.create(TokenType.UnorderedGreaterOrEqual, index, line, col, 2); case '>': in_stream.read(); switch (in_stream.peek()) { case '=': in_stream.read(); return factory.create(TokenType.UnorderedOrLess, index, line, col, 3); default: break; } return factory.create(TokenType.UnorderedLessOrEqual, index, line, col, 2); } return factory.create(TokenType.Not, index, line, col); case '~': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.TildeAssign, index, line, col, 2); } return factory.create(TokenType.Tilde, index, line, col); case '=': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.Equal, index, line, col, 2); case '>': in_stream.read(); return factory.create(TokenType.GoesTo, index, line, col, 2); } return factory.create(TokenType.Assign, index, line, col); case '<': switch (next) { case '<': in_stream.read(); switch (in_stream.peek()) { case '=': in_stream.read(); return factory.create(TokenType.ShiftLeftAssign, index, line, col, 3); default: break; } return factory.create(TokenType.ShiftLeft, index, line, col, 2); case '>': in_stream.read(); switch (in_stream.peek()) { case '=': in_stream.read(); return factory.create(TokenType.LessEqualOrGreater, index, line, col, 3); default: break; } return factory.create(TokenType.LessOrGreater, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.LessEqual, index, line, col, 2); } return factory.create(TokenType.LessThan, index, line, col); case '>': switch (next) { case '>': in_stream.read(); int p = in_stream.peek(); if (p != -1) { switch ((char)p) { case '=': in_stream.read(); return factory.create(TokenType.ShiftRightAssign, index, line, col, 3); case '>': in_stream.read(); int q = in_stream.peek(); if (q != -1 && q == '=') { in_stream.read(); return factory.create(TokenType.TripleRightShiftAssign, index, line, col, 4); } return factory.create(TokenType.ShiftRightUnsigned, index, line, col, 3); } } return factory.create(TokenType.ShiftRight, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.GreaterEqual, index, line, col, 2); } return factory.create(TokenType.GreaterThan, index, line, col); case '?': return factory.create(TokenType.Question, index, line, col); case '$': return factory.create(TokenType.Dollar, index, line, col); case ';': return factory.create(TokenType.Semicolon, index, line, col); case ':': return factory.create(TokenType.Colon, index, line, col); case ',': return factory.create(TokenType.Comma, index, line, col); case '.': if (next == '.') { in_stream.read(); int p = in_stream.peek(); if (p != -1 && p == '.') { in_stream.read(); return factory.create(TokenType.TripleDot, index, line, col, 3); } return factory.create(TokenType.DoubleDot, index, line, col, 2); } return factory.create(TokenType.Dot, index, line, col); case ')': return factory.create(TokenType.CloseParenthesis, index, line, col); case '(': return factory.create(TokenType.OpenParenthesis, index, line, col); case ']': return factory.create(TokenType.CloseSquareBracket, index, line, col); case '[': return factory.create(TokenType.OpenSquareBracket, index, line, col); case '}': return factory.create(TokenType.CloseCurlyBrace, index, line, col); case '{': return factory.create(TokenType.OpenCurlyBrace, index, line, col); case '#': return factory.create(TokenType.Hash, index, line, col); default: return null; } } }
LexOperator { public Token read() throws IOException, LexerException { int index = in_stream.getPosition(); int line = in_stream.getLine(); int col = in_stream.getCol(); int next = in_stream.peek(); if (next == -1) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Unexpected end of input stream when parsing operator"); } if (next == '.') { int tmp = in_stream.peek(2); if (tmp > 0 && Character.isDigit(tmp)) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Literal float encountered when operator '.' expected"); } } char cur = (char)in_stream.read(); next = in_stream.peek(); switch (cur) { case '+': switch (next) { case '+': in_stream.read(); return factory.create(TokenType.Increment, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.PlusAssign, index, line, col, 2); } return factory.create(TokenType.Plus, index, line, col); case '-': switch (next) { case '-': in_stream.read(); return factory.create(TokenType.Decrement, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.MinusAssign, index, line, col, 2); } return factory.create(TokenType.Minus, index, line, col); case '*': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.TimesAssign, index, line, col, 2); default: break; } return factory.create(TokenType.Times, index, line, col); case '/': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.DivAssign, index, line, col, 2); } return factory.create(TokenType.Div, index, line, col); case '%': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.ModAssign, index, line, col, 2); } return factory.create(TokenType.Mod, index, line, col); case '&': switch (next) { case '&': in_stream.read(); return factory.create(TokenType.LogicalAnd, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.BitwiseAndAssign, index, line, col, 2); } return factory.create(TokenType.BitwiseAnd, index, line, col); case '|': switch (next) { case '|': in_stream.read(); return factory.create(TokenType.LogicalOr, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.BitwiseOrAssign, index, line, col, 2); } return factory.create(TokenType.BitwiseOr, index, line, col); case '^': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.XorAssign, index, line, col, 2); case '^': in_stream.read(); if (in_stream.peek() == '=') { in_stream.read(); return factory.create(TokenType.PowAssign, index, line, col, 3); } return factory.create(TokenType.Pow, index, line, col, 2); } return factory.create(TokenType.Xor, index, line, col); case '!': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.NotEqual, index, line, col, 2); case '<': in_stream.read(); switch (in_stream.peek()) { case '=': in_stream.read(); return factory.create(TokenType.UnorderedOrGreater, index, line, col, 3); case '>': in_stream.read(); switch (in_stream.peek()) { case '=': in_stream.read(); return factory.create(TokenType.Unordered, index, line, col, 4); } return factory.create(TokenType.UnorderedOrEqual, index, line, col, 3); } return factory.create(TokenType.UnorderedGreaterOrEqual, index, line, col, 2); case '>': in_stream.read(); switch (in_stream.peek()) { case '=': in_stream.read(); return factory.create(TokenType.UnorderedOrLess, index, line, col, 3); default: break; } return factory.create(TokenType.UnorderedLessOrEqual, index, line, col, 2); } return factory.create(TokenType.Not, index, line, col); case '~': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.TildeAssign, index, line, col, 2); } return factory.create(TokenType.Tilde, index, line, col); case '=': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.Equal, index, line, col, 2); case '>': in_stream.read(); return factory.create(TokenType.GoesTo, index, line, col, 2); } return factory.create(TokenType.Assign, index, line, col); case '<': switch (next) { case '<': in_stream.read(); switch (in_stream.peek()) { case '=': in_stream.read(); return factory.create(TokenType.ShiftLeftAssign, index, line, col, 3); default: break; } return factory.create(TokenType.ShiftLeft, index, line, col, 2); case '>': in_stream.read(); switch (in_stream.peek()) { case '=': in_stream.read(); return factory.create(TokenType.LessEqualOrGreater, index, line, col, 3); default: break; } return factory.create(TokenType.LessOrGreater, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.LessEqual, index, line, col, 2); } return factory.create(TokenType.LessThan, index, line, col); case '>': switch (next) { case '>': in_stream.read(); int p = in_stream.peek(); if (p != -1) { switch ((char)p) { case '=': in_stream.read(); return factory.create(TokenType.ShiftRightAssign, index, line, col, 3); case '>': in_stream.read(); int q = in_stream.peek(); if (q != -1 && q == '=') { in_stream.read(); return factory.create(TokenType.TripleRightShiftAssign, index, line, col, 4); } return factory.create(TokenType.ShiftRightUnsigned, index, line, col, 3); } } return factory.create(TokenType.ShiftRight, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.GreaterEqual, index, line, col, 2); } return factory.create(TokenType.GreaterThan, index, line, col); case '?': return factory.create(TokenType.Question, index, line, col); case '$': return factory.create(TokenType.Dollar, index, line, col); case ';': return factory.create(TokenType.Semicolon, index, line, col); case ':': return factory.create(TokenType.Colon, index, line, col); case ',': return factory.create(TokenType.Comma, index, line, col); case '.': if (next == '.') { in_stream.read(); int p = in_stream.peek(); if (p != -1 && p == '.') { in_stream.read(); return factory.create(TokenType.TripleDot, index, line, col, 3); } return factory.create(TokenType.DoubleDot, index, line, col, 2); } return factory.create(TokenType.Dot, index, line, col); case ')': return factory.create(TokenType.CloseParenthesis, index, line, col); case '(': return factory.create(TokenType.OpenParenthesis, index, line, col); case ']': return factory.create(TokenType.CloseSquareBracket, index, line, col); case '[': return factory.create(TokenType.OpenSquareBracket, index, line, col); case '}': return factory.create(TokenType.CloseCurlyBrace, index, line, col); case '{': return factory.create(TokenType.OpenCurlyBrace, index, line, col); case '#': return factory.create(TokenType.Hash, index, line, col); default: return null; } } LexOperator(TokenFactory factory, LexerStream in_stream); }
LexOperator { public Token read() throws IOException, LexerException { int index = in_stream.getPosition(); int line = in_stream.getLine(); int col = in_stream.getCol(); int next = in_stream.peek(); if (next == -1) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Unexpected end of input stream when parsing operator"); } if (next == '.') { int tmp = in_stream.peek(2); if (tmp > 0 && Character.isDigit(tmp)) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Literal float encountered when operator '.' expected"); } } char cur = (char)in_stream.read(); next = in_stream.peek(); switch (cur) { case '+': switch (next) { case '+': in_stream.read(); return factory.create(TokenType.Increment, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.PlusAssign, index, line, col, 2); } return factory.create(TokenType.Plus, index, line, col); case '-': switch (next) { case '-': in_stream.read(); return factory.create(TokenType.Decrement, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.MinusAssign, index, line, col, 2); } return factory.create(TokenType.Minus, index, line, col); case '*': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.TimesAssign, index, line, col, 2); default: break; } return factory.create(TokenType.Times, index, line, col); case '/': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.DivAssign, index, line, col, 2); } return factory.create(TokenType.Div, index, line, col); case '%': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.ModAssign, index, line, col, 2); } return factory.create(TokenType.Mod, index, line, col); case '&': switch (next) { case '&': in_stream.read(); return factory.create(TokenType.LogicalAnd, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.BitwiseAndAssign, index, line, col, 2); } return factory.create(TokenType.BitwiseAnd, index, line, col); case '|': switch (next) { case '|': in_stream.read(); return factory.create(TokenType.LogicalOr, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.BitwiseOrAssign, index, line, col, 2); } return factory.create(TokenType.BitwiseOr, index, line, col); case '^': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.XorAssign, index, line, col, 2); case '^': in_stream.read(); if (in_stream.peek() == '=') { in_stream.read(); return factory.create(TokenType.PowAssign, index, line, col, 3); } return factory.create(TokenType.Pow, index, line, col, 2); } return factory.create(TokenType.Xor, index, line, col); case '!': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.NotEqual, index, line, col, 2); case '<': in_stream.read(); switch (in_stream.peek()) { case '=': in_stream.read(); return factory.create(TokenType.UnorderedOrGreater, index, line, col, 3); case '>': in_stream.read(); switch (in_stream.peek()) { case '=': in_stream.read(); return factory.create(TokenType.Unordered, index, line, col, 4); } return factory.create(TokenType.UnorderedOrEqual, index, line, col, 3); } return factory.create(TokenType.UnorderedGreaterOrEqual, index, line, col, 2); case '>': in_stream.read(); switch (in_stream.peek()) { case '=': in_stream.read(); return factory.create(TokenType.UnorderedOrLess, index, line, col, 3); default: break; } return factory.create(TokenType.UnorderedLessOrEqual, index, line, col, 2); } return factory.create(TokenType.Not, index, line, col); case '~': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.TildeAssign, index, line, col, 2); } return factory.create(TokenType.Tilde, index, line, col); case '=': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.Equal, index, line, col, 2); case '>': in_stream.read(); return factory.create(TokenType.GoesTo, index, line, col, 2); } return factory.create(TokenType.Assign, index, line, col); case '<': switch (next) { case '<': in_stream.read(); switch (in_stream.peek()) { case '=': in_stream.read(); return factory.create(TokenType.ShiftLeftAssign, index, line, col, 3); default: break; } return factory.create(TokenType.ShiftLeft, index, line, col, 2); case '>': in_stream.read(); switch (in_stream.peek()) { case '=': in_stream.read(); return factory.create(TokenType.LessEqualOrGreater, index, line, col, 3); default: break; } return factory.create(TokenType.LessOrGreater, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.LessEqual, index, line, col, 2); } return factory.create(TokenType.LessThan, index, line, col); case '>': switch (next) { case '>': in_stream.read(); int p = in_stream.peek(); if (p != -1) { switch ((char)p) { case '=': in_stream.read(); return factory.create(TokenType.ShiftRightAssign, index, line, col, 3); case '>': in_stream.read(); int q = in_stream.peek(); if (q != -1 && q == '=') { in_stream.read(); return factory.create(TokenType.TripleRightShiftAssign, index, line, col, 4); } return factory.create(TokenType.ShiftRightUnsigned, index, line, col, 3); } } return factory.create(TokenType.ShiftRight, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.GreaterEqual, index, line, col, 2); } return factory.create(TokenType.GreaterThan, index, line, col); case '?': return factory.create(TokenType.Question, index, line, col); case '$': return factory.create(TokenType.Dollar, index, line, col); case ';': return factory.create(TokenType.Semicolon, index, line, col); case ':': return factory.create(TokenType.Colon, index, line, col); case ',': return factory.create(TokenType.Comma, index, line, col); case '.': if (next == '.') { in_stream.read(); int p = in_stream.peek(); if (p != -1 && p == '.') { in_stream.read(); return factory.create(TokenType.TripleDot, index, line, col, 3); } return factory.create(TokenType.DoubleDot, index, line, col, 2); } return factory.create(TokenType.Dot, index, line, col); case ')': return factory.create(TokenType.CloseParenthesis, index, line, col); case '(': return factory.create(TokenType.OpenParenthesis, index, line, col); case ']': return factory.create(TokenType.CloseSquareBracket, index, line, col); case '[': return factory.create(TokenType.OpenSquareBracket, index, line, col); case '}': return factory.create(TokenType.CloseCurlyBrace, index, line, col); case '{': return factory.create(TokenType.OpenCurlyBrace, index, line, col); case '#': return factory.create(TokenType.Hash, index, line, col); default: return null; } } LexOperator(TokenFactory factory, LexerStream in_stream); Token read(); }
LexOperator { public Token read() throws IOException, LexerException { int index = in_stream.getPosition(); int line = in_stream.getLine(); int col = in_stream.getCol(); int next = in_stream.peek(); if (next == -1) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Unexpected end of input stream when parsing operator"); } if (next == '.') { int tmp = in_stream.peek(2); if (tmp > 0 && Character.isDigit(tmp)) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Literal float encountered when operator '.' expected"); } } char cur = (char)in_stream.read(); next = in_stream.peek(); switch (cur) { case '+': switch (next) { case '+': in_stream.read(); return factory.create(TokenType.Increment, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.PlusAssign, index, line, col, 2); } return factory.create(TokenType.Plus, index, line, col); case '-': switch (next) { case '-': in_stream.read(); return factory.create(TokenType.Decrement, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.MinusAssign, index, line, col, 2); } return factory.create(TokenType.Minus, index, line, col); case '*': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.TimesAssign, index, line, col, 2); default: break; } return factory.create(TokenType.Times, index, line, col); case '/': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.DivAssign, index, line, col, 2); } return factory.create(TokenType.Div, index, line, col); case '%': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.ModAssign, index, line, col, 2); } return factory.create(TokenType.Mod, index, line, col); case '&': switch (next) { case '&': in_stream.read(); return factory.create(TokenType.LogicalAnd, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.BitwiseAndAssign, index, line, col, 2); } return factory.create(TokenType.BitwiseAnd, index, line, col); case '|': switch (next) { case '|': in_stream.read(); return factory.create(TokenType.LogicalOr, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.BitwiseOrAssign, index, line, col, 2); } return factory.create(TokenType.BitwiseOr, index, line, col); case '^': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.XorAssign, index, line, col, 2); case '^': in_stream.read(); if (in_stream.peek() == '=') { in_stream.read(); return factory.create(TokenType.PowAssign, index, line, col, 3); } return factory.create(TokenType.Pow, index, line, col, 2); } return factory.create(TokenType.Xor, index, line, col); case '!': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.NotEqual, index, line, col, 2); case '<': in_stream.read(); switch (in_stream.peek()) { case '=': in_stream.read(); return factory.create(TokenType.UnorderedOrGreater, index, line, col, 3); case '>': in_stream.read(); switch (in_stream.peek()) { case '=': in_stream.read(); return factory.create(TokenType.Unordered, index, line, col, 4); } return factory.create(TokenType.UnorderedOrEqual, index, line, col, 3); } return factory.create(TokenType.UnorderedGreaterOrEqual, index, line, col, 2); case '>': in_stream.read(); switch (in_stream.peek()) { case '=': in_stream.read(); return factory.create(TokenType.UnorderedOrLess, index, line, col, 3); default: break; } return factory.create(TokenType.UnorderedLessOrEqual, index, line, col, 2); } return factory.create(TokenType.Not, index, line, col); case '~': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.TildeAssign, index, line, col, 2); } return factory.create(TokenType.Tilde, index, line, col); case '=': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.Equal, index, line, col, 2); case '>': in_stream.read(); return factory.create(TokenType.GoesTo, index, line, col, 2); } return factory.create(TokenType.Assign, index, line, col); case '<': switch (next) { case '<': in_stream.read(); switch (in_stream.peek()) { case '=': in_stream.read(); return factory.create(TokenType.ShiftLeftAssign, index, line, col, 3); default: break; } return factory.create(TokenType.ShiftLeft, index, line, col, 2); case '>': in_stream.read(); switch (in_stream.peek()) { case '=': in_stream.read(); return factory.create(TokenType.LessEqualOrGreater, index, line, col, 3); default: break; } return factory.create(TokenType.LessOrGreater, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.LessEqual, index, line, col, 2); } return factory.create(TokenType.LessThan, index, line, col); case '>': switch (next) { case '>': in_stream.read(); int p = in_stream.peek(); if (p != -1) { switch ((char)p) { case '=': in_stream.read(); return factory.create(TokenType.ShiftRightAssign, index, line, col, 3); case '>': in_stream.read(); int q = in_stream.peek(); if (q != -1 && q == '=') { in_stream.read(); return factory.create(TokenType.TripleRightShiftAssign, index, line, col, 4); } return factory.create(TokenType.ShiftRightUnsigned, index, line, col, 3); } } return factory.create(TokenType.ShiftRight, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.GreaterEqual, index, line, col, 2); } return factory.create(TokenType.GreaterThan, index, line, col); case '?': return factory.create(TokenType.Question, index, line, col); case '$': return factory.create(TokenType.Dollar, index, line, col); case ';': return factory.create(TokenType.Semicolon, index, line, col); case ':': return factory.create(TokenType.Colon, index, line, col); case ',': return factory.create(TokenType.Comma, index, line, col); case '.': if (next == '.') { in_stream.read(); int p = in_stream.peek(); if (p != -1 && p == '.') { in_stream.read(); return factory.create(TokenType.TripleDot, index, line, col, 3); } return factory.create(TokenType.DoubleDot, index, line, col, 2); } return factory.create(TokenType.Dot, index, line, col); case ')': return factory.create(TokenType.CloseParenthesis, index, line, col); case '(': return factory.create(TokenType.OpenParenthesis, index, line, col); case ']': return factory.create(TokenType.CloseSquareBracket, index, line, col); case '[': return factory.create(TokenType.OpenSquareBracket, index, line, col); case '}': return factory.create(TokenType.CloseCurlyBrace, index, line, col); case '{': return factory.create(TokenType.OpenCurlyBrace, index, line, col); case '#': return factory.create(TokenType.Hash, index, line, col); default: return null; } } LexOperator(TokenFactory factory, LexerStream in_stream); Token read(); }
@Test public void add() throws Exception { String str = "+ w++ += f"; LexerStream ls = new LexerStream(new StringReader(str)); LexOperator lex = new LexOperator(new BaseTokenFactory(), ls); Token tok = lex.read(); assertEquals(TokenType.Plus, tok.getType()); assertEquals(1, tok.getLine()); assertEquals(1, tok.getCol()); assertEquals(1, tok.getEndLine()); assertEquals(2, tok.getEndCol()); ls.read(); ls.read(); tok = lex.read(); assertEquals(TokenType.Increment, tok.getType()); assertEquals(1, tok.getLine()); assertEquals(4, tok.getCol()); assertEquals(1, tok.getEndLine()); assertEquals(6, tok.getEndCol()); ls.read(); tok = lex.read(); assertEquals(TokenType.PlusAssign, tok.getType()); assertEquals(1, tok.getLine()); assertEquals(7, tok.getCol()); assertEquals(1, tok.getEndLine()); assertEquals(9, tok.getEndCol()); assertEquals(' ', ls.read()); assertEquals('f', ls.read()); assertEquals(-1, ls.read()); }
public Token read() throws IOException, LexerException { int index = in_stream.getPosition(); int line = in_stream.getLine(); int col = in_stream.getCol(); int next = in_stream.peek(); if (next == -1) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Unexpected end of input stream when parsing operator"); } if (next == '.') { int tmp = in_stream.peek(2); if (tmp > 0 && Character.isDigit(tmp)) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Literal float encountered when operator '.' expected"); } } char cur = (char)in_stream.read(); next = in_stream.peek(); switch (cur) { case '+': switch (next) { case '+': in_stream.read(); return factory.create(TokenType.Increment, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.PlusAssign, index, line, col, 2); } return factory.create(TokenType.Plus, index, line, col); case '-': switch (next) { case '-': in_stream.read(); return factory.create(TokenType.Decrement, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.MinusAssign, index, line, col, 2); } return factory.create(TokenType.Minus, index, line, col); case '*': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.TimesAssign, index, line, col, 2); default: break; } return factory.create(TokenType.Times, index, line, col); case '/': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.DivAssign, index, line, col, 2); } return factory.create(TokenType.Div, index, line, col); case '%': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.ModAssign, index, line, col, 2); } return factory.create(TokenType.Mod, index, line, col); case '&': switch (next) { case '&': in_stream.read(); return factory.create(TokenType.LogicalAnd, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.BitwiseAndAssign, index, line, col, 2); } return factory.create(TokenType.BitwiseAnd, index, line, col); case '|': switch (next) { case '|': in_stream.read(); return factory.create(TokenType.LogicalOr, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.BitwiseOrAssign, index, line, col, 2); } return factory.create(TokenType.BitwiseOr, index, line, col); case '^': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.XorAssign, index, line, col, 2); case '^': in_stream.read(); if (in_stream.peek() == '=') { in_stream.read(); return factory.create(TokenType.PowAssign, index, line, col, 3); } return factory.create(TokenType.Pow, index, line, col, 2); } return factory.create(TokenType.Xor, index, line, col); case '!': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.NotEqual, index, line, col, 2); case '<': in_stream.read(); switch (in_stream.peek()) { case '=': in_stream.read(); return factory.create(TokenType.UnorderedOrGreater, index, line, col, 3); case '>': in_stream.read(); switch (in_stream.peek()) { case '=': in_stream.read(); return factory.create(TokenType.Unordered, index, line, col, 4); } return factory.create(TokenType.UnorderedOrEqual, index, line, col, 3); } return factory.create(TokenType.UnorderedGreaterOrEqual, index, line, col, 2); case '>': in_stream.read(); switch (in_stream.peek()) { case '=': in_stream.read(); return factory.create(TokenType.UnorderedOrLess, index, line, col, 3); default: break; } return factory.create(TokenType.UnorderedLessOrEqual, index, line, col, 2); } return factory.create(TokenType.Not, index, line, col); case '~': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.TildeAssign, index, line, col, 2); } return factory.create(TokenType.Tilde, index, line, col); case '=': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.Equal, index, line, col, 2); case '>': in_stream.read(); return factory.create(TokenType.GoesTo, index, line, col, 2); } return factory.create(TokenType.Assign, index, line, col); case '<': switch (next) { case '<': in_stream.read(); switch (in_stream.peek()) { case '=': in_stream.read(); return factory.create(TokenType.ShiftLeftAssign, index, line, col, 3); default: break; } return factory.create(TokenType.ShiftLeft, index, line, col, 2); case '>': in_stream.read(); switch (in_stream.peek()) { case '=': in_stream.read(); return factory.create(TokenType.LessEqualOrGreater, index, line, col, 3); default: break; } return factory.create(TokenType.LessOrGreater, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.LessEqual, index, line, col, 2); } return factory.create(TokenType.LessThan, index, line, col); case '>': switch (next) { case '>': in_stream.read(); int p = in_stream.peek(); if (p != -1) { switch ((char)p) { case '=': in_stream.read(); return factory.create(TokenType.ShiftRightAssign, index, line, col, 3); case '>': in_stream.read(); int q = in_stream.peek(); if (q != -1 && q == '=') { in_stream.read(); return factory.create(TokenType.TripleRightShiftAssign, index, line, col, 4); } return factory.create(TokenType.ShiftRightUnsigned, index, line, col, 3); } } return factory.create(TokenType.ShiftRight, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.GreaterEqual, index, line, col, 2); } return factory.create(TokenType.GreaterThan, index, line, col); case '?': return factory.create(TokenType.Question, index, line, col); case '$': return factory.create(TokenType.Dollar, index, line, col); case ';': return factory.create(TokenType.Semicolon, index, line, col); case ':': return factory.create(TokenType.Colon, index, line, col); case ',': return factory.create(TokenType.Comma, index, line, col); case '.': if (next == '.') { in_stream.read(); int p = in_stream.peek(); if (p != -1 && p == '.') { in_stream.read(); return factory.create(TokenType.TripleDot, index, line, col, 3); } return factory.create(TokenType.DoubleDot, index, line, col, 2); } return factory.create(TokenType.Dot, index, line, col); case ')': return factory.create(TokenType.CloseParenthesis, index, line, col); case '(': return factory.create(TokenType.OpenParenthesis, index, line, col); case ']': return factory.create(TokenType.CloseSquareBracket, index, line, col); case '[': return factory.create(TokenType.OpenSquareBracket, index, line, col); case '}': return factory.create(TokenType.CloseCurlyBrace, index, line, col); case '{': return factory.create(TokenType.OpenCurlyBrace, index, line, col); case '#': return factory.create(TokenType.Hash, index, line, col); default: return null; } }
LexOperator { public Token read() throws IOException, LexerException { int index = in_stream.getPosition(); int line = in_stream.getLine(); int col = in_stream.getCol(); int next = in_stream.peek(); if (next == -1) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Unexpected end of input stream when parsing operator"); } if (next == '.') { int tmp = in_stream.peek(2); if (tmp > 0 && Character.isDigit(tmp)) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Literal float encountered when operator '.' expected"); } } char cur = (char)in_stream.read(); next = in_stream.peek(); switch (cur) { case '+': switch (next) { case '+': in_stream.read(); return factory.create(TokenType.Increment, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.PlusAssign, index, line, col, 2); } return factory.create(TokenType.Plus, index, line, col); case '-': switch (next) { case '-': in_stream.read(); return factory.create(TokenType.Decrement, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.MinusAssign, index, line, col, 2); } return factory.create(TokenType.Minus, index, line, col); case '*': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.TimesAssign, index, line, col, 2); default: break; } return factory.create(TokenType.Times, index, line, col); case '/': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.DivAssign, index, line, col, 2); } return factory.create(TokenType.Div, index, line, col); case '%': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.ModAssign, index, line, col, 2); } return factory.create(TokenType.Mod, index, line, col); case '&': switch (next) { case '&': in_stream.read(); return factory.create(TokenType.LogicalAnd, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.BitwiseAndAssign, index, line, col, 2); } return factory.create(TokenType.BitwiseAnd, index, line, col); case '|': switch (next) { case '|': in_stream.read(); return factory.create(TokenType.LogicalOr, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.BitwiseOrAssign, index, line, col, 2); } return factory.create(TokenType.BitwiseOr, index, line, col); case '^': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.XorAssign, index, line, col, 2); case '^': in_stream.read(); if (in_stream.peek() == '=') { in_stream.read(); return factory.create(TokenType.PowAssign, index, line, col, 3); } return factory.create(TokenType.Pow, index, line, col, 2); } return factory.create(TokenType.Xor, index, line, col); case '!': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.NotEqual, index, line, col, 2); case '<': in_stream.read(); switch (in_stream.peek()) { case '=': in_stream.read(); return factory.create(TokenType.UnorderedOrGreater, index, line, col, 3); case '>': in_stream.read(); switch (in_stream.peek()) { case '=': in_stream.read(); return factory.create(TokenType.Unordered, index, line, col, 4); } return factory.create(TokenType.UnorderedOrEqual, index, line, col, 3); } return factory.create(TokenType.UnorderedGreaterOrEqual, index, line, col, 2); case '>': in_stream.read(); switch (in_stream.peek()) { case '=': in_stream.read(); return factory.create(TokenType.UnorderedOrLess, index, line, col, 3); default: break; } return factory.create(TokenType.UnorderedLessOrEqual, index, line, col, 2); } return factory.create(TokenType.Not, index, line, col); case '~': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.TildeAssign, index, line, col, 2); } return factory.create(TokenType.Tilde, index, line, col); case '=': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.Equal, index, line, col, 2); case '>': in_stream.read(); return factory.create(TokenType.GoesTo, index, line, col, 2); } return factory.create(TokenType.Assign, index, line, col); case '<': switch (next) { case '<': in_stream.read(); switch (in_stream.peek()) { case '=': in_stream.read(); return factory.create(TokenType.ShiftLeftAssign, index, line, col, 3); default: break; } return factory.create(TokenType.ShiftLeft, index, line, col, 2); case '>': in_stream.read(); switch (in_stream.peek()) { case '=': in_stream.read(); return factory.create(TokenType.LessEqualOrGreater, index, line, col, 3); default: break; } return factory.create(TokenType.LessOrGreater, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.LessEqual, index, line, col, 2); } return factory.create(TokenType.LessThan, index, line, col); case '>': switch (next) { case '>': in_stream.read(); int p = in_stream.peek(); if (p != -1) { switch ((char)p) { case '=': in_stream.read(); return factory.create(TokenType.ShiftRightAssign, index, line, col, 3); case '>': in_stream.read(); int q = in_stream.peek(); if (q != -1 && q == '=') { in_stream.read(); return factory.create(TokenType.TripleRightShiftAssign, index, line, col, 4); } return factory.create(TokenType.ShiftRightUnsigned, index, line, col, 3); } } return factory.create(TokenType.ShiftRight, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.GreaterEqual, index, line, col, 2); } return factory.create(TokenType.GreaterThan, index, line, col); case '?': return factory.create(TokenType.Question, index, line, col); case '$': return factory.create(TokenType.Dollar, index, line, col); case ';': return factory.create(TokenType.Semicolon, index, line, col); case ':': return factory.create(TokenType.Colon, index, line, col); case ',': return factory.create(TokenType.Comma, index, line, col); case '.': if (next == '.') { in_stream.read(); int p = in_stream.peek(); if (p != -1 && p == '.') { in_stream.read(); return factory.create(TokenType.TripleDot, index, line, col, 3); } return factory.create(TokenType.DoubleDot, index, line, col, 2); } return factory.create(TokenType.Dot, index, line, col); case ')': return factory.create(TokenType.CloseParenthesis, index, line, col); case '(': return factory.create(TokenType.OpenParenthesis, index, line, col); case ']': return factory.create(TokenType.CloseSquareBracket, index, line, col); case '[': return factory.create(TokenType.OpenSquareBracket, index, line, col); case '}': return factory.create(TokenType.CloseCurlyBrace, index, line, col); case '{': return factory.create(TokenType.OpenCurlyBrace, index, line, col); case '#': return factory.create(TokenType.Hash, index, line, col); default: return null; } } }
LexOperator { public Token read() throws IOException, LexerException { int index = in_stream.getPosition(); int line = in_stream.getLine(); int col = in_stream.getCol(); int next = in_stream.peek(); if (next == -1) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Unexpected end of input stream when parsing operator"); } if (next == '.') { int tmp = in_stream.peek(2); if (tmp > 0 && Character.isDigit(tmp)) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Literal float encountered when operator '.' expected"); } } char cur = (char)in_stream.read(); next = in_stream.peek(); switch (cur) { case '+': switch (next) { case '+': in_stream.read(); return factory.create(TokenType.Increment, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.PlusAssign, index, line, col, 2); } return factory.create(TokenType.Plus, index, line, col); case '-': switch (next) { case '-': in_stream.read(); return factory.create(TokenType.Decrement, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.MinusAssign, index, line, col, 2); } return factory.create(TokenType.Minus, index, line, col); case '*': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.TimesAssign, index, line, col, 2); default: break; } return factory.create(TokenType.Times, index, line, col); case '/': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.DivAssign, index, line, col, 2); } return factory.create(TokenType.Div, index, line, col); case '%': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.ModAssign, index, line, col, 2); } return factory.create(TokenType.Mod, index, line, col); case '&': switch (next) { case '&': in_stream.read(); return factory.create(TokenType.LogicalAnd, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.BitwiseAndAssign, index, line, col, 2); } return factory.create(TokenType.BitwiseAnd, index, line, col); case '|': switch (next) { case '|': in_stream.read(); return factory.create(TokenType.LogicalOr, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.BitwiseOrAssign, index, line, col, 2); } return factory.create(TokenType.BitwiseOr, index, line, col); case '^': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.XorAssign, index, line, col, 2); case '^': in_stream.read(); if (in_stream.peek() == '=') { in_stream.read(); return factory.create(TokenType.PowAssign, index, line, col, 3); } return factory.create(TokenType.Pow, index, line, col, 2); } return factory.create(TokenType.Xor, index, line, col); case '!': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.NotEqual, index, line, col, 2); case '<': in_stream.read(); switch (in_stream.peek()) { case '=': in_stream.read(); return factory.create(TokenType.UnorderedOrGreater, index, line, col, 3); case '>': in_stream.read(); switch (in_stream.peek()) { case '=': in_stream.read(); return factory.create(TokenType.Unordered, index, line, col, 4); } return factory.create(TokenType.UnorderedOrEqual, index, line, col, 3); } return factory.create(TokenType.UnorderedGreaterOrEqual, index, line, col, 2); case '>': in_stream.read(); switch (in_stream.peek()) { case '=': in_stream.read(); return factory.create(TokenType.UnorderedOrLess, index, line, col, 3); default: break; } return factory.create(TokenType.UnorderedLessOrEqual, index, line, col, 2); } return factory.create(TokenType.Not, index, line, col); case '~': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.TildeAssign, index, line, col, 2); } return factory.create(TokenType.Tilde, index, line, col); case '=': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.Equal, index, line, col, 2); case '>': in_stream.read(); return factory.create(TokenType.GoesTo, index, line, col, 2); } return factory.create(TokenType.Assign, index, line, col); case '<': switch (next) { case '<': in_stream.read(); switch (in_stream.peek()) { case '=': in_stream.read(); return factory.create(TokenType.ShiftLeftAssign, index, line, col, 3); default: break; } return factory.create(TokenType.ShiftLeft, index, line, col, 2); case '>': in_stream.read(); switch (in_stream.peek()) { case '=': in_stream.read(); return factory.create(TokenType.LessEqualOrGreater, index, line, col, 3); default: break; } return factory.create(TokenType.LessOrGreater, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.LessEqual, index, line, col, 2); } return factory.create(TokenType.LessThan, index, line, col); case '>': switch (next) { case '>': in_stream.read(); int p = in_stream.peek(); if (p != -1) { switch ((char)p) { case '=': in_stream.read(); return factory.create(TokenType.ShiftRightAssign, index, line, col, 3); case '>': in_stream.read(); int q = in_stream.peek(); if (q != -1 && q == '=') { in_stream.read(); return factory.create(TokenType.TripleRightShiftAssign, index, line, col, 4); } return factory.create(TokenType.ShiftRightUnsigned, index, line, col, 3); } } return factory.create(TokenType.ShiftRight, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.GreaterEqual, index, line, col, 2); } return factory.create(TokenType.GreaterThan, index, line, col); case '?': return factory.create(TokenType.Question, index, line, col); case '$': return factory.create(TokenType.Dollar, index, line, col); case ';': return factory.create(TokenType.Semicolon, index, line, col); case ':': return factory.create(TokenType.Colon, index, line, col); case ',': return factory.create(TokenType.Comma, index, line, col); case '.': if (next == '.') { in_stream.read(); int p = in_stream.peek(); if (p != -1 && p == '.') { in_stream.read(); return factory.create(TokenType.TripleDot, index, line, col, 3); } return factory.create(TokenType.DoubleDot, index, line, col, 2); } return factory.create(TokenType.Dot, index, line, col); case ')': return factory.create(TokenType.CloseParenthesis, index, line, col); case '(': return factory.create(TokenType.OpenParenthesis, index, line, col); case ']': return factory.create(TokenType.CloseSquareBracket, index, line, col); case '[': return factory.create(TokenType.OpenSquareBracket, index, line, col); case '}': return factory.create(TokenType.CloseCurlyBrace, index, line, col); case '{': return factory.create(TokenType.OpenCurlyBrace, index, line, col); case '#': return factory.create(TokenType.Hash, index, line, col); default: return null; } } LexOperator(TokenFactory factory, LexerStream in_stream); }
LexOperator { public Token read() throws IOException, LexerException { int index = in_stream.getPosition(); int line = in_stream.getLine(); int col = in_stream.getCol(); int next = in_stream.peek(); if (next == -1) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Unexpected end of input stream when parsing operator"); } if (next == '.') { int tmp = in_stream.peek(2); if (tmp > 0 && Character.isDigit(tmp)) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Literal float encountered when operator '.' expected"); } } char cur = (char)in_stream.read(); next = in_stream.peek(); switch (cur) { case '+': switch (next) { case '+': in_stream.read(); return factory.create(TokenType.Increment, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.PlusAssign, index, line, col, 2); } return factory.create(TokenType.Plus, index, line, col); case '-': switch (next) { case '-': in_stream.read(); return factory.create(TokenType.Decrement, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.MinusAssign, index, line, col, 2); } return factory.create(TokenType.Minus, index, line, col); case '*': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.TimesAssign, index, line, col, 2); default: break; } return factory.create(TokenType.Times, index, line, col); case '/': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.DivAssign, index, line, col, 2); } return factory.create(TokenType.Div, index, line, col); case '%': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.ModAssign, index, line, col, 2); } return factory.create(TokenType.Mod, index, line, col); case '&': switch (next) { case '&': in_stream.read(); return factory.create(TokenType.LogicalAnd, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.BitwiseAndAssign, index, line, col, 2); } return factory.create(TokenType.BitwiseAnd, index, line, col); case '|': switch (next) { case '|': in_stream.read(); return factory.create(TokenType.LogicalOr, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.BitwiseOrAssign, index, line, col, 2); } return factory.create(TokenType.BitwiseOr, index, line, col); case '^': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.XorAssign, index, line, col, 2); case '^': in_stream.read(); if (in_stream.peek() == '=') { in_stream.read(); return factory.create(TokenType.PowAssign, index, line, col, 3); } return factory.create(TokenType.Pow, index, line, col, 2); } return factory.create(TokenType.Xor, index, line, col); case '!': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.NotEqual, index, line, col, 2); case '<': in_stream.read(); switch (in_stream.peek()) { case '=': in_stream.read(); return factory.create(TokenType.UnorderedOrGreater, index, line, col, 3); case '>': in_stream.read(); switch (in_stream.peek()) { case '=': in_stream.read(); return factory.create(TokenType.Unordered, index, line, col, 4); } return factory.create(TokenType.UnorderedOrEqual, index, line, col, 3); } return factory.create(TokenType.UnorderedGreaterOrEqual, index, line, col, 2); case '>': in_stream.read(); switch (in_stream.peek()) { case '=': in_stream.read(); return factory.create(TokenType.UnorderedOrLess, index, line, col, 3); default: break; } return factory.create(TokenType.UnorderedLessOrEqual, index, line, col, 2); } return factory.create(TokenType.Not, index, line, col); case '~': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.TildeAssign, index, line, col, 2); } return factory.create(TokenType.Tilde, index, line, col); case '=': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.Equal, index, line, col, 2); case '>': in_stream.read(); return factory.create(TokenType.GoesTo, index, line, col, 2); } return factory.create(TokenType.Assign, index, line, col); case '<': switch (next) { case '<': in_stream.read(); switch (in_stream.peek()) { case '=': in_stream.read(); return factory.create(TokenType.ShiftLeftAssign, index, line, col, 3); default: break; } return factory.create(TokenType.ShiftLeft, index, line, col, 2); case '>': in_stream.read(); switch (in_stream.peek()) { case '=': in_stream.read(); return factory.create(TokenType.LessEqualOrGreater, index, line, col, 3); default: break; } return factory.create(TokenType.LessOrGreater, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.LessEqual, index, line, col, 2); } return factory.create(TokenType.LessThan, index, line, col); case '>': switch (next) { case '>': in_stream.read(); int p = in_stream.peek(); if (p != -1) { switch ((char)p) { case '=': in_stream.read(); return factory.create(TokenType.ShiftRightAssign, index, line, col, 3); case '>': in_stream.read(); int q = in_stream.peek(); if (q != -1 && q == '=') { in_stream.read(); return factory.create(TokenType.TripleRightShiftAssign, index, line, col, 4); } return factory.create(TokenType.ShiftRightUnsigned, index, line, col, 3); } } return factory.create(TokenType.ShiftRight, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.GreaterEqual, index, line, col, 2); } return factory.create(TokenType.GreaterThan, index, line, col); case '?': return factory.create(TokenType.Question, index, line, col); case '$': return factory.create(TokenType.Dollar, index, line, col); case ';': return factory.create(TokenType.Semicolon, index, line, col); case ':': return factory.create(TokenType.Colon, index, line, col); case ',': return factory.create(TokenType.Comma, index, line, col); case '.': if (next == '.') { in_stream.read(); int p = in_stream.peek(); if (p != -1 && p == '.') { in_stream.read(); return factory.create(TokenType.TripleDot, index, line, col, 3); } return factory.create(TokenType.DoubleDot, index, line, col, 2); } return factory.create(TokenType.Dot, index, line, col); case ')': return factory.create(TokenType.CloseParenthesis, index, line, col); case '(': return factory.create(TokenType.OpenParenthesis, index, line, col); case ']': return factory.create(TokenType.CloseSquareBracket, index, line, col); case '[': return factory.create(TokenType.OpenSquareBracket, index, line, col); case '}': return factory.create(TokenType.CloseCurlyBrace, index, line, col); case '{': return factory.create(TokenType.OpenCurlyBrace, index, line, col); case '#': return factory.create(TokenType.Hash, index, line, col); default: return null; } } LexOperator(TokenFactory factory, LexerStream in_stream); Token read(); }
LexOperator { public Token read() throws IOException, LexerException { int index = in_stream.getPosition(); int line = in_stream.getLine(); int col = in_stream.getCol(); int next = in_stream.peek(); if (next == -1) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Unexpected end of input stream when parsing operator"); } if (next == '.') { int tmp = in_stream.peek(2); if (tmp > 0 && Character.isDigit(tmp)) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Literal float encountered when operator '.' expected"); } } char cur = (char)in_stream.read(); next = in_stream.peek(); switch (cur) { case '+': switch (next) { case '+': in_stream.read(); return factory.create(TokenType.Increment, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.PlusAssign, index, line, col, 2); } return factory.create(TokenType.Plus, index, line, col); case '-': switch (next) { case '-': in_stream.read(); return factory.create(TokenType.Decrement, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.MinusAssign, index, line, col, 2); } return factory.create(TokenType.Minus, index, line, col); case '*': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.TimesAssign, index, line, col, 2); default: break; } return factory.create(TokenType.Times, index, line, col); case '/': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.DivAssign, index, line, col, 2); } return factory.create(TokenType.Div, index, line, col); case '%': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.ModAssign, index, line, col, 2); } return factory.create(TokenType.Mod, index, line, col); case '&': switch (next) { case '&': in_stream.read(); return factory.create(TokenType.LogicalAnd, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.BitwiseAndAssign, index, line, col, 2); } return factory.create(TokenType.BitwiseAnd, index, line, col); case '|': switch (next) { case '|': in_stream.read(); return factory.create(TokenType.LogicalOr, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.BitwiseOrAssign, index, line, col, 2); } return factory.create(TokenType.BitwiseOr, index, line, col); case '^': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.XorAssign, index, line, col, 2); case '^': in_stream.read(); if (in_stream.peek() == '=') { in_stream.read(); return factory.create(TokenType.PowAssign, index, line, col, 3); } return factory.create(TokenType.Pow, index, line, col, 2); } return factory.create(TokenType.Xor, index, line, col); case '!': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.NotEqual, index, line, col, 2); case '<': in_stream.read(); switch (in_stream.peek()) { case '=': in_stream.read(); return factory.create(TokenType.UnorderedOrGreater, index, line, col, 3); case '>': in_stream.read(); switch (in_stream.peek()) { case '=': in_stream.read(); return factory.create(TokenType.Unordered, index, line, col, 4); } return factory.create(TokenType.UnorderedOrEqual, index, line, col, 3); } return factory.create(TokenType.UnorderedGreaterOrEqual, index, line, col, 2); case '>': in_stream.read(); switch (in_stream.peek()) { case '=': in_stream.read(); return factory.create(TokenType.UnorderedOrLess, index, line, col, 3); default: break; } return factory.create(TokenType.UnorderedLessOrEqual, index, line, col, 2); } return factory.create(TokenType.Not, index, line, col); case '~': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.TildeAssign, index, line, col, 2); } return factory.create(TokenType.Tilde, index, line, col); case '=': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.Equal, index, line, col, 2); case '>': in_stream.read(); return factory.create(TokenType.GoesTo, index, line, col, 2); } return factory.create(TokenType.Assign, index, line, col); case '<': switch (next) { case '<': in_stream.read(); switch (in_stream.peek()) { case '=': in_stream.read(); return factory.create(TokenType.ShiftLeftAssign, index, line, col, 3); default: break; } return factory.create(TokenType.ShiftLeft, index, line, col, 2); case '>': in_stream.read(); switch (in_stream.peek()) { case '=': in_stream.read(); return factory.create(TokenType.LessEqualOrGreater, index, line, col, 3); default: break; } return factory.create(TokenType.LessOrGreater, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.LessEqual, index, line, col, 2); } return factory.create(TokenType.LessThan, index, line, col); case '>': switch (next) { case '>': in_stream.read(); int p = in_stream.peek(); if (p != -1) { switch ((char)p) { case '=': in_stream.read(); return factory.create(TokenType.ShiftRightAssign, index, line, col, 3); case '>': in_stream.read(); int q = in_stream.peek(); if (q != -1 && q == '=') { in_stream.read(); return factory.create(TokenType.TripleRightShiftAssign, index, line, col, 4); } return factory.create(TokenType.ShiftRightUnsigned, index, line, col, 3); } } return factory.create(TokenType.ShiftRight, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.GreaterEqual, index, line, col, 2); } return factory.create(TokenType.GreaterThan, index, line, col); case '?': return factory.create(TokenType.Question, index, line, col); case '$': return factory.create(TokenType.Dollar, index, line, col); case ';': return factory.create(TokenType.Semicolon, index, line, col); case ':': return factory.create(TokenType.Colon, index, line, col); case ',': return factory.create(TokenType.Comma, index, line, col); case '.': if (next == '.') { in_stream.read(); int p = in_stream.peek(); if (p != -1 && p == '.') { in_stream.read(); return factory.create(TokenType.TripleDot, index, line, col, 3); } return factory.create(TokenType.DoubleDot, index, line, col, 2); } return factory.create(TokenType.Dot, index, line, col); case ')': return factory.create(TokenType.CloseParenthesis, index, line, col); case '(': return factory.create(TokenType.OpenParenthesis, index, line, col); case ']': return factory.create(TokenType.CloseSquareBracket, index, line, col); case '[': return factory.create(TokenType.OpenSquareBracket, index, line, col); case '}': return factory.create(TokenType.CloseCurlyBrace, index, line, col); case '{': return factory.create(TokenType.OpenCurlyBrace, index, line, col); case '#': return factory.create(TokenType.Hash, index, line, col); default: return null; } } LexOperator(TokenFactory factory, LexerStream in_stream); Token read(); }
@Test public void sub() throws Exception { String str = "a-=b-- -c"; LexerStream ls = new LexerStream(new StringReader(str)); LexOperator lex = new LexOperator(new BaseTokenFactory(), ls); ls.read(); Token tok = lex.read(); assertEquals(TokenType.MinusAssign, tok.getType()); assertEquals(1, tok.getLine()); assertEquals(2, tok.getCol()); assertEquals(1, tok.getEndLine()); assertEquals(4, tok.getEndCol()); ls.read(); tok = lex.read(); assertEquals(TokenType.Decrement, tok.getType()); assertEquals(1, tok.getLine()); assertEquals(5, tok.getCol()); assertEquals(1, tok.getEndLine()); assertEquals(7, tok.getEndCol()); ls.read(); tok = lex.read(); assertEquals(TokenType.Minus, tok.getType()); assertEquals(1, tok.getLine()); assertEquals(8, tok.getCol()); assertEquals(1, tok.getEndLine()); assertEquals(9, tok.getEndCol()); assertEquals('c', ls.read()); assertEquals(-1, ls.read()); }
public Token read() throws IOException, LexerException { int index = in_stream.getPosition(); int line = in_stream.getLine(); int col = in_stream.getCol(); int next = in_stream.peek(); if (next == -1) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Unexpected end of input stream when parsing operator"); } if (next == '.') { int tmp = in_stream.peek(2); if (tmp > 0 && Character.isDigit(tmp)) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Literal float encountered when operator '.' expected"); } } char cur = (char)in_stream.read(); next = in_stream.peek(); switch (cur) { case '+': switch (next) { case '+': in_stream.read(); return factory.create(TokenType.Increment, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.PlusAssign, index, line, col, 2); } return factory.create(TokenType.Plus, index, line, col); case '-': switch (next) { case '-': in_stream.read(); return factory.create(TokenType.Decrement, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.MinusAssign, index, line, col, 2); } return factory.create(TokenType.Minus, index, line, col); case '*': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.TimesAssign, index, line, col, 2); default: break; } return factory.create(TokenType.Times, index, line, col); case '/': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.DivAssign, index, line, col, 2); } return factory.create(TokenType.Div, index, line, col); case '%': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.ModAssign, index, line, col, 2); } return factory.create(TokenType.Mod, index, line, col); case '&': switch (next) { case '&': in_stream.read(); return factory.create(TokenType.LogicalAnd, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.BitwiseAndAssign, index, line, col, 2); } return factory.create(TokenType.BitwiseAnd, index, line, col); case '|': switch (next) { case '|': in_stream.read(); return factory.create(TokenType.LogicalOr, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.BitwiseOrAssign, index, line, col, 2); } return factory.create(TokenType.BitwiseOr, index, line, col); case '^': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.XorAssign, index, line, col, 2); case '^': in_stream.read(); if (in_stream.peek() == '=') { in_stream.read(); return factory.create(TokenType.PowAssign, index, line, col, 3); } return factory.create(TokenType.Pow, index, line, col, 2); } return factory.create(TokenType.Xor, index, line, col); case '!': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.NotEqual, index, line, col, 2); case '<': in_stream.read(); switch (in_stream.peek()) { case '=': in_stream.read(); return factory.create(TokenType.UnorderedOrGreater, index, line, col, 3); case '>': in_stream.read(); switch (in_stream.peek()) { case '=': in_stream.read(); return factory.create(TokenType.Unordered, index, line, col, 4); } return factory.create(TokenType.UnorderedOrEqual, index, line, col, 3); } return factory.create(TokenType.UnorderedGreaterOrEqual, index, line, col, 2); case '>': in_stream.read(); switch (in_stream.peek()) { case '=': in_stream.read(); return factory.create(TokenType.UnorderedOrLess, index, line, col, 3); default: break; } return factory.create(TokenType.UnorderedLessOrEqual, index, line, col, 2); } return factory.create(TokenType.Not, index, line, col); case '~': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.TildeAssign, index, line, col, 2); } return factory.create(TokenType.Tilde, index, line, col); case '=': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.Equal, index, line, col, 2); case '>': in_stream.read(); return factory.create(TokenType.GoesTo, index, line, col, 2); } return factory.create(TokenType.Assign, index, line, col); case '<': switch (next) { case '<': in_stream.read(); switch (in_stream.peek()) { case '=': in_stream.read(); return factory.create(TokenType.ShiftLeftAssign, index, line, col, 3); default: break; } return factory.create(TokenType.ShiftLeft, index, line, col, 2); case '>': in_stream.read(); switch (in_stream.peek()) { case '=': in_stream.read(); return factory.create(TokenType.LessEqualOrGreater, index, line, col, 3); default: break; } return factory.create(TokenType.LessOrGreater, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.LessEqual, index, line, col, 2); } return factory.create(TokenType.LessThan, index, line, col); case '>': switch (next) { case '>': in_stream.read(); int p = in_stream.peek(); if (p != -1) { switch ((char)p) { case '=': in_stream.read(); return factory.create(TokenType.ShiftRightAssign, index, line, col, 3); case '>': in_stream.read(); int q = in_stream.peek(); if (q != -1 && q == '=') { in_stream.read(); return factory.create(TokenType.TripleRightShiftAssign, index, line, col, 4); } return factory.create(TokenType.ShiftRightUnsigned, index, line, col, 3); } } return factory.create(TokenType.ShiftRight, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.GreaterEqual, index, line, col, 2); } return factory.create(TokenType.GreaterThan, index, line, col); case '?': return factory.create(TokenType.Question, index, line, col); case '$': return factory.create(TokenType.Dollar, index, line, col); case ';': return factory.create(TokenType.Semicolon, index, line, col); case ':': return factory.create(TokenType.Colon, index, line, col); case ',': return factory.create(TokenType.Comma, index, line, col); case '.': if (next == '.') { in_stream.read(); int p = in_stream.peek(); if (p != -1 && p == '.') { in_stream.read(); return factory.create(TokenType.TripleDot, index, line, col, 3); } return factory.create(TokenType.DoubleDot, index, line, col, 2); } return factory.create(TokenType.Dot, index, line, col); case ')': return factory.create(TokenType.CloseParenthesis, index, line, col); case '(': return factory.create(TokenType.OpenParenthesis, index, line, col); case ']': return factory.create(TokenType.CloseSquareBracket, index, line, col); case '[': return factory.create(TokenType.OpenSquareBracket, index, line, col); case '}': return factory.create(TokenType.CloseCurlyBrace, index, line, col); case '{': return factory.create(TokenType.OpenCurlyBrace, index, line, col); case '#': return factory.create(TokenType.Hash, index, line, col); default: return null; } }
LexOperator { public Token read() throws IOException, LexerException { int index = in_stream.getPosition(); int line = in_stream.getLine(); int col = in_stream.getCol(); int next = in_stream.peek(); if (next == -1) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Unexpected end of input stream when parsing operator"); } if (next == '.') { int tmp = in_stream.peek(2); if (tmp > 0 && Character.isDigit(tmp)) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Literal float encountered when operator '.' expected"); } } char cur = (char)in_stream.read(); next = in_stream.peek(); switch (cur) { case '+': switch (next) { case '+': in_stream.read(); return factory.create(TokenType.Increment, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.PlusAssign, index, line, col, 2); } return factory.create(TokenType.Plus, index, line, col); case '-': switch (next) { case '-': in_stream.read(); return factory.create(TokenType.Decrement, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.MinusAssign, index, line, col, 2); } return factory.create(TokenType.Minus, index, line, col); case '*': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.TimesAssign, index, line, col, 2); default: break; } return factory.create(TokenType.Times, index, line, col); case '/': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.DivAssign, index, line, col, 2); } return factory.create(TokenType.Div, index, line, col); case '%': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.ModAssign, index, line, col, 2); } return factory.create(TokenType.Mod, index, line, col); case '&': switch (next) { case '&': in_stream.read(); return factory.create(TokenType.LogicalAnd, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.BitwiseAndAssign, index, line, col, 2); } return factory.create(TokenType.BitwiseAnd, index, line, col); case '|': switch (next) { case '|': in_stream.read(); return factory.create(TokenType.LogicalOr, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.BitwiseOrAssign, index, line, col, 2); } return factory.create(TokenType.BitwiseOr, index, line, col); case '^': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.XorAssign, index, line, col, 2); case '^': in_stream.read(); if (in_stream.peek() == '=') { in_stream.read(); return factory.create(TokenType.PowAssign, index, line, col, 3); } return factory.create(TokenType.Pow, index, line, col, 2); } return factory.create(TokenType.Xor, index, line, col); case '!': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.NotEqual, index, line, col, 2); case '<': in_stream.read(); switch (in_stream.peek()) { case '=': in_stream.read(); return factory.create(TokenType.UnorderedOrGreater, index, line, col, 3); case '>': in_stream.read(); switch (in_stream.peek()) { case '=': in_stream.read(); return factory.create(TokenType.Unordered, index, line, col, 4); } return factory.create(TokenType.UnorderedOrEqual, index, line, col, 3); } return factory.create(TokenType.UnorderedGreaterOrEqual, index, line, col, 2); case '>': in_stream.read(); switch (in_stream.peek()) { case '=': in_stream.read(); return factory.create(TokenType.UnorderedOrLess, index, line, col, 3); default: break; } return factory.create(TokenType.UnorderedLessOrEqual, index, line, col, 2); } return factory.create(TokenType.Not, index, line, col); case '~': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.TildeAssign, index, line, col, 2); } return factory.create(TokenType.Tilde, index, line, col); case '=': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.Equal, index, line, col, 2); case '>': in_stream.read(); return factory.create(TokenType.GoesTo, index, line, col, 2); } return factory.create(TokenType.Assign, index, line, col); case '<': switch (next) { case '<': in_stream.read(); switch (in_stream.peek()) { case '=': in_stream.read(); return factory.create(TokenType.ShiftLeftAssign, index, line, col, 3); default: break; } return factory.create(TokenType.ShiftLeft, index, line, col, 2); case '>': in_stream.read(); switch (in_stream.peek()) { case '=': in_stream.read(); return factory.create(TokenType.LessEqualOrGreater, index, line, col, 3); default: break; } return factory.create(TokenType.LessOrGreater, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.LessEqual, index, line, col, 2); } return factory.create(TokenType.LessThan, index, line, col); case '>': switch (next) { case '>': in_stream.read(); int p = in_stream.peek(); if (p != -1) { switch ((char)p) { case '=': in_stream.read(); return factory.create(TokenType.ShiftRightAssign, index, line, col, 3); case '>': in_stream.read(); int q = in_stream.peek(); if (q != -1 && q == '=') { in_stream.read(); return factory.create(TokenType.TripleRightShiftAssign, index, line, col, 4); } return factory.create(TokenType.ShiftRightUnsigned, index, line, col, 3); } } return factory.create(TokenType.ShiftRight, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.GreaterEqual, index, line, col, 2); } return factory.create(TokenType.GreaterThan, index, line, col); case '?': return factory.create(TokenType.Question, index, line, col); case '$': return factory.create(TokenType.Dollar, index, line, col); case ';': return factory.create(TokenType.Semicolon, index, line, col); case ':': return factory.create(TokenType.Colon, index, line, col); case ',': return factory.create(TokenType.Comma, index, line, col); case '.': if (next == '.') { in_stream.read(); int p = in_stream.peek(); if (p != -1 && p == '.') { in_stream.read(); return factory.create(TokenType.TripleDot, index, line, col, 3); } return factory.create(TokenType.DoubleDot, index, line, col, 2); } return factory.create(TokenType.Dot, index, line, col); case ')': return factory.create(TokenType.CloseParenthesis, index, line, col); case '(': return factory.create(TokenType.OpenParenthesis, index, line, col); case ']': return factory.create(TokenType.CloseSquareBracket, index, line, col); case '[': return factory.create(TokenType.OpenSquareBracket, index, line, col); case '}': return factory.create(TokenType.CloseCurlyBrace, index, line, col); case '{': return factory.create(TokenType.OpenCurlyBrace, index, line, col); case '#': return factory.create(TokenType.Hash, index, line, col); default: return null; } } }
LexOperator { public Token read() throws IOException, LexerException { int index = in_stream.getPosition(); int line = in_stream.getLine(); int col = in_stream.getCol(); int next = in_stream.peek(); if (next == -1) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Unexpected end of input stream when parsing operator"); } if (next == '.') { int tmp = in_stream.peek(2); if (tmp > 0 && Character.isDigit(tmp)) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Literal float encountered when operator '.' expected"); } } char cur = (char)in_stream.read(); next = in_stream.peek(); switch (cur) { case '+': switch (next) { case '+': in_stream.read(); return factory.create(TokenType.Increment, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.PlusAssign, index, line, col, 2); } return factory.create(TokenType.Plus, index, line, col); case '-': switch (next) { case '-': in_stream.read(); return factory.create(TokenType.Decrement, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.MinusAssign, index, line, col, 2); } return factory.create(TokenType.Minus, index, line, col); case '*': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.TimesAssign, index, line, col, 2); default: break; } return factory.create(TokenType.Times, index, line, col); case '/': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.DivAssign, index, line, col, 2); } return factory.create(TokenType.Div, index, line, col); case '%': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.ModAssign, index, line, col, 2); } return factory.create(TokenType.Mod, index, line, col); case '&': switch (next) { case '&': in_stream.read(); return factory.create(TokenType.LogicalAnd, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.BitwiseAndAssign, index, line, col, 2); } return factory.create(TokenType.BitwiseAnd, index, line, col); case '|': switch (next) { case '|': in_stream.read(); return factory.create(TokenType.LogicalOr, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.BitwiseOrAssign, index, line, col, 2); } return factory.create(TokenType.BitwiseOr, index, line, col); case '^': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.XorAssign, index, line, col, 2); case '^': in_stream.read(); if (in_stream.peek() == '=') { in_stream.read(); return factory.create(TokenType.PowAssign, index, line, col, 3); } return factory.create(TokenType.Pow, index, line, col, 2); } return factory.create(TokenType.Xor, index, line, col); case '!': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.NotEqual, index, line, col, 2); case '<': in_stream.read(); switch (in_stream.peek()) { case '=': in_stream.read(); return factory.create(TokenType.UnorderedOrGreater, index, line, col, 3); case '>': in_stream.read(); switch (in_stream.peek()) { case '=': in_stream.read(); return factory.create(TokenType.Unordered, index, line, col, 4); } return factory.create(TokenType.UnorderedOrEqual, index, line, col, 3); } return factory.create(TokenType.UnorderedGreaterOrEqual, index, line, col, 2); case '>': in_stream.read(); switch (in_stream.peek()) { case '=': in_stream.read(); return factory.create(TokenType.UnorderedOrLess, index, line, col, 3); default: break; } return factory.create(TokenType.UnorderedLessOrEqual, index, line, col, 2); } return factory.create(TokenType.Not, index, line, col); case '~': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.TildeAssign, index, line, col, 2); } return factory.create(TokenType.Tilde, index, line, col); case '=': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.Equal, index, line, col, 2); case '>': in_stream.read(); return factory.create(TokenType.GoesTo, index, line, col, 2); } return factory.create(TokenType.Assign, index, line, col); case '<': switch (next) { case '<': in_stream.read(); switch (in_stream.peek()) { case '=': in_stream.read(); return factory.create(TokenType.ShiftLeftAssign, index, line, col, 3); default: break; } return factory.create(TokenType.ShiftLeft, index, line, col, 2); case '>': in_stream.read(); switch (in_stream.peek()) { case '=': in_stream.read(); return factory.create(TokenType.LessEqualOrGreater, index, line, col, 3); default: break; } return factory.create(TokenType.LessOrGreater, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.LessEqual, index, line, col, 2); } return factory.create(TokenType.LessThan, index, line, col); case '>': switch (next) { case '>': in_stream.read(); int p = in_stream.peek(); if (p != -1) { switch ((char)p) { case '=': in_stream.read(); return factory.create(TokenType.ShiftRightAssign, index, line, col, 3); case '>': in_stream.read(); int q = in_stream.peek(); if (q != -1 && q == '=') { in_stream.read(); return factory.create(TokenType.TripleRightShiftAssign, index, line, col, 4); } return factory.create(TokenType.ShiftRightUnsigned, index, line, col, 3); } } return factory.create(TokenType.ShiftRight, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.GreaterEqual, index, line, col, 2); } return factory.create(TokenType.GreaterThan, index, line, col); case '?': return factory.create(TokenType.Question, index, line, col); case '$': return factory.create(TokenType.Dollar, index, line, col); case ';': return factory.create(TokenType.Semicolon, index, line, col); case ':': return factory.create(TokenType.Colon, index, line, col); case ',': return factory.create(TokenType.Comma, index, line, col); case '.': if (next == '.') { in_stream.read(); int p = in_stream.peek(); if (p != -1 && p == '.') { in_stream.read(); return factory.create(TokenType.TripleDot, index, line, col, 3); } return factory.create(TokenType.DoubleDot, index, line, col, 2); } return factory.create(TokenType.Dot, index, line, col); case ')': return factory.create(TokenType.CloseParenthesis, index, line, col); case '(': return factory.create(TokenType.OpenParenthesis, index, line, col); case ']': return factory.create(TokenType.CloseSquareBracket, index, line, col); case '[': return factory.create(TokenType.OpenSquareBracket, index, line, col); case '}': return factory.create(TokenType.CloseCurlyBrace, index, line, col); case '{': return factory.create(TokenType.OpenCurlyBrace, index, line, col); case '#': return factory.create(TokenType.Hash, index, line, col); default: return null; } } LexOperator(TokenFactory factory, LexerStream in_stream); }
LexOperator { public Token read() throws IOException, LexerException { int index = in_stream.getPosition(); int line = in_stream.getLine(); int col = in_stream.getCol(); int next = in_stream.peek(); if (next == -1) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Unexpected end of input stream when parsing operator"); } if (next == '.') { int tmp = in_stream.peek(2); if (tmp > 0 && Character.isDigit(tmp)) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Literal float encountered when operator '.' expected"); } } char cur = (char)in_stream.read(); next = in_stream.peek(); switch (cur) { case '+': switch (next) { case '+': in_stream.read(); return factory.create(TokenType.Increment, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.PlusAssign, index, line, col, 2); } return factory.create(TokenType.Plus, index, line, col); case '-': switch (next) { case '-': in_stream.read(); return factory.create(TokenType.Decrement, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.MinusAssign, index, line, col, 2); } return factory.create(TokenType.Minus, index, line, col); case '*': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.TimesAssign, index, line, col, 2); default: break; } return factory.create(TokenType.Times, index, line, col); case '/': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.DivAssign, index, line, col, 2); } return factory.create(TokenType.Div, index, line, col); case '%': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.ModAssign, index, line, col, 2); } return factory.create(TokenType.Mod, index, line, col); case '&': switch (next) { case '&': in_stream.read(); return factory.create(TokenType.LogicalAnd, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.BitwiseAndAssign, index, line, col, 2); } return factory.create(TokenType.BitwiseAnd, index, line, col); case '|': switch (next) { case '|': in_stream.read(); return factory.create(TokenType.LogicalOr, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.BitwiseOrAssign, index, line, col, 2); } return factory.create(TokenType.BitwiseOr, index, line, col); case '^': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.XorAssign, index, line, col, 2); case '^': in_stream.read(); if (in_stream.peek() == '=') { in_stream.read(); return factory.create(TokenType.PowAssign, index, line, col, 3); } return factory.create(TokenType.Pow, index, line, col, 2); } return factory.create(TokenType.Xor, index, line, col); case '!': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.NotEqual, index, line, col, 2); case '<': in_stream.read(); switch (in_stream.peek()) { case '=': in_stream.read(); return factory.create(TokenType.UnorderedOrGreater, index, line, col, 3); case '>': in_stream.read(); switch (in_stream.peek()) { case '=': in_stream.read(); return factory.create(TokenType.Unordered, index, line, col, 4); } return factory.create(TokenType.UnorderedOrEqual, index, line, col, 3); } return factory.create(TokenType.UnorderedGreaterOrEqual, index, line, col, 2); case '>': in_stream.read(); switch (in_stream.peek()) { case '=': in_stream.read(); return factory.create(TokenType.UnorderedOrLess, index, line, col, 3); default: break; } return factory.create(TokenType.UnorderedLessOrEqual, index, line, col, 2); } return factory.create(TokenType.Not, index, line, col); case '~': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.TildeAssign, index, line, col, 2); } return factory.create(TokenType.Tilde, index, line, col); case '=': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.Equal, index, line, col, 2); case '>': in_stream.read(); return factory.create(TokenType.GoesTo, index, line, col, 2); } return factory.create(TokenType.Assign, index, line, col); case '<': switch (next) { case '<': in_stream.read(); switch (in_stream.peek()) { case '=': in_stream.read(); return factory.create(TokenType.ShiftLeftAssign, index, line, col, 3); default: break; } return factory.create(TokenType.ShiftLeft, index, line, col, 2); case '>': in_stream.read(); switch (in_stream.peek()) { case '=': in_stream.read(); return factory.create(TokenType.LessEqualOrGreater, index, line, col, 3); default: break; } return factory.create(TokenType.LessOrGreater, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.LessEqual, index, line, col, 2); } return factory.create(TokenType.LessThan, index, line, col); case '>': switch (next) { case '>': in_stream.read(); int p = in_stream.peek(); if (p != -1) { switch ((char)p) { case '=': in_stream.read(); return factory.create(TokenType.ShiftRightAssign, index, line, col, 3); case '>': in_stream.read(); int q = in_stream.peek(); if (q != -1 && q == '=') { in_stream.read(); return factory.create(TokenType.TripleRightShiftAssign, index, line, col, 4); } return factory.create(TokenType.ShiftRightUnsigned, index, line, col, 3); } } return factory.create(TokenType.ShiftRight, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.GreaterEqual, index, line, col, 2); } return factory.create(TokenType.GreaterThan, index, line, col); case '?': return factory.create(TokenType.Question, index, line, col); case '$': return factory.create(TokenType.Dollar, index, line, col); case ';': return factory.create(TokenType.Semicolon, index, line, col); case ':': return factory.create(TokenType.Colon, index, line, col); case ',': return factory.create(TokenType.Comma, index, line, col); case '.': if (next == '.') { in_stream.read(); int p = in_stream.peek(); if (p != -1 && p == '.') { in_stream.read(); return factory.create(TokenType.TripleDot, index, line, col, 3); } return factory.create(TokenType.DoubleDot, index, line, col, 2); } return factory.create(TokenType.Dot, index, line, col); case ')': return factory.create(TokenType.CloseParenthesis, index, line, col); case '(': return factory.create(TokenType.OpenParenthesis, index, line, col); case ']': return factory.create(TokenType.CloseSquareBracket, index, line, col); case '[': return factory.create(TokenType.OpenSquareBracket, index, line, col); case '}': return factory.create(TokenType.CloseCurlyBrace, index, line, col); case '{': return factory.create(TokenType.OpenCurlyBrace, index, line, col); case '#': return factory.create(TokenType.Hash, index, line, col); default: return null; } } LexOperator(TokenFactory factory, LexerStream in_stream); Token read(); }
LexOperator { public Token read() throws IOException, LexerException { int index = in_stream.getPosition(); int line = in_stream.getLine(); int col = in_stream.getCol(); int next = in_stream.peek(); if (next == -1) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Unexpected end of input stream when parsing operator"); } if (next == '.') { int tmp = in_stream.peek(2); if (tmp > 0 && Character.isDigit(tmp)) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Literal float encountered when operator '.' expected"); } } char cur = (char)in_stream.read(); next = in_stream.peek(); switch (cur) { case '+': switch (next) { case '+': in_stream.read(); return factory.create(TokenType.Increment, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.PlusAssign, index, line, col, 2); } return factory.create(TokenType.Plus, index, line, col); case '-': switch (next) { case '-': in_stream.read(); return factory.create(TokenType.Decrement, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.MinusAssign, index, line, col, 2); } return factory.create(TokenType.Minus, index, line, col); case '*': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.TimesAssign, index, line, col, 2); default: break; } return factory.create(TokenType.Times, index, line, col); case '/': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.DivAssign, index, line, col, 2); } return factory.create(TokenType.Div, index, line, col); case '%': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.ModAssign, index, line, col, 2); } return factory.create(TokenType.Mod, index, line, col); case '&': switch (next) { case '&': in_stream.read(); return factory.create(TokenType.LogicalAnd, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.BitwiseAndAssign, index, line, col, 2); } return factory.create(TokenType.BitwiseAnd, index, line, col); case '|': switch (next) { case '|': in_stream.read(); return factory.create(TokenType.LogicalOr, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.BitwiseOrAssign, index, line, col, 2); } return factory.create(TokenType.BitwiseOr, index, line, col); case '^': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.XorAssign, index, line, col, 2); case '^': in_stream.read(); if (in_stream.peek() == '=') { in_stream.read(); return factory.create(TokenType.PowAssign, index, line, col, 3); } return factory.create(TokenType.Pow, index, line, col, 2); } return factory.create(TokenType.Xor, index, line, col); case '!': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.NotEqual, index, line, col, 2); case '<': in_stream.read(); switch (in_stream.peek()) { case '=': in_stream.read(); return factory.create(TokenType.UnorderedOrGreater, index, line, col, 3); case '>': in_stream.read(); switch (in_stream.peek()) { case '=': in_stream.read(); return factory.create(TokenType.Unordered, index, line, col, 4); } return factory.create(TokenType.UnorderedOrEqual, index, line, col, 3); } return factory.create(TokenType.UnorderedGreaterOrEqual, index, line, col, 2); case '>': in_stream.read(); switch (in_stream.peek()) { case '=': in_stream.read(); return factory.create(TokenType.UnorderedOrLess, index, line, col, 3); default: break; } return factory.create(TokenType.UnorderedLessOrEqual, index, line, col, 2); } return factory.create(TokenType.Not, index, line, col); case '~': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.TildeAssign, index, line, col, 2); } return factory.create(TokenType.Tilde, index, line, col); case '=': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.Equal, index, line, col, 2); case '>': in_stream.read(); return factory.create(TokenType.GoesTo, index, line, col, 2); } return factory.create(TokenType.Assign, index, line, col); case '<': switch (next) { case '<': in_stream.read(); switch (in_stream.peek()) { case '=': in_stream.read(); return factory.create(TokenType.ShiftLeftAssign, index, line, col, 3); default: break; } return factory.create(TokenType.ShiftLeft, index, line, col, 2); case '>': in_stream.read(); switch (in_stream.peek()) { case '=': in_stream.read(); return factory.create(TokenType.LessEqualOrGreater, index, line, col, 3); default: break; } return factory.create(TokenType.LessOrGreater, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.LessEqual, index, line, col, 2); } return factory.create(TokenType.LessThan, index, line, col); case '>': switch (next) { case '>': in_stream.read(); int p = in_stream.peek(); if (p != -1) { switch ((char)p) { case '=': in_stream.read(); return factory.create(TokenType.ShiftRightAssign, index, line, col, 3); case '>': in_stream.read(); int q = in_stream.peek(); if (q != -1 && q == '=') { in_stream.read(); return factory.create(TokenType.TripleRightShiftAssign, index, line, col, 4); } return factory.create(TokenType.ShiftRightUnsigned, index, line, col, 3); } } return factory.create(TokenType.ShiftRight, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.GreaterEqual, index, line, col, 2); } return factory.create(TokenType.GreaterThan, index, line, col); case '?': return factory.create(TokenType.Question, index, line, col); case '$': return factory.create(TokenType.Dollar, index, line, col); case ';': return factory.create(TokenType.Semicolon, index, line, col); case ':': return factory.create(TokenType.Colon, index, line, col); case ',': return factory.create(TokenType.Comma, index, line, col); case '.': if (next == '.') { in_stream.read(); int p = in_stream.peek(); if (p != -1 && p == '.') { in_stream.read(); return factory.create(TokenType.TripleDot, index, line, col, 3); } return factory.create(TokenType.DoubleDot, index, line, col, 2); } return factory.create(TokenType.Dot, index, line, col); case ')': return factory.create(TokenType.CloseParenthesis, index, line, col); case '(': return factory.create(TokenType.OpenParenthesis, index, line, col); case ']': return factory.create(TokenType.CloseSquareBracket, index, line, col); case '[': return factory.create(TokenType.OpenSquareBracket, index, line, col); case '}': return factory.create(TokenType.CloseCurlyBrace, index, line, col); case '{': return factory.create(TokenType.OpenCurlyBrace, index, line, col); case '#': return factory.create(TokenType.Hash, index, line, col); default: return null; } } LexOperator(TokenFactory factory, LexerStream in_stream); Token read(); }
@Test public void mul() throws Exception { String str = "a *=b*c;"; LexerStream ls = new LexerStream(new StringReader(str)); LexOperator lex = new LexOperator(new BaseTokenFactory(), ls); ls.read(); ls.read(); Token tok = lex.read(); assertEquals(TokenType.TimesAssign, tok.getType()); assertEquals(1, tok.getLine()); assertEquals(3, tok.getCol()); assertEquals(1, tok.getEndLine()); assertEquals(5, tok.getEndCol()); ls.read(); tok = lex.read(); assertEquals(TokenType.Times, tok.getType()); assertEquals(1, tok.getLine()); assertEquals(6, tok.getCol()); assertEquals(1, tok.getEndLine()); assertEquals(7, tok.getEndCol()); assertEquals('c', ls.read()); tok = lex.read(); assertEquals(TokenType.Semicolon, tok.getType()); assertEquals(-1, ls.read()); }
public Token read() throws IOException, LexerException { int index = in_stream.getPosition(); int line = in_stream.getLine(); int col = in_stream.getCol(); int next = in_stream.peek(); if (next == -1) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Unexpected end of input stream when parsing operator"); } if (next == '.') { int tmp = in_stream.peek(2); if (tmp > 0 && Character.isDigit(tmp)) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Literal float encountered when operator '.' expected"); } } char cur = (char)in_stream.read(); next = in_stream.peek(); switch (cur) { case '+': switch (next) { case '+': in_stream.read(); return factory.create(TokenType.Increment, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.PlusAssign, index, line, col, 2); } return factory.create(TokenType.Plus, index, line, col); case '-': switch (next) { case '-': in_stream.read(); return factory.create(TokenType.Decrement, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.MinusAssign, index, line, col, 2); } return factory.create(TokenType.Minus, index, line, col); case '*': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.TimesAssign, index, line, col, 2); default: break; } return factory.create(TokenType.Times, index, line, col); case '/': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.DivAssign, index, line, col, 2); } return factory.create(TokenType.Div, index, line, col); case '%': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.ModAssign, index, line, col, 2); } return factory.create(TokenType.Mod, index, line, col); case '&': switch (next) { case '&': in_stream.read(); return factory.create(TokenType.LogicalAnd, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.BitwiseAndAssign, index, line, col, 2); } return factory.create(TokenType.BitwiseAnd, index, line, col); case '|': switch (next) { case '|': in_stream.read(); return factory.create(TokenType.LogicalOr, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.BitwiseOrAssign, index, line, col, 2); } return factory.create(TokenType.BitwiseOr, index, line, col); case '^': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.XorAssign, index, line, col, 2); case '^': in_stream.read(); if (in_stream.peek() == '=') { in_stream.read(); return factory.create(TokenType.PowAssign, index, line, col, 3); } return factory.create(TokenType.Pow, index, line, col, 2); } return factory.create(TokenType.Xor, index, line, col); case '!': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.NotEqual, index, line, col, 2); case '<': in_stream.read(); switch (in_stream.peek()) { case '=': in_stream.read(); return factory.create(TokenType.UnorderedOrGreater, index, line, col, 3); case '>': in_stream.read(); switch (in_stream.peek()) { case '=': in_stream.read(); return factory.create(TokenType.Unordered, index, line, col, 4); } return factory.create(TokenType.UnorderedOrEqual, index, line, col, 3); } return factory.create(TokenType.UnorderedGreaterOrEqual, index, line, col, 2); case '>': in_stream.read(); switch (in_stream.peek()) { case '=': in_stream.read(); return factory.create(TokenType.UnorderedOrLess, index, line, col, 3); default: break; } return factory.create(TokenType.UnorderedLessOrEqual, index, line, col, 2); } return factory.create(TokenType.Not, index, line, col); case '~': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.TildeAssign, index, line, col, 2); } return factory.create(TokenType.Tilde, index, line, col); case '=': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.Equal, index, line, col, 2); case '>': in_stream.read(); return factory.create(TokenType.GoesTo, index, line, col, 2); } return factory.create(TokenType.Assign, index, line, col); case '<': switch (next) { case '<': in_stream.read(); switch (in_stream.peek()) { case '=': in_stream.read(); return factory.create(TokenType.ShiftLeftAssign, index, line, col, 3); default: break; } return factory.create(TokenType.ShiftLeft, index, line, col, 2); case '>': in_stream.read(); switch (in_stream.peek()) { case '=': in_stream.read(); return factory.create(TokenType.LessEqualOrGreater, index, line, col, 3); default: break; } return factory.create(TokenType.LessOrGreater, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.LessEqual, index, line, col, 2); } return factory.create(TokenType.LessThan, index, line, col); case '>': switch (next) { case '>': in_stream.read(); int p = in_stream.peek(); if (p != -1) { switch ((char)p) { case '=': in_stream.read(); return factory.create(TokenType.ShiftRightAssign, index, line, col, 3); case '>': in_stream.read(); int q = in_stream.peek(); if (q != -1 && q == '=') { in_stream.read(); return factory.create(TokenType.TripleRightShiftAssign, index, line, col, 4); } return factory.create(TokenType.ShiftRightUnsigned, index, line, col, 3); } } return factory.create(TokenType.ShiftRight, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.GreaterEqual, index, line, col, 2); } return factory.create(TokenType.GreaterThan, index, line, col); case '?': return factory.create(TokenType.Question, index, line, col); case '$': return factory.create(TokenType.Dollar, index, line, col); case ';': return factory.create(TokenType.Semicolon, index, line, col); case ':': return factory.create(TokenType.Colon, index, line, col); case ',': return factory.create(TokenType.Comma, index, line, col); case '.': if (next == '.') { in_stream.read(); int p = in_stream.peek(); if (p != -1 && p == '.') { in_stream.read(); return factory.create(TokenType.TripleDot, index, line, col, 3); } return factory.create(TokenType.DoubleDot, index, line, col, 2); } return factory.create(TokenType.Dot, index, line, col); case ')': return factory.create(TokenType.CloseParenthesis, index, line, col); case '(': return factory.create(TokenType.OpenParenthesis, index, line, col); case ']': return factory.create(TokenType.CloseSquareBracket, index, line, col); case '[': return factory.create(TokenType.OpenSquareBracket, index, line, col); case '}': return factory.create(TokenType.CloseCurlyBrace, index, line, col); case '{': return factory.create(TokenType.OpenCurlyBrace, index, line, col); case '#': return factory.create(TokenType.Hash, index, line, col); default: return null; } }
LexOperator { public Token read() throws IOException, LexerException { int index = in_stream.getPosition(); int line = in_stream.getLine(); int col = in_stream.getCol(); int next = in_stream.peek(); if (next == -1) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Unexpected end of input stream when parsing operator"); } if (next == '.') { int tmp = in_stream.peek(2); if (tmp > 0 && Character.isDigit(tmp)) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Literal float encountered when operator '.' expected"); } } char cur = (char)in_stream.read(); next = in_stream.peek(); switch (cur) { case '+': switch (next) { case '+': in_stream.read(); return factory.create(TokenType.Increment, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.PlusAssign, index, line, col, 2); } return factory.create(TokenType.Plus, index, line, col); case '-': switch (next) { case '-': in_stream.read(); return factory.create(TokenType.Decrement, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.MinusAssign, index, line, col, 2); } return factory.create(TokenType.Minus, index, line, col); case '*': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.TimesAssign, index, line, col, 2); default: break; } return factory.create(TokenType.Times, index, line, col); case '/': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.DivAssign, index, line, col, 2); } return factory.create(TokenType.Div, index, line, col); case '%': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.ModAssign, index, line, col, 2); } return factory.create(TokenType.Mod, index, line, col); case '&': switch (next) { case '&': in_stream.read(); return factory.create(TokenType.LogicalAnd, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.BitwiseAndAssign, index, line, col, 2); } return factory.create(TokenType.BitwiseAnd, index, line, col); case '|': switch (next) { case '|': in_stream.read(); return factory.create(TokenType.LogicalOr, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.BitwiseOrAssign, index, line, col, 2); } return factory.create(TokenType.BitwiseOr, index, line, col); case '^': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.XorAssign, index, line, col, 2); case '^': in_stream.read(); if (in_stream.peek() == '=') { in_stream.read(); return factory.create(TokenType.PowAssign, index, line, col, 3); } return factory.create(TokenType.Pow, index, line, col, 2); } return factory.create(TokenType.Xor, index, line, col); case '!': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.NotEqual, index, line, col, 2); case '<': in_stream.read(); switch (in_stream.peek()) { case '=': in_stream.read(); return factory.create(TokenType.UnorderedOrGreater, index, line, col, 3); case '>': in_stream.read(); switch (in_stream.peek()) { case '=': in_stream.read(); return factory.create(TokenType.Unordered, index, line, col, 4); } return factory.create(TokenType.UnorderedOrEqual, index, line, col, 3); } return factory.create(TokenType.UnorderedGreaterOrEqual, index, line, col, 2); case '>': in_stream.read(); switch (in_stream.peek()) { case '=': in_stream.read(); return factory.create(TokenType.UnorderedOrLess, index, line, col, 3); default: break; } return factory.create(TokenType.UnorderedLessOrEqual, index, line, col, 2); } return factory.create(TokenType.Not, index, line, col); case '~': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.TildeAssign, index, line, col, 2); } return factory.create(TokenType.Tilde, index, line, col); case '=': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.Equal, index, line, col, 2); case '>': in_stream.read(); return factory.create(TokenType.GoesTo, index, line, col, 2); } return factory.create(TokenType.Assign, index, line, col); case '<': switch (next) { case '<': in_stream.read(); switch (in_stream.peek()) { case '=': in_stream.read(); return factory.create(TokenType.ShiftLeftAssign, index, line, col, 3); default: break; } return factory.create(TokenType.ShiftLeft, index, line, col, 2); case '>': in_stream.read(); switch (in_stream.peek()) { case '=': in_stream.read(); return factory.create(TokenType.LessEqualOrGreater, index, line, col, 3); default: break; } return factory.create(TokenType.LessOrGreater, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.LessEqual, index, line, col, 2); } return factory.create(TokenType.LessThan, index, line, col); case '>': switch (next) { case '>': in_stream.read(); int p = in_stream.peek(); if (p != -1) { switch ((char)p) { case '=': in_stream.read(); return factory.create(TokenType.ShiftRightAssign, index, line, col, 3); case '>': in_stream.read(); int q = in_stream.peek(); if (q != -1 && q == '=') { in_stream.read(); return factory.create(TokenType.TripleRightShiftAssign, index, line, col, 4); } return factory.create(TokenType.ShiftRightUnsigned, index, line, col, 3); } } return factory.create(TokenType.ShiftRight, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.GreaterEqual, index, line, col, 2); } return factory.create(TokenType.GreaterThan, index, line, col); case '?': return factory.create(TokenType.Question, index, line, col); case '$': return factory.create(TokenType.Dollar, index, line, col); case ';': return factory.create(TokenType.Semicolon, index, line, col); case ':': return factory.create(TokenType.Colon, index, line, col); case ',': return factory.create(TokenType.Comma, index, line, col); case '.': if (next == '.') { in_stream.read(); int p = in_stream.peek(); if (p != -1 && p == '.') { in_stream.read(); return factory.create(TokenType.TripleDot, index, line, col, 3); } return factory.create(TokenType.DoubleDot, index, line, col, 2); } return factory.create(TokenType.Dot, index, line, col); case ')': return factory.create(TokenType.CloseParenthesis, index, line, col); case '(': return factory.create(TokenType.OpenParenthesis, index, line, col); case ']': return factory.create(TokenType.CloseSquareBracket, index, line, col); case '[': return factory.create(TokenType.OpenSquareBracket, index, line, col); case '}': return factory.create(TokenType.CloseCurlyBrace, index, line, col); case '{': return factory.create(TokenType.OpenCurlyBrace, index, line, col); case '#': return factory.create(TokenType.Hash, index, line, col); default: return null; } } }
LexOperator { public Token read() throws IOException, LexerException { int index = in_stream.getPosition(); int line = in_stream.getLine(); int col = in_stream.getCol(); int next = in_stream.peek(); if (next == -1) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Unexpected end of input stream when parsing operator"); } if (next == '.') { int tmp = in_stream.peek(2); if (tmp > 0 && Character.isDigit(tmp)) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Literal float encountered when operator '.' expected"); } } char cur = (char)in_stream.read(); next = in_stream.peek(); switch (cur) { case '+': switch (next) { case '+': in_stream.read(); return factory.create(TokenType.Increment, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.PlusAssign, index, line, col, 2); } return factory.create(TokenType.Plus, index, line, col); case '-': switch (next) { case '-': in_stream.read(); return factory.create(TokenType.Decrement, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.MinusAssign, index, line, col, 2); } return factory.create(TokenType.Minus, index, line, col); case '*': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.TimesAssign, index, line, col, 2); default: break; } return factory.create(TokenType.Times, index, line, col); case '/': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.DivAssign, index, line, col, 2); } return factory.create(TokenType.Div, index, line, col); case '%': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.ModAssign, index, line, col, 2); } return factory.create(TokenType.Mod, index, line, col); case '&': switch (next) { case '&': in_stream.read(); return factory.create(TokenType.LogicalAnd, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.BitwiseAndAssign, index, line, col, 2); } return factory.create(TokenType.BitwiseAnd, index, line, col); case '|': switch (next) { case '|': in_stream.read(); return factory.create(TokenType.LogicalOr, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.BitwiseOrAssign, index, line, col, 2); } return factory.create(TokenType.BitwiseOr, index, line, col); case '^': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.XorAssign, index, line, col, 2); case '^': in_stream.read(); if (in_stream.peek() == '=') { in_stream.read(); return factory.create(TokenType.PowAssign, index, line, col, 3); } return factory.create(TokenType.Pow, index, line, col, 2); } return factory.create(TokenType.Xor, index, line, col); case '!': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.NotEqual, index, line, col, 2); case '<': in_stream.read(); switch (in_stream.peek()) { case '=': in_stream.read(); return factory.create(TokenType.UnorderedOrGreater, index, line, col, 3); case '>': in_stream.read(); switch (in_stream.peek()) { case '=': in_stream.read(); return factory.create(TokenType.Unordered, index, line, col, 4); } return factory.create(TokenType.UnorderedOrEqual, index, line, col, 3); } return factory.create(TokenType.UnorderedGreaterOrEqual, index, line, col, 2); case '>': in_stream.read(); switch (in_stream.peek()) { case '=': in_stream.read(); return factory.create(TokenType.UnorderedOrLess, index, line, col, 3); default: break; } return factory.create(TokenType.UnorderedLessOrEqual, index, line, col, 2); } return factory.create(TokenType.Not, index, line, col); case '~': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.TildeAssign, index, line, col, 2); } return factory.create(TokenType.Tilde, index, line, col); case '=': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.Equal, index, line, col, 2); case '>': in_stream.read(); return factory.create(TokenType.GoesTo, index, line, col, 2); } return factory.create(TokenType.Assign, index, line, col); case '<': switch (next) { case '<': in_stream.read(); switch (in_stream.peek()) { case '=': in_stream.read(); return factory.create(TokenType.ShiftLeftAssign, index, line, col, 3); default: break; } return factory.create(TokenType.ShiftLeft, index, line, col, 2); case '>': in_stream.read(); switch (in_stream.peek()) { case '=': in_stream.read(); return factory.create(TokenType.LessEqualOrGreater, index, line, col, 3); default: break; } return factory.create(TokenType.LessOrGreater, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.LessEqual, index, line, col, 2); } return factory.create(TokenType.LessThan, index, line, col); case '>': switch (next) { case '>': in_stream.read(); int p = in_stream.peek(); if (p != -1) { switch ((char)p) { case '=': in_stream.read(); return factory.create(TokenType.ShiftRightAssign, index, line, col, 3); case '>': in_stream.read(); int q = in_stream.peek(); if (q != -1 && q == '=') { in_stream.read(); return factory.create(TokenType.TripleRightShiftAssign, index, line, col, 4); } return factory.create(TokenType.ShiftRightUnsigned, index, line, col, 3); } } return factory.create(TokenType.ShiftRight, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.GreaterEqual, index, line, col, 2); } return factory.create(TokenType.GreaterThan, index, line, col); case '?': return factory.create(TokenType.Question, index, line, col); case '$': return factory.create(TokenType.Dollar, index, line, col); case ';': return factory.create(TokenType.Semicolon, index, line, col); case ':': return factory.create(TokenType.Colon, index, line, col); case ',': return factory.create(TokenType.Comma, index, line, col); case '.': if (next == '.') { in_stream.read(); int p = in_stream.peek(); if (p != -1 && p == '.') { in_stream.read(); return factory.create(TokenType.TripleDot, index, line, col, 3); } return factory.create(TokenType.DoubleDot, index, line, col, 2); } return factory.create(TokenType.Dot, index, line, col); case ')': return factory.create(TokenType.CloseParenthesis, index, line, col); case '(': return factory.create(TokenType.OpenParenthesis, index, line, col); case ']': return factory.create(TokenType.CloseSquareBracket, index, line, col); case '[': return factory.create(TokenType.OpenSquareBracket, index, line, col); case '}': return factory.create(TokenType.CloseCurlyBrace, index, line, col); case '{': return factory.create(TokenType.OpenCurlyBrace, index, line, col); case '#': return factory.create(TokenType.Hash, index, line, col); default: return null; } } LexOperator(TokenFactory factory, LexerStream in_stream); }
LexOperator { public Token read() throws IOException, LexerException { int index = in_stream.getPosition(); int line = in_stream.getLine(); int col = in_stream.getCol(); int next = in_stream.peek(); if (next == -1) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Unexpected end of input stream when parsing operator"); } if (next == '.') { int tmp = in_stream.peek(2); if (tmp > 0 && Character.isDigit(tmp)) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Literal float encountered when operator '.' expected"); } } char cur = (char)in_stream.read(); next = in_stream.peek(); switch (cur) { case '+': switch (next) { case '+': in_stream.read(); return factory.create(TokenType.Increment, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.PlusAssign, index, line, col, 2); } return factory.create(TokenType.Plus, index, line, col); case '-': switch (next) { case '-': in_stream.read(); return factory.create(TokenType.Decrement, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.MinusAssign, index, line, col, 2); } return factory.create(TokenType.Minus, index, line, col); case '*': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.TimesAssign, index, line, col, 2); default: break; } return factory.create(TokenType.Times, index, line, col); case '/': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.DivAssign, index, line, col, 2); } return factory.create(TokenType.Div, index, line, col); case '%': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.ModAssign, index, line, col, 2); } return factory.create(TokenType.Mod, index, line, col); case '&': switch (next) { case '&': in_stream.read(); return factory.create(TokenType.LogicalAnd, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.BitwiseAndAssign, index, line, col, 2); } return factory.create(TokenType.BitwiseAnd, index, line, col); case '|': switch (next) { case '|': in_stream.read(); return factory.create(TokenType.LogicalOr, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.BitwiseOrAssign, index, line, col, 2); } return factory.create(TokenType.BitwiseOr, index, line, col); case '^': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.XorAssign, index, line, col, 2); case '^': in_stream.read(); if (in_stream.peek() == '=') { in_stream.read(); return factory.create(TokenType.PowAssign, index, line, col, 3); } return factory.create(TokenType.Pow, index, line, col, 2); } return factory.create(TokenType.Xor, index, line, col); case '!': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.NotEqual, index, line, col, 2); case '<': in_stream.read(); switch (in_stream.peek()) { case '=': in_stream.read(); return factory.create(TokenType.UnorderedOrGreater, index, line, col, 3); case '>': in_stream.read(); switch (in_stream.peek()) { case '=': in_stream.read(); return factory.create(TokenType.Unordered, index, line, col, 4); } return factory.create(TokenType.UnorderedOrEqual, index, line, col, 3); } return factory.create(TokenType.UnorderedGreaterOrEqual, index, line, col, 2); case '>': in_stream.read(); switch (in_stream.peek()) { case '=': in_stream.read(); return factory.create(TokenType.UnorderedOrLess, index, line, col, 3); default: break; } return factory.create(TokenType.UnorderedLessOrEqual, index, line, col, 2); } return factory.create(TokenType.Not, index, line, col); case '~': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.TildeAssign, index, line, col, 2); } return factory.create(TokenType.Tilde, index, line, col); case '=': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.Equal, index, line, col, 2); case '>': in_stream.read(); return factory.create(TokenType.GoesTo, index, line, col, 2); } return factory.create(TokenType.Assign, index, line, col); case '<': switch (next) { case '<': in_stream.read(); switch (in_stream.peek()) { case '=': in_stream.read(); return factory.create(TokenType.ShiftLeftAssign, index, line, col, 3); default: break; } return factory.create(TokenType.ShiftLeft, index, line, col, 2); case '>': in_stream.read(); switch (in_stream.peek()) { case '=': in_stream.read(); return factory.create(TokenType.LessEqualOrGreater, index, line, col, 3); default: break; } return factory.create(TokenType.LessOrGreater, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.LessEqual, index, line, col, 2); } return factory.create(TokenType.LessThan, index, line, col); case '>': switch (next) { case '>': in_stream.read(); int p = in_stream.peek(); if (p != -1) { switch ((char)p) { case '=': in_stream.read(); return factory.create(TokenType.ShiftRightAssign, index, line, col, 3); case '>': in_stream.read(); int q = in_stream.peek(); if (q != -1 && q == '=') { in_stream.read(); return factory.create(TokenType.TripleRightShiftAssign, index, line, col, 4); } return factory.create(TokenType.ShiftRightUnsigned, index, line, col, 3); } } return factory.create(TokenType.ShiftRight, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.GreaterEqual, index, line, col, 2); } return factory.create(TokenType.GreaterThan, index, line, col); case '?': return factory.create(TokenType.Question, index, line, col); case '$': return factory.create(TokenType.Dollar, index, line, col); case ';': return factory.create(TokenType.Semicolon, index, line, col); case ':': return factory.create(TokenType.Colon, index, line, col); case ',': return factory.create(TokenType.Comma, index, line, col); case '.': if (next == '.') { in_stream.read(); int p = in_stream.peek(); if (p != -1 && p == '.') { in_stream.read(); return factory.create(TokenType.TripleDot, index, line, col, 3); } return factory.create(TokenType.DoubleDot, index, line, col, 2); } return factory.create(TokenType.Dot, index, line, col); case ')': return factory.create(TokenType.CloseParenthesis, index, line, col); case '(': return factory.create(TokenType.OpenParenthesis, index, line, col); case ']': return factory.create(TokenType.CloseSquareBracket, index, line, col); case '[': return factory.create(TokenType.OpenSquareBracket, index, line, col); case '}': return factory.create(TokenType.CloseCurlyBrace, index, line, col); case '{': return factory.create(TokenType.OpenCurlyBrace, index, line, col); case '#': return factory.create(TokenType.Hash, index, line, col); default: return null; } } LexOperator(TokenFactory factory, LexerStream in_stream); Token read(); }
LexOperator { public Token read() throws IOException, LexerException { int index = in_stream.getPosition(); int line = in_stream.getLine(); int col = in_stream.getCol(); int next = in_stream.peek(); if (next == -1) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Unexpected end of input stream when parsing operator"); } if (next == '.') { int tmp = in_stream.peek(2); if (tmp > 0 && Character.isDigit(tmp)) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Literal float encountered when operator '.' expected"); } } char cur = (char)in_stream.read(); next = in_stream.peek(); switch (cur) { case '+': switch (next) { case '+': in_stream.read(); return factory.create(TokenType.Increment, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.PlusAssign, index, line, col, 2); } return factory.create(TokenType.Plus, index, line, col); case '-': switch (next) { case '-': in_stream.read(); return factory.create(TokenType.Decrement, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.MinusAssign, index, line, col, 2); } return factory.create(TokenType.Minus, index, line, col); case '*': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.TimesAssign, index, line, col, 2); default: break; } return factory.create(TokenType.Times, index, line, col); case '/': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.DivAssign, index, line, col, 2); } return factory.create(TokenType.Div, index, line, col); case '%': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.ModAssign, index, line, col, 2); } return factory.create(TokenType.Mod, index, line, col); case '&': switch (next) { case '&': in_stream.read(); return factory.create(TokenType.LogicalAnd, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.BitwiseAndAssign, index, line, col, 2); } return factory.create(TokenType.BitwiseAnd, index, line, col); case '|': switch (next) { case '|': in_stream.read(); return factory.create(TokenType.LogicalOr, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.BitwiseOrAssign, index, line, col, 2); } return factory.create(TokenType.BitwiseOr, index, line, col); case '^': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.XorAssign, index, line, col, 2); case '^': in_stream.read(); if (in_stream.peek() == '=') { in_stream.read(); return factory.create(TokenType.PowAssign, index, line, col, 3); } return factory.create(TokenType.Pow, index, line, col, 2); } return factory.create(TokenType.Xor, index, line, col); case '!': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.NotEqual, index, line, col, 2); case '<': in_stream.read(); switch (in_stream.peek()) { case '=': in_stream.read(); return factory.create(TokenType.UnorderedOrGreater, index, line, col, 3); case '>': in_stream.read(); switch (in_stream.peek()) { case '=': in_stream.read(); return factory.create(TokenType.Unordered, index, line, col, 4); } return factory.create(TokenType.UnorderedOrEqual, index, line, col, 3); } return factory.create(TokenType.UnorderedGreaterOrEqual, index, line, col, 2); case '>': in_stream.read(); switch (in_stream.peek()) { case '=': in_stream.read(); return factory.create(TokenType.UnorderedOrLess, index, line, col, 3); default: break; } return factory.create(TokenType.UnorderedLessOrEqual, index, line, col, 2); } return factory.create(TokenType.Not, index, line, col); case '~': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.TildeAssign, index, line, col, 2); } return factory.create(TokenType.Tilde, index, line, col); case '=': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.Equal, index, line, col, 2); case '>': in_stream.read(); return factory.create(TokenType.GoesTo, index, line, col, 2); } return factory.create(TokenType.Assign, index, line, col); case '<': switch (next) { case '<': in_stream.read(); switch (in_stream.peek()) { case '=': in_stream.read(); return factory.create(TokenType.ShiftLeftAssign, index, line, col, 3); default: break; } return factory.create(TokenType.ShiftLeft, index, line, col, 2); case '>': in_stream.read(); switch (in_stream.peek()) { case '=': in_stream.read(); return factory.create(TokenType.LessEqualOrGreater, index, line, col, 3); default: break; } return factory.create(TokenType.LessOrGreater, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.LessEqual, index, line, col, 2); } return factory.create(TokenType.LessThan, index, line, col); case '>': switch (next) { case '>': in_stream.read(); int p = in_stream.peek(); if (p != -1) { switch ((char)p) { case '=': in_stream.read(); return factory.create(TokenType.ShiftRightAssign, index, line, col, 3); case '>': in_stream.read(); int q = in_stream.peek(); if (q != -1 && q == '=') { in_stream.read(); return factory.create(TokenType.TripleRightShiftAssign, index, line, col, 4); } return factory.create(TokenType.ShiftRightUnsigned, index, line, col, 3); } } return factory.create(TokenType.ShiftRight, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.GreaterEqual, index, line, col, 2); } return factory.create(TokenType.GreaterThan, index, line, col); case '?': return factory.create(TokenType.Question, index, line, col); case '$': return factory.create(TokenType.Dollar, index, line, col); case ';': return factory.create(TokenType.Semicolon, index, line, col); case ':': return factory.create(TokenType.Colon, index, line, col); case ',': return factory.create(TokenType.Comma, index, line, col); case '.': if (next == '.') { in_stream.read(); int p = in_stream.peek(); if (p != -1 && p == '.') { in_stream.read(); return factory.create(TokenType.TripleDot, index, line, col, 3); } return factory.create(TokenType.DoubleDot, index, line, col, 2); } return factory.create(TokenType.Dot, index, line, col); case ')': return factory.create(TokenType.CloseParenthesis, index, line, col); case '(': return factory.create(TokenType.OpenParenthesis, index, line, col); case ']': return factory.create(TokenType.CloseSquareBracket, index, line, col); case '[': return factory.create(TokenType.OpenSquareBracket, index, line, col); case '}': return factory.create(TokenType.CloseCurlyBrace, index, line, col); case '{': return factory.create(TokenType.OpenCurlyBrace, index, line, col); case '#': return factory.create(TokenType.Hash, index, line, col); default: return null; } } LexOperator(TokenFactory factory, LexerStream in_stream); Token read(); }
@Test public void div() throws Exception { String str = "a/=b/c;"; LexerStream ls = new LexerStream(new StringReader(str)); LexOperator lex = new LexOperator(new BaseTokenFactory(), ls); ls.read(); Token tok = lex.read(); assertEquals(TokenType.DivAssign, tok.getType()); assertEquals(1, tok.getLine()); assertEquals(2, tok.getCol()); assertEquals(1, tok.getEndLine()); assertEquals(4, tok.getEndCol()); ls.read(); tok = lex.read(); assertEquals(TokenType.Div, tok.getType()); assertEquals(1, tok.getLine()); assertEquals(5, tok.getCol()); assertEquals(1, tok.getEndLine()); assertEquals(6, tok.getEndCol()); assertEquals('c', ls.read()); tok = lex.read(); assertEquals(TokenType.Semicolon, tok.getType()); assertEquals(-1, ls.read()); }
public Token read() throws IOException, LexerException { int index = in_stream.getPosition(); int line = in_stream.getLine(); int col = in_stream.getCol(); int next = in_stream.peek(); if (next == -1) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Unexpected end of input stream when parsing operator"); } if (next == '.') { int tmp = in_stream.peek(2); if (tmp > 0 && Character.isDigit(tmp)) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Literal float encountered when operator '.' expected"); } } char cur = (char)in_stream.read(); next = in_stream.peek(); switch (cur) { case '+': switch (next) { case '+': in_stream.read(); return factory.create(TokenType.Increment, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.PlusAssign, index, line, col, 2); } return factory.create(TokenType.Plus, index, line, col); case '-': switch (next) { case '-': in_stream.read(); return factory.create(TokenType.Decrement, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.MinusAssign, index, line, col, 2); } return factory.create(TokenType.Minus, index, line, col); case '*': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.TimesAssign, index, line, col, 2); default: break; } return factory.create(TokenType.Times, index, line, col); case '/': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.DivAssign, index, line, col, 2); } return factory.create(TokenType.Div, index, line, col); case '%': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.ModAssign, index, line, col, 2); } return factory.create(TokenType.Mod, index, line, col); case '&': switch (next) { case '&': in_stream.read(); return factory.create(TokenType.LogicalAnd, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.BitwiseAndAssign, index, line, col, 2); } return factory.create(TokenType.BitwiseAnd, index, line, col); case '|': switch (next) { case '|': in_stream.read(); return factory.create(TokenType.LogicalOr, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.BitwiseOrAssign, index, line, col, 2); } return factory.create(TokenType.BitwiseOr, index, line, col); case '^': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.XorAssign, index, line, col, 2); case '^': in_stream.read(); if (in_stream.peek() == '=') { in_stream.read(); return factory.create(TokenType.PowAssign, index, line, col, 3); } return factory.create(TokenType.Pow, index, line, col, 2); } return factory.create(TokenType.Xor, index, line, col); case '!': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.NotEqual, index, line, col, 2); case '<': in_stream.read(); switch (in_stream.peek()) { case '=': in_stream.read(); return factory.create(TokenType.UnorderedOrGreater, index, line, col, 3); case '>': in_stream.read(); switch (in_stream.peek()) { case '=': in_stream.read(); return factory.create(TokenType.Unordered, index, line, col, 4); } return factory.create(TokenType.UnorderedOrEqual, index, line, col, 3); } return factory.create(TokenType.UnorderedGreaterOrEqual, index, line, col, 2); case '>': in_stream.read(); switch (in_stream.peek()) { case '=': in_stream.read(); return factory.create(TokenType.UnorderedOrLess, index, line, col, 3); default: break; } return factory.create(TokenType.UnorderedLessOrEqual, index, line, col, 2); } return factory.create(TokenType.Not, index, line, col); case '~': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.TildeAssign, index, line, col, 2); } return factory.create(TokenType.Tilde, index, line, col); case '=': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.Equal, index, line, col, 2); case '>': in_stream.read(); return factory.create(TokenType.GoesTo, index, line, col, 2); } return factory.create(TokenType.Assign, index, line, col); case '<': switch (next) { case '<': in_stream.read(); switch (in_stream.peek()) { case '=': in_stream.read(); return factory.create(TokenType.ShiftLeftAssign, index, line, col, 3); default: break; } return factory.create(TokenType.ShiftLeft, index, line, col, 2); case '>': in_stream.read(); switch (in_stream.peek()) { case '=': in_stream.read(); return factory.create(TokenType.LessEqualOrGreater, index, line, col, 3); default: break; } return factory.create(TokenType.LessOrGreater, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.LessEqual, index, line, col, 2); } return factory.create(TokenType.LessThan, index, line, col); case '>': switch (next) { case '>': in_stream.read(); int p = in_stream.peek(); if (p != -1) { switch ((char)p) { case '=': in_stream.read(); return factory.create(TokenType.ShiftRightAssign, index, line, col, 3); case '>': in_stream.read(); int q = in_stream.peek(); if (q != -1 && q == '=') { in_stream.read(); return factory.create(TokenType.TripleRightShiftAssign, index, line, col, 4); } return factory.create(TokenType.ShiftRightUnsigned, index, line, col, 3); } } return factory.create(TokenType.ShiftRight, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.GreaterEqual, index, line, col, 2); } return factory.create(TokenType.GreaterThan, index, line, col); case '?': return factory.create(TokenType.Question, index, line, col); case '$': return factory.create(TokenType.Dollar, index, line, col); case ';': return factory.create(TokenType.Semicolon, index, line, col); case ':': return factory.create(TokenType.Colon, index, line, col); case ',': return factory.create(TokenType.Comma, index, line, col); case '.': if (next == '.') { in_stream.read(); int p = in_stream.peek(); if (p != -1 && p == '.') { in_stream.read(); return factory.create(TokenType.TripleDot, index, line, col, 3); } return factory.create(TokenType.DoubleDot, index, line, col, 2); } return factory.create(TokenType.Dot, index, line, col); case ')': return factory.create(TokenType.CloseParenthesis, index, line, col); case '(': return factory.create(TokenType.OpenParenthesis, index, line, col); case ']': return factory.create(TokenType.CloseSquareBracket, index, line, col); case '[': return factory.create(TokenType.OpenSquareBracket, index, line, col); case '}': return factory.create(TokenType.CloseCurlyBrace, index, line, col); case '{': return factory.create(TokenType.OpenCurlyBrace, index, line, col); case '#': return factory.create(TokenType.Hash, index, line, col); default: return null; } }
LexOperator { public Token read() throws IOException, LexerException { int index = in_stream.getPosition(); int line = in_stream.getLine(); int col = in_stream.getCol(); int next = in_stream.peek(); if (next == -1) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Unexpected end of input stream when parsing operator"); } if (next == '.') { int tmp = in_stream.peek(2); if (tmp > 0 && Character.isDigit(tmp)) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Literal float encountered when operator '.' expected"); } } char cur = (char)in_stream.read(); next = in_stream.peek(); switch (cur) { case '+': switch (next) { case '+': in_stream.read(); return factory.create(TokenType.Increment, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.PlusAssign, index, line, col, 2); } return factory.create(TokenType.Plus, index, line, col); case '-': switch (next) { case '-': in_stream.read(); return factory.create(TokenType.Decrement, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.MinusAssign, index, line, col, 2); } return factory.create(TokenType.Minus, index, line, col); case '*': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.TimesAssign, index, line, col, 2); default: break; } return factory.create(TokenType.Times, index, line, col); case '/': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.DivAssign, index, line, col, 2); } return factory.create(TokenType.Div, index, line, col); case '%': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.ModAssign, index, line, col, 2); } return factory.create(TokenType.Mod, index, line, col); case '&': switch (next) { case '&': in_stream.read(); return factory.create(TokenType.LogicalAnd, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.BitwiseAndAssign, index, line, col, 2); } return factory.create(TokenType.BitwiseAnd, index, line, col); case '|': switch (next) { case '|': in_stream.read(); return factory.create(TokenType.LogicalOr, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.BitwiseOrAssign, index, line, col, 2); } return factory.create(TokenType.BitwiseOr, index, line, col); case '^': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.XorAssign, index, line, col, 2); case '^': in_stream.read(); if (in_stream.peek() == '=') { in_stream.read(); return factory.create(TokenType.PowAssign, index, line, col, 3); } return factory.create(TokenType.Pow, index, line, col, 2); } return factory.create(TokenType.Xor, index, line, col); case '!': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.NotEqual, index, line, col, 2); case '<': in_stream.read(); switch (in_stream.peek()) { case '=': in_stream.read(); return factory.create(TokenType.UnorderedOrGreater, index, line, col, 3); case '>': in_stream.read(); switch (in_stream.peek()) { case '=': in_stream.read(); return factory.create(TokenType.Unordered, index, line, col, 4); } return factory.create(TokenType.UnorderedOrEqual, index, line, col, 3); } return factory.create(TokenType.UnorderedGreaterOrEqual, index, line, col, 2); case '>': in_stream.read(); switch (in_stream.peek()) { case '=': in_stream.read(); return factory.create(TokenType.UnorderedOrLess, index, line, col, 3); default: break; } return factory.create(TokenType.UnorderedLessOrEqual, index, line, col, 2); } return factory.create(TokenType.Not, index, line, col); case '~': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.TildeAssign, index, line, col, 2); } return factory.create(TokenType.Tilde, index, line, col); case '=': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.Equal, index, line, col, 2); case '>': in_stream.read(); return factory.create(TokenType.GoesTo, index, line, col, 2); } return factory.create(TokenType.Assign, index, line, col); case '<': switch (next) { case '<': in_stream.read(); switch (in_stream.peek()) { case '=': in_stream.read(); return factory.create(TokenType.ShiftLeftAssign, index, line, col, 3); default: break; } return factory.create(TokenType.ShiftLeft, index, line, col, 2); case '>': in_stream.read(); switch (in_stream.peek()) { case '=': in_stream.read(); return factory.create(TokenType.LessEqualOrGreater, index, line, col, 3); default: break; } return factory.create(TokenType.LessOrGreater, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.LessEqual, index, line, col, 2); } return factory.create(TokenType.LessThan, index, line, col); case '>': switch (next) { case '>': in_stream.read(); int p = in_stream.peek(); if (p != -1) { switch ((char)p) { case '=': in_stream.read(); return factory.create(TokenType.ShiftRightAssign, index, line, col, 3); case '>': in_stream.read(); int q = in_stream.peek(); if (q != -1 && q == '=') { in_stream.read(); return factory.create(TokenType.TripleRightShiftAssign, index, line, col, 4); } return factory.create(TokenType.ShiftRightUnsigned, index, line, col, 3); } } return factory.create(TokenType.ShiftRight, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.GreaterEqual, index, line, col, 2); } return factory.create(TokenType.GreaterThan, index, line, col); case '?': return factory.create(TokenType.Question, index, line, col); case '$': return factory.create(TokenType.Dollar, index, line, col); case ';': return factory.create(TokenType.Semicolon, index, line, col); case ':': return factory.create(TokenType.Colon, index, line, col); case ',': return factory.create(TokenType.Comma, index, line, col); case '.': if (next == '.') { in_stream.read(); int p = in_stream.peek(); if (p != -1 && p == '.') { in_stream.read(); return factory.create(TokenType.TripleDot, index, line, col, 3); } return factory.create(TokenType.DoubleDot, index, line, col, 2); } return factory.create(TokenType.Dot, index, line, col); case ')': return factory.create(TokenType.CloseParenthesis, index, line, col); case '(': return factory.create(TokenType.OpenParenthesis, index, line, col); case ']': return factory.create(TokenType.CloseSquareBracket, index, line, col); case '[': return factory.create(TokenType.OpenSquareBracket, index, line, col); case '}': return factory.create(TokenType.CloseCurlyBrace, index, line, col); case '{': return factory.create(TokenType.OpenCurlyBrace, index, line, col); case '#': return factory.create(TokenType.Hash, index, line, col); default: return null; } } }
LexOperator { public Token read() throws IOException, LexerException { int index = in_stream.getPosition(); int line = in_stream.getLine(); int col = in_stream.getCol(); int next = in_stream.peek(); if (next == -1) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Unexpected end of input stream when parsing operator"); } if (next == '.') { int tmp = in_stream.peek(2); if (tmp > 0 && Character.isDigit(tmp)) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Literal float encountered when operator '.' expected"); } } char cur = (char)in_stream.read(); next = in_stream.peek(); switch (cur) { case '+': switch (next) { case '+': in_stream.read(); return factory.create(TokenType.Increment, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.PlusAssign, index, line, col, 2); } return factory.create(TokenType.Plus, index, line, col); case '-': switch (next) { case '-': in_stream.read(); return factory.create(TokenType.Decrement, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.MinusAssign, index, line, col, 2); } return factory.create(TokenType.Minus, index, line, col); case '*': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.TimesAssign, index, line, col, 2); default: break; } return factory.create(TokenType.Times, index, line, col); case '/': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.DivAssign, index, line, col, 2); } return factory.create(TokenType.Div, index, line, col); case '%': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.ModAssign, index, line, col, 2); } return factory.create(TokenType.Mod, index, line, col); case '&': switch (next) { case '&': in_stream.read(); return factory.create(TokenType.LogicalAnd, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.BitwiseAndAssign, index, line, col, 2); } return factory.create(TokenType.BitwiseAnd, index, line, col); case '|': switch (next) { case '|': in_stream.read(); return factory.create(TokenType.LogicalOr, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.BitwiseOrAssign, index, line, col, 2); } return factory.create(TokenType.BitwiseOr, index, line, col); case '^': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.XorAssign, index, line, col, 2); case '^': in_stream.read(); if (in_stream.peek() == '=') { in_stream.read(); return factory.create(TokenType.PowAssign, index, line, col, 3); } return factory.create(TokenType.Pow, index, line, col, 2); } return factory.create(TokenType.Xor, index, line, col); case '!': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.NotEqual, index, line, col, 2); case '<': in_stream.read(); switch (in_stream.peek()) { case '=': in_stream.read(); return factory.create(TokenType.UnorderedOrGreater, index, line, col, 3); case '>': in_stream.read(); switch (in_stream.peek()) { case '=': in_stream.read(); return factory.create(TokenType.Unordered, index, line, col, 4); } return factory.create(TokenType.UnorderedOrEqual, index, line, col, 3); } return factory.create(TokenType.UnorderedGreaterOrEqual, index, line, col, 2); case '>': in_stream.read(); switch (in_stream.peek()) { case '=': in_stream.read(); return factory.create(TokenType.UnorderedOrLess, index, line, col, 3); default: break; } return factory.create(TokenType.UnorderedLessOrEqual, index, line, col, 2); } return factory.create(TokenType.Not, index, line, col); case '~': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.TildeAssign, index, line, col, 2); } return factory.create(TokenType.Tilde, index, line, col); case '=': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.Equal, index, line, col, 2); case '>': in_stream.read(); return factory.create(TokenType.GoesTo, index, line, col, 2); } return factory.create(TokenType.Assign, index, line, col); case '<': switch (next) { case '<': in_stream.read(); switch (in_stream.peek()) { case '=': in_stream.read(); return factory.create(TokenType.ShiftLeftAssign, index, line, col, 3); default: break; } return factory.create(TokenType.ShiftLeft, index, line, col, 2); case '>': in_stream.read(); switch (in_stream.peek()) { case '=': in_stream.read(); return factory.create(TokenType.LessEqualOrGreater, index, line, col, 3); default: break; } return factory.create(TokenType.LessOrGreater, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.LessEqual, index, line, col, 2); } return factory.create(TokenType.LessThan, index, line, col); case '>': switch (next) { case '>': in_stream.read(); int p = in_stream.peek(); if (p != -1) { switch ((char)p) { case '=': in_stream.read(); return factory.create(TokenType.ShiftRightAssign, index, line, col, 3); case '>': in_stream.read(); int q = in_stream.peek(); if (q != -1 && q == '=') { in_stream.read(); return factory.create(TokenType.TripleRightShiftAssign, index, line, col, 4); } return factory.create(TokenType.ShiftRightUnsigned, index, line, col, 3); } } return factory.create(TokenType.ShiftRight, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.GreaterEqual, index, line, col, 2); } return factory.create(TokenType.GreaterThan, index, line, col); case '?': return factory.create(TokenType.Question, index, line, col); case '$': return factory.create(TokenType.Dollar, index, line, col); case ';': return factory.create(TokenType.Semicolon, index, line, col); case ':': return factory.create(TokenType.Colon, index, line, col); case ',': return factory.create(TokenType.Comma, index, line, col); case '.': if (next == '.') { in_stream.read(); int p = in_stream.peek(); if (p != -1 && p == '.') { in_stream.read(); return factory.create(TokenType.TripleDot, index, line, col, 3); } return factory.create(TokenType.DoubleDot, index, line, col, 2); } return factory.create(TokenType.Dot, index, line, col); case ')': return factory.create(TokenType.CloseParenthesis, index, line, col); case '(': return factory.create(TokenType.OpenParenthesis, index, line, col); case ']': return factory.create(TokenType.CloseSquareBracket, index, line, col); case '[': return factory.create(TokenType.OpenSquareBracket, index, line, col); case '}': return factory.create(TokenType.CloseCurlyBrace, index, line, col); case '{': return factory.create(TokenType.OpenCurlyBrace, index, line, col); case '#': return factory.create(TokenType.Hash, index, line, col); default: return null; } } LexOperator(TokenFactory factory, LexerStream in_stream); }
LexOperator { public Token read() throws IOException, LexerException { int index = in_stream.getPosition(); int line = in_stream.getLine(); int col = in_stream.getCol(); int next = in_stream.peek(); if (next == -1) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Unexpected end of input stream when parsing operator"); } if (next == '.') { int tmp = in_stream.peek(2); if (tmp > 0 && Character.isDigit(tmp)) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Literal float encountered when operator '.' expected"); } } char cur = (char)in_stream.read(); next = in_stream.peek(); switch (cur) { case '+': switch (next) { case '+': in_stream.read(); return factory.create(TokenType.Increment, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.PlusAssign, index, line, col, 2); } return factory.create(TokenType.Plus, index, line, col); case '-': switch (next) { case '-': in_stream.read(); return factory.create(TokenType.Decrement, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.MinusAssign, index, line, col, 2); } return factory.create(TokenType.Minus, index, line, col); case '*': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.TimesAssign, index, line, col, 2); default: break; } return factory.create(TokenType.Times, index, line, col); case '/': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.DivAssign, index, line, col, 2); } return factory.create(TokenType.Div, index, line, col); case '%': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.ModAssign, index, line, col, 2); } return factory.create(TokenType.Mod, index, line, col); case '&': switch (next) { case '&': in_stream.read(); return factory.create(TokenType.LogicalAnd, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.BitwiseAndAssign, index, line, col, 2); } return factory.create(TokenType.BitwiseAnd, index, line, col); case '|': switch (next) { case '|': in_stream.read(); return factory.create(TokenType.LogicalOr, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.BitwiseOrAssign, index, line, col, 2); } return factory.create(TokenType.BitwiseOr, index, line, col); case '^': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.XorAssign, index, line, col, 2); case '^': in_stream.read(); if (in_stream.peek() == '=') { in_stream.read(); return factory.create(TokenType.PowAssign, index, line, col, 3); } return factory.create(TokenType.Pow, index, line, col, 2); } return factory.create(TokenType.Xor, index, line, col); case '!': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.NotEqual, index, line, col, 2); case '<': in_stream.read(); switch (in_stream.peek()) { case '=': in_stream.read(); return factory.create(TokenType.UnorderedOrGreater, index, line, col, 3); case '>': in_stream.read(); switch (in_stream.peek()) { case '=': in_stream.read(); return factory.create(TokenType.Unordered, index, line, col, 4); } return factory.create(TokenType.UnorderedOrEqual, index, line, col, 3); } return factory.create(TokenType.UnorderedGreaterOrEqual, index, line, col, 2); case '>': in_stream.read(); switch (in_stream.peek()) { case '=': in_stream.read(); return factory.create(TokenType.UnorderedOrLess, index, line, col, 3); default: break; } return factory.create(TokenType.UnorderedLessOrEqual, index, line, col, 2); } return factory.create(TokenType.Not, index, line, col); case '~': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.TildeAssign, index, line, col, 2); } return factory.create(TokenType.Tilde, index, line, col); case '=': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.Equal, index, line, col, 2); case '>': in_stream.read(); return factory.create(TokenType.GoesTo, index, line, col, 2); } return factory.create(TokenType.Assign, index, line, col); case '<': switch (next) { case '<': in_stream.read(); switch (in_stream.peek()) { case '=': in_stream.read(); return factory.create(TokenType.ShiftLeftAssign, index, line, col, 3); default: break; } return factory.create(TokenType.ShiftLeft, index, line, col, 2); case '>': in_stream.read(); switch (in_stream.peek()) { case '=': in_stream.read(); return factory.create(TokenType.LessEqualOrGreater, index, line, col, 3); default: break; } return factory.create(TokenType.LessOrGreater, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.LessEqual, index, line, col, 2); } return factory.create(TokenType.LessThan, index, line, col); case '>': switch (next) { case '>': in_stream.read(); int p = in_stream.peek(); if (p != -1) { switch ((char)p) { case '=': in_stream.read(); return factory.create(TokenType.ShiftRightAssign, index, line, col, 3); case '>': in_stream.read(); int q = in_stream.peek(); if (q != -1 && q == '=') { in_stream.read(); return factory.create(TokenType.TripleRightShiftAssign, index, line, col, 4); } return factory.create(TokenType.ShiftRightUnsigned, index, line, col, 3); } } return factory.create(TokenType.ShiftRight, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.GreaterEqual, index, line, col, 2); } return factory.create(TokenType.GreaterThan, index, line, col); case '?': return factory.create(TokenType.Question, index, line, col); case '$': return factory.create(TokenType.Dollar, index, line, col); case ';': return factory.create(TokenType.Semicolon, index, line, col); case ':': return factory.create(TokenType.Colon, index, line, col); case ',': return factory.create(TokenType.Comma, index, line, col); case '.': if (next == '.') { in_stream.read(); int p = in_stream.peek(); if (p != -1 && p == '.') { in_stream.read(); return factory.create(TokenType.TripleDot, index, line, col, 3); } return factory.create(TokenType.DoubleDot, index, line, col, 2); } return factory.create(TokenType.Dot, index, line, col); case ')': return factory.create(TokenType.CloseParenthesis, index, line, col); case '(': return factory.create(TokenType.OpenParenthesis, index, line, col); case ']': return factory.create(TokenType.CloseSquareBracket, index, line, col); case '[': return factory.create(TokenType.OpenSquareBracket, index, line, col); case '}': return factory.create(TokenType.CloseCurlyBrace, index, line, col); case '{': return factory.create(TokenType.OpenCurlyBrace, index, line, col); case '#': return factory.create(TokenType.Hash, index, line, col); default: return null; } } LexOperator(TokenFactory factory, LexerStream in_stream); Token read(); }
LexOperator { public Token read() throws IOException, LexerException { int index = in_stream.getPosition(); int line = in_stream.getLine(); int col = in_stream.getCol(); int next = in_stream.peek(); if (next == -1) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Unexpected end of input stream when parsing operator"); } if (next == '.') { int tmp = in_stream.peek(2); if (tmp > 0 && Character.isDigit(tmp)) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Literal float encountered when operator '.' expected"); } } char cur = (char)in_stream.read(); next = in_stream.peek(); switch (cur) { case '+': switch (next) { case '+': in_stream.read(); return factory.create(TokenType.Increment, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.PlusAssign, index, line, col, 2); } return factory.create(TokenType.Plus, index, line, col); case '-': switch (next) { case '-': in_stream.read(); return factory.create(TokenType.Decrement, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.MinusAssign, index, line, col, 2); } return factory.create(TokenType.Minus, index, line, col); case '*': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.TimesAssign, index, line, col, 2); default: break; } return factory.create(TokenType.Times, index, line, col); case '/': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.DivAssign, index, line, col, 2); } return factory.create(TokenType.Div, index, line, col); case '%': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.ModAssign, index, line, col, 2); } return factory.create(TokenType.Mod, index, line, col); case '&': switch (next) { case '&': in_stream.read(); return factory.create(TokenType.LogicalAnd, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.BitwiseAndAssign, index, line, col, 2); } return factory.create(TokenType.BitwiseAnd, index, line, col); case '|': switch (next) { case '|': in_stream.read(); return factory.create(TokenType.LogicalOr, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.BitwiseOrAssign, index, line, col, 2); } return factory.create(TokenType.BitwiseOr, index, line, col); case '^': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.XorAssign, index, line, col, 2); case '^': in_stream.read(); if (in_stream.peek() == '=') { in_stream.read(); return factory.create(TokenType.PowAssign, index, line, col, 3); } return factory.create(TokenType.Pow, index, line, col, 2); } return factory.create(TokenType.Xor, index, line, col); case '!': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.NotEqual, index, line, col, 2); case '<': in_stream.read(); switch (in_stream.peek()) { case '=': in_stream.read(); return factory.create(TokenType.UnorderedOrGreater, index, line, col, 3); case '>': in_stream.read(); switch (in_stream.peek()) { case '=': in_stream.read(); return factory.create(TokenType.Unordered, index, line, col, 4); } return factory.create(TokenType.UnorderedOrEqual, index, line, col, 3); } return factory.create(TokenType.UnorderedGreaterOrEqual, index, line, col, 2); case '>': in_stream.read(); switch (in_stream.peek()) { case '=': in_stream.read(); return factory.create(TokenType.UnorderedOrLess, index, line, col, 3); default: break; } return factory.create(TokenType.UnorderedLessOrEqual, index, line, col, 2); } return factory.create(TokenType.Not, index, line, col); case '~': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.TildeAssign, index, line, col, 2); } return factory.create(TokenType.Tilde, index, line, col); case '=': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.Equal, index, line, col, 2); case '>': in_stream.read(); return factory.create(TokenType.GoesTo, index, line, col, 2); } return factory.create(TokenType.Assign, index, line, col); case '<': switch (next) { case '<': in_stream.read(); switch (in_stream.peek()) { case '=': in_stream.read(); return factory.create(TokenType.ShiftLeftAssign, index, line, col, 3); default: break; } return factory.create(TokenType.ShiftLeft, index, line, col, 2); case '>': in_stream.read(); switch (in_stream.peek()) { case '=': in_stream.read(); return factory.create(TokenType.LessEqualOrGreater, index, line, col, 3); default: break; } return factory.create(TokenType.LessOrGreater, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.LessEqual, index, line, col, 2); } return factory.create(TokenType.LessThan, index, line, col); case '>': switch (next) { case '>': in_stream.read(); int p = in_stream.peek(); if (p != -1) { switch ((char)p) { case '=': in_stream.read(); return factory.create(TokenType.ShiftRightAssign, index, line, col, 3); case '>': in_stream.read(); int q = in_stream.peek(); if (q != -1 && q == '=') { in_stream.read(); return factory.create(TokenType.TripleRightShiftAssign, index, line, col, 4); } return factory.create(TokenType.ShiftRightUnsigned, index, line, col, 3); } } return factory.create(TokenType.ShiftRight, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.GreaterEqual, index, line, col, 2); } return factory.create(TokenType.GreaterThan, index, line, col); case '?': return factory.create(TokenType.Question, index, line, col); case '$': return factory.create(TokenType.Dollar, index, line, col); case ';': return factory.create(TokenType.Semicolon, index, line, col); case ':': return factory.create(TokenType.Colon, index, line, col); case ',': return factory.create(TokenType.Comma, index, line, col); case '.': if (next == '.') { in_stream.read(); int p = in_stream.peek(); if (p != -1 && p == '.') { in_stream.read(); return factory.create(TokenType.TripleDot, index, line, col, 3); } return factory.create(TokenType.DoubleDot, index, line, col, 2); } return factory.create(TokenType.Dot, index, line, col); case ')': return factory.create(TokenType.CloseParenthesis, index, line, col); case '(': return factory.create(TokenType.OpenParenthesis, index, line, col); case ']': return factory.create(TokenType.CloseSquareBracket, index, line, col); case '[': return factory.create(TokenType.OpenSquareBracket, index, line, col); case '}': return factory.create(TokenType.CloseCurlyBrace, index, line, col); case '{': return factory.create(TokenType.OpenCurlyBrace, index, line, col); case '#': return factory.create(TokenType.Hash, index, line, col); default: return null; } } LexOperator(TokenFactory factory, LexerStream in_stream); Token read(); }
@Test public void mod() throws Exception { String str = "a%=b%c;"; LexerStream ls = new LexerStream(new StringReader(str)); LexOperator lex = new LexOperator(new BaseTokenFactory(), ls); ls.read(); Token tok = lex.read(); assertEquals(TokenType.ModAssign, tok.getType()); assertEquals(1, tok.getLine()); assertEquals(2, tok.getCol()); assertEquals(1, tok.getEndLine()); assertEquals(4, tok.getEndCol()); ls.read(); tok = lex.read(); assertEquals(TokenType.Mod, tok.getType()); assertEquals(1, tok.getLine()); assertEquals(5, tok.getCol()); assertEquals(1, tok.getEndLine()); assertEquals(6, tok.getEndCol()); assertEquals('c', ls.read()); tok = lex.read(); assertEquals(TokenType.Semicolon, tok.getType()); assertEquals(-1, ls.read()); }
public Token read() throws IOException, LexerException { int index = in_stream.getPosition(); int line = in_stream.getLine(); int col = in_stream.getCol(); int next = in_stream.peek(); if (next == -1) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Unexpected end of input stream when parsing operator"); } if (next == '.') { int tmp = in_stream.peek(2); if (tmp > 0 && Character.isDigit(tmp)) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Literal float encountered when operator '.' expected"); } } char cur = (char)in_stream.read(); next = in_stream.peek(); switch (cur) { case '+': switch (next) { case '+': in_stream.read(); return factory.create(TokenType.Increment, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.PlusAssign, index, line, col, 2); } return factory.create(TokenType.Plus, index, line, col); case '-': switch (next) { case '-': in_stream.read(); return factory.create(TokenType.Decrement, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.MinusAssign, index, line, col, 2); } return factory.create(TokenType.Minus, index, line, col); case '*': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.TimesAssign, index, line, col, 2); default: break; } return factory.create(TokenType.Times, index, line, col); case '/': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.DivAssign, index, line, col, 2); } return factory.create(TokenType.Div, index, line, col); case '%': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.ModAssign, index, line, col, 2); } return factory.create(TokenType.Mod, index, line, col); case '&': switch (next) { case '&': in_stream.read(); return factory.create(TokenType.LogicalAnd, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.BitwiseAndAssign, index, line, col, 2); } return factory.create(TokenType.BitwiseAnd, index, line, col); case '|': switch (next) { case '|': in_stream.read(); return factory.create(TokenType.LogicalOr, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.BitwiseOrAssign, index, line, col, 2); } return factory.create(TokenType.BitwiseOr, index, line, col); case '^': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.XorAssign, index, line, col, 2); case '^': in_stream.read(); if (in_stream.peek() == '=') { in_stream.read(); return factory.create(TokenType.PowAssign, index, line, col, 3); } return factory.create(TokenType.Pow, index, line, col, 2); } return factory.create(TokenType.Xor, index, line, col); case '!': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.NotEqual, index, line, col, 2); case '<': in_stream.read(); switch (in_stream.peek()) { case '=': in_stream.read(); return factory.create(TokenType.UnorderedOrGreater, index, line, col, 3); case '>': in_stream.read(); switch (in_stream.peek()) { case '=': in_stream.read(); return factory.create(TokenType.Unordered, index, line, col, 4); } return factory.create(TokenType.UnorderedOrEqual, index, line, col, 3); } return factory.create(TokenType.UnorderedGreaterOrEqual, index, line, col, 2); case '>': in_stream.read(); switch (in_stream.peek()) { case '=': in_stream.read(); return factory.create(TokenType.UnorderedOrLess, index, line, col, 3); default: break; } return factory.create(TokenType.UnorderedLessOrEqual, index, line, col, 2); } return factory.create(TokenType.Not, index, line, col); case '~': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.TildeAssign, index, line, col, 2); } return factory.create(TokenType.Tilde, index, line, col); case '=': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.Equal, index, line, col, 2); case '>': in_stream.read(); return factory.create(TokenType.GoesTo, index, line, col, 2); } return factory.create(TokenType.Assign, index, line, col); case '<': switch (next) { case '<': in_stream.read(); switch (in_stream.peek()) { case '=': in_stream.read(); return factory.create(TokenType.ShiftLeftAssign, index, line, col, 3); default: break; } return factory.create(TokenType.ShiftLeft, index, line, col, 2); case '>': in_stream.read(); switch (in_stream.peek()) { case '=': in_stream.read(); return factory.create(TokenType.LessEqualOrGreater, index, line, col, 3); default: break; } return factory.create(TokenType.LessOrGreater, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.LessEqual, index, line, col, 2); } return factory.create(TokenType.LessThan, index, line, col); case '>': switch (next) { case '>': in_stream.read(); int p = in_stream.peek(); if (p != -1) { switch ((char)p) { case '=': in_stream.read(); return factory.create(TokenType.ShiftRightAssign, index, line, col, 3); case '>': in_stream.read(); int q = in_stream.peek(); if (q != -1 && q == '=') { in_stream.read(); return factory.create(TokenType.TripleRightShiftAssign, index, line, col, 4); } return factory.create(TokenType.ShiftRightUnsigned, index, line, col, 3); } } return factory.create(TokenType.ShiftRight, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.GreaterEqual, index, line, col, 2); } return factory.create(TokenType.GreaterThan, index, line, col); case '?': return factory.create(TokenType.Question, index, line, col); case '$': return factory.create(TokenType.Dollar, index, line, col); case ';': return factory.create(TokenType.Semicolon, index, line, col); case ':': return factory.create(TokenType.Colon, index, line, col); case ',': return factory.create(TokenType.Comma, index, line, col); case '.': if (next == '.') { in_stream.read(); int p = in_stream.peek(); if (p != -1 && p == '.') { in_stream.read(); return factory.create(TokenType.TripleDot, index, line, col, 3); } return factory.create(TokenType.DoubleDot, index, line, col, 2); } return factory.create(TokenType.Dot, index, line, col); case ')': return factory.create(TokenType.CloseParenthesis, index, line, col); case '(': return factory.create(TokenType.OpenParenthesis, index, line, col); case ']': return factory.create(TokenType.CloseSquareBracket, index, line, col); case '[': return factory.create(TokenType.OpenSquareBracket, index, line, col); case '}': return factory.create(TokenType.CloseCurlyBrace, index, line, col); case '{': return factory.create(TokenType.OpenCurlyBrace, index, line, col); case '#': return factory.create(TokenType.Hash, index, line, col); default: return null; } }
LexOperator { public Token read() throws IOException, LexerException { int index = in_stream.getPosition(); int line = in_stream.getLine(); int col = in_stream.getCol(); int next = in_stream.peek(); if (next == -1) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Unexpected end of input stream when parsing operator"); } if (next == '.') { int tmp = in_stream.peek(2); if (tmp > 0 && Character.isDigit(tmp)) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Literal float encountered when operator '.' expected"); } } char cur = (char)in_stream.read(); next = in_stream.peek(); switch (cur) { case '+': switch (next) { case '+': in_stream.read(); return factory.create(TokenType.Increment, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.PlusAssign, index, line, col, 2); } return factory.create(TokenType.Plus, index, line, col); case '-': switch (next) { case '-': in_stream.read(); return factory.create(TokenType.Decrement, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.MinusAssign, index, line, col, 2); } return factory.create(TokenType.Minus, index, line, col); case '*': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.TimesAssign, index, line, col, 2); default: break; } return factory.create(TokenType.Times, index, line, col); case '/': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.DivAssign, index, line, col, 2); } return factory.create(TokenType.Div, index, line, col); case '%': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.ModAssign, index, line, col, 2); } return factory.create(TokenType.Mod, index, line, col); case '&': switch (next) { case '&': in_stream.read(); return factory.create(TokenType.LogicalAnd, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.BitwiseAndAssign, index, line, col, 2); } return factory.create(TokenType.BitwiseAnd, index, line, col); case '|': switch (next) { case '|': in_stream.read(); return factory.create(TokenType.LogicalOr, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.BitwiseOrAssign, index, line, col, 2); } return factory.create(TokenType.BitwiseOr, index, line, col); case '^': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.XorAssign, index, line, col, 2); case '^': in_stream.read(); if (in_stream.peek() == '=') { in_stream.read(); return factory.create(TokenType.PowAssign, index, line, col, 3); } return factory.create(TokenType.Pow, index, line, col, 2); } return factory.create(TokenType.Xor, index, line, col); case '!': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.NotEqual, index, line, col, 2); case '<': in_stream.read(); switch (in_stream.peek()) { case '=': in_stream.read(); return factory.create(TokenType.UnorderedOrGreater, index, line, col, 3); case '>': in_stream.read(); switch (in_stream.peek()) { case '=': in_stream.read(); return factory.create(TokenType.Unordered, index, line, col, 4); } return factory.create(TokenType.UnorderedOrEqual, index, line, col, 3); } return factory.create(TokenType.UnorderedGreaterOrEqual, index, line, col, 2); case '>': in_stream.read(); switch (in_stream.peek()) { case '=': in_stream.read(); return factory.create(TokenType.UnorderedOrLess, index, line, col, 3); default: break; } return factory.create(TokenType.UnorderedLessOrEqual, index, line, col, 2); } return factory.create(TokenType.Not, index, line, col); case '~': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.TildeAssign, index, line, col, 2); } return factory.create(TokenType.Tilde, index, line, col); case '=': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.Equal, index, line, col, 2); case '>': in_stream.read(); return factory.create(TokenType.GoesTo, index, line, col, 2); } return factory.create(TokenType.Assign, index, line, col); case '<': switch (next) { case '<': in_stream.read(); switch (in_stream.peek()) { case '=': in_stream.read(); return factory.create(TokenType.ShiftLeftAssign, index, line, col, 3); default: break; } return factory.create(TokenType.ShiftLeft, index, line, col, 2); case '>': in_stream.read(); switch (in_stream.peek()) { case '=': in_stream.read(); return factory.create(TokenType.LessEqualOrGreater, index, line, col, 3); default: break; } return factory.create(TokenType.LessOrGreater, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.LessEqual, index, line, col, 2); } return factory.create(TokenType.LessThan, index, line, col); case '>': switch (next) { case '>': in_stream.read(); int p = in_stream.peek(); if (p != -1) { switch ((char)p) { case '=': in_stream.read(); return factory.create(TokenType.ShiftRightAssign, index, line, col, 3); case '>': in_stream.read(); int q = in_stream.peek(); if (q != -1 && q == '=') { in_stream.read(); return factory.create(TokenType.TripleRightShiftAssign, index, line, col, 4); } return factory.create(TokenType.ShiftRightUnsigned, index, line, col, 3); } } return factory.create(TokenType.ShiftRight, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.GreaterEqual, index, line, col, 2); } return factory.create(TokenType.GreaterThan, index, line, col); case '?': return factory.create(TokenType.Question, index, line, col); case '$': return factory.create(TokenType.Dollar, index, line, col); case ';': return factory.create(TokenType.Semicolon, index, line, col); case ':': return factory.create(TokenType.Colon, index, line, col); case ',': return factory.create(TokenType.Comma, index, line, col); case '.': if (next == '.') { in_stream.read(); int p = in_stream.peek(); if (p != -1 && p == '.') { in_stream.read(); return factory.create(TokenType.TripleDot, index, line, col, 3); } return factory.create(TokenType.DoubleDot, index, line, col, 2); } return factory.create(TokenType.Dot, index, line, col); case ')': return factory.create(TokenType.CloseParenthesis, index, line, col); case '(': return factory.create(TokenType.OpenParenthesis, index, line, col); case ']': return factory.create(TokenType.CloseSquareBracket, index, line, col); case '[': return factory.create(TokenType.OpenSquareBracket, index, line, col); case '}': return factory.create(TokenType.CloseCurlyBrace, index, line, col); case '{': return factory.create(TokenType.OpenCurlyBrace, index, line, col); case '#': return factory.create(TokenType.Hash, index, line, col); default: return null; } } }
LexOperator { public Token read() throws IOException, LexerException { int index = in_stream.getPosition(); int line = in_stream.getLine(); int col = in_stream.getCol(); int next = in_stream.peek(); if (next == -1) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Unexpected end of input stream when parsing operator"); } if (next == '.') { int tmp = in_stream.peek(2); if (tmp > 0 && Character.isDigit(tmp)) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Literal float encountered when operator '.' expected"); } } char cur = (char)in_stream.read(); next = in_stream.peek(); switch (cur) { case '+': switch (next) { case '+': in_stream.read(); return factory.create(TokenType.Increment, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.PlusAssign, index, line, col, 2); } return factory.create(TokenType.Plus, index, line, col); case '-': switch (next) { case '-': in_stream.read(); return factory.create(TokenType.Decrement, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.MinusAssign, index, line, col, 2); } return factory.create(TokenType.Minus, index, line, col); case '*': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.TimesAssign, index, line, col, 2); default: break; } return factory.create(TokenType.Times, index, line, col); case '/': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.DivAssign, index, line, col, 2); } return factory.create(TokenType.Div, index, line, col); case '%': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.ModAssign, index, line, col, 2); } return factory.create(TokenType.Mod, index, line, col); case '&': switch (next) { case '&': in_stream.read(); return factory.create(TokenType.LogicalAnd, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.BitwiseAndAssign, index, line, col, 2); } return factory.create(TokenType.BitwiseAnd, index, line, col); case '|': switch (next) { case '|': in_stream.read(); return factory.create(TokenType.LogicalOr, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.BitwiseOrAssign, index, line, col, 2); } return factory.create(TokenType.BitwiseOr, index, line, col); case '^': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.XorAssign, index, line, col, 2); case '^': in_stream.read(); if (in_stream.peek() == '=') { in_stream.read(); return factory.create(TokenType.PowAssign, index, line, col, 3); } return factory.create(TokenType.Pow, index, line, col, 2); } return factory.create(TokenType.Xor, index, line, col); case '!': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.NotEqual, index, line, col, 2); case '<': in_stream.read(); switch (in_stream.peek()) { case '=': in_stream.read(); return factory.create(TokenType.UnorderedOrGreater, index, line, col, 3); case '>': in_stream.read(); switch (in_stream.peek()) { case '=': in_stream.read(); return factory.create(TokenType.Unordered, index, line, col, 4); } return factory.create(TokenType.UnorderedOrEqual, index, line, col, 3); } return factory.create(TokenType.UnorderedGreaterOrEqual, index, line, col, 2); case '>': in_stream.read(); switch (in_stream.peek()) { case '=': in_stream.read(); return factory.create(TokenType.UnorderedOrLess, index, line, col, 3); default: break; } return factory.create(TokenType.UnorderedLessOrEqual, index, line, col, 2); } return factory.create(TokenType.Not, index, line, col); case '~': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.TildeAssign, index, line, col, 2); } return factory.create(TokenType.Tilde, index, line, col); case '=': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.Equal, index, line, col, 2); case '>': in_stream.read(); return factory.create(TokenType.GoesTo, index, line, col, 2); } return factory.create(TokenType.Assign, index, line, col); case '<': switch (next) { case '<': in_stream.read(); switch (in_stream.peek()) { case '=': in_stream.read(); return factory.create(TokenType.ShiftLeftAssign, index, line, col, 3); default: break; } return factory.create(TokenType.ShiftLeft, index, line, col, 2); case '>': in_stream.read(); switch (in_stream.peek()) { case '=': in_stream.read(); return factory.create(TokenType.LessEqualOrGreater, index, line, col, 3); default: break; } return factory.create(TokenType.LessOrGreater, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.LessEqual, index, line, col, 2); } return factory.create(TokenType.LessThan, index, line, col); case '>': switch (next) { case '>': in_stream.read(); int p = in_stream.peek(); if (p != -1) { switch ((char)p) { case '=': in_stream.read(); return factory.create(TokenType.ShiftRightAssign, index, line, col, 3); case '>': in_stream.read(); int q = in_stream.peek(); if (q != -1 && q == '=') { in_stream.read(); return factory.create(TokenType.TripleRightShiftAssign, index, line, col, 4); } return factory.create(TokenType.ShiftRightUnsigned, index, line, col, 3); } } return factory.create(TokenType.ShiftRight, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.GreaterEqual, index, line, col, 2); } return factory.create(TokenType.GreaterThan, index, line, col); case '?': return factory.create(TokenType.Question, index, line, col); case '$': return factory.create(TokenType.Dollar, index, line, col); case ';': return factory.create(TokenType.Semicolon, index, line, col); case ':': return factory.create(TokenType.Colon, index, line, col); case ',': return factory.create(TokenType.Comma, index, line, col); case '.': if (next == '.') { in_stream.read(); int p = in_stream.peek(); if (p != -1 && p == '.') { in_stream.read(); return factory.create(TokenType.TripleDot, index, line, col, 3); } return factory.create(TokenType.DoubleDot, index, line, col, 2); } return factory.create(TokenType.Dot, index, line, col); case ')': return factory.create(TokenType.CloseParenthesis, index, line, col); case '(': return factory.create(TokenType.OpenParenthesis, index, line, col); case ']': return factory.create(TokenType.CloseSquareBracket, index, line, col); case '[': return factory.create(TokenType.OpenSquareBracket, index, line, col); case '}': return factory.create(TokenType.CloseCurlyBrace, index, line, col); case '{': return factory.create(TokenType.OpenCurlyBrace, index, line, col); case '#': return factory.create(TokenType.Hash, index, line, col); default: return null; } } LexOperator(TokenFactory factory, LexerStream in_stream); }
LexOperator { public Token read() throws IOException, LexerException { int index = in_stream.getPosition(); int line = in_stream.getLine(); int col = in_stream.getCol(); int next = in_stream.peek(); if (next == -1) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Unexpected end of input stream when parsing operator"); } if (next == '.') { int tmp = in_stream.peek(2); if (tmp > 0 && Character.isDigit(tmp)) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Literal float encountered when operator '.' expected"); } } char cur = (char)in_stream.read(); next = in_stream.peek(); switch (cur) { case '+': switch (next) { case '+': in_stream.read(); return factory.create(TokenType.Increment, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.PlusAssign, index, line, col, 2); } return factory.create(TokenType.Plus, index, line, col); case '-': switch (next) { case '-': in_stream.read(); return factory.create(TokenType.Decrement, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.MinusAssign, index, line, col, 2); } return factory.create(TokenType.Minus, index, line, col); case '*': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.TimesAssign, index, line, col, 2); default: break; } return factory.create(TokenType.Times, index, line, col); case '/': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.DivAssign, index, line, col, 2); } return factory.create(TokenType.Div, index, line, col); case '%': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.ModAssign, index, line, col, 2); } return factory.create(TokenType.Mod, index, line, col); case '&': switch (next) { case '&': in_stream.read(); return factory.create(TokenType.LogicalAnd, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.BitwiseAndAssign, index, line, col, 2); } return factory.create(TokenType.BitwiseAnd, index, line, col); case '|': switch (next) { case '|': in_stream.read(); return factory.create(TokenType.LogicalOr, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.BitwiseOrAssign, index, line, col, 2); } return factory.create(TokenType.BitwiseOr, index, line, col); case '^': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.XorAssign, index, line, col, 2); case '^': in_stream.read(); if (in_stream.peek() == '=') { in_stream.read(); return factory.create(TokenType.PowAssign, index, line, col, 3); } return factory.create(TokenType.Pow, index, line, col, 2); } return factory.create(TokenType.Xor, index, line, col); case '!': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.NotEqual, index, line, col, 2); case '<': in_stream.read(); switch (in_stream.peek()) { case '=': in_stream.read(); return factory.create(TokenType.UnorderedOrGreater, index, line, col, 3); case '>': in_stream.read(); switch (in_stream.peek()) { case '=': in_stream.read(); return factory.create(TokenType.Unordered, index, line, col, 4); } return factory.create(TokenType.UnorderedOrEqual, index, line, col, 3); } return factory.create(TokenType.UnorderedGreaterOrEqual, index, line, col, 2); case '>': in_stream.read(); switch (in_stream.peek()) { case '=': in_stream.read(); return factory.create(TokenType.UnorderedOrLess, index, line, col, 3); default: break; } return factory.create(TokenType.UnorderedLessOrEqual, index, line, col, 2); } return factory.create(TokenType.Not, index, line, col); case '~': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.TildeAssign, index, line, col, 2); } return factory.create(TokenType.Tilde, index, line, col); case '=': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.Equal, index, line, col, 2); case '>': in_stream.read(); return factory.create(TokenType.GoesTo, index, line, col, 2); } return factory.create(TokenType.Assign, index, line, col); case '<': switch (next) { case '<': in_stream.read(); switch (in_stream.peek()) { case '=': in_stream.read(); return factory.create(TokenType.ShiftLeftAssign, index, line, col, 3); default: break; } return factory.create(TokenType.ShiftLeft, index, line, col, 2); case '>': in_stream.read(); switch (in_stream.peek()) { case '=': in_stream.read(); return factory.create(TokenType.LessEqualOrGreater, index, line, col, 3); default: break; } return factory.create(TokenType.LessOrGreater, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.LessEqual, index, line, col, 2); } return factory.create(TokenType.LessThan, index, line, col); case '>': switch (next) { case '>': in_stream.read(); int p = in_stream.peek(); if (p != -1) { switch ((char)p) { case '=': in_stream.read(); return factory.create(TokenType.ShiftRightAssign, index, line, col, 3); case '>': in_stream.read(); int q = in_stream.peek(); if (q != -1 && q == '=') { in_stream.read(); return factory.create(TokenType.TripleRightShiftAssign, index, line, col, 4); } return factory.create(TokenType.ShiftRightUnsigned, index, line, col, 3); } } return factory.create(TokenType.ShiftRight, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.GreaterEqual, index, line, col, 2); } return factory.create(TokenType.GreaterThan, index, line, col); case '?': return factory.create(TokenType.Question, index, line, col); case '$': return factory.create(TokenType.Dollar, index, line, col); case ';': return factory.create(TokenType.Semicolon, index, line, col); case ':': return factory.create(TokenType.Colon, index, line, col); case ',': return factory.create(TokenType.Comma, index, line, col); case '.': if (next == '.') { in_stream.read(); int p = in_stream.peek(); if (p != -1 && p == '.') { in_stream.read(); return factory.create(TokenType.TripleDot, index, line, col, 3); } return factory.create(TokenType.DoubleDot, index, line, col, 2); } return factory.create(TokenType.Dot, index, line, col); case ')': return factory.create(TokenType.CloseParenthesis, index, line, col); case '(': return factory.create(TokenType.OpenParenthesis, index, line, col); case ']': return factory.create(TokenType.CloseSquareBracket, index, line, col); case '[': return factory.create(TokenType.OpenSquareBracket, index, line, col); case '}': return factory.create(TokenType.CloseCurlyBrace, index, line, col); case '{': return factory.create(TokenType.OpenCurlyBrace, index, line, col); case '#': return factory.create(TokenType.Hash, index, line, col); default: return null; } } LexOperator(TokenFactory factory, LexerStream in_stream); Token read(); }
LexOperator { public Token read() throws IOException, LexerException { int index = in_stream.getPosition(); int line = in_stream.getLine(); int col = in_stream.getCol(); int next = in_stream.peek(); if (next == -1) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Unexpected end of input stream when parsing operator"); } if (next == '.') { int tmp = in_stream.peek(2); if (tmp > 0 && Character.isDigit(tmp)) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Literal float encountered when operator '.' expected"); } } char cur = (char)in_stream.read(); next = in_stream.peek(); switch (cur) { case '+': switch (next) { case '+': in_stream.read(); return factory.create(TokenType.Increment, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.PlusAssign, index, line, col, 2); } return factory.create(TokenType.Plus, index, line, col); case '-': switch (next) { case '-': in_stream.read(); return factory.create(TokenType.Decrement, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.MinusAssign, index, line, col, 2); } return factory.create(TokenType.Minus, index, line, col); case '*': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.TimesAssign, index, line, col, 2); default: break; } return factory.create(TokenType.Times, index, line, col); case '/': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.DivAssign, index, line, col, 2); } return factory.create(TokenType.Div, index, line, col); case '%': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.ModAssign, index, line, col, 2); } return factory.create(TokenType.Mod, index, line, col); case '&': switch (next) { case '&': in_stream.read(); return factory.create(TokenType.LogicalAnd, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.BitwiseAndAssign, index, line, col, 2); } return factory.create(TokenType.BitwiseAnd, index, line, col); case '|': switch (next) { case '|': in_stream.read(); return factory.create(TokenType.LogicalOr, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.BitwiseOrAssign, index, line, col, 2); } return factory.create(TokenType.BitwiseOr, index, line, col); case '^': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.XorAssign, index, line, col, 2); case '^': in_stream.read(); if (in_stream.peek() == '=') { in_stream.read(); return factory.create(TokenType.PowAssign, index, line, col, 3); } return factory.create(TokenType.Pow, index, line, col, 2); } return factory.create(TokenType.Xor, index, line, col); case '!': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.NotEqual, index, line, col, 2); case '<': in_stream.read(); switch (in_stream.peek()) { case '=': in_stream.read(); return factory.create(TokenType.UnorderedOrGreater, index, line, col, 3); case '>': in_stream.read(); switch (in_stream.peek()) { case '=': in_stream.read(); return factory.create(TokenType.Unordered, index, line, col, 4); } return factory.create(TokenType.UnorderedOrEqual, index, line, col, 3); } return factory.create(TokenType.UnorderedGreaterOrEqual, index, line, col, 2); case '>': in_stream.read(); switch (in_stream.peek()) { case '=': in_stream.read(); return factory.create(TokenType.UnorderedOrLess, index, line, col, 3); default: break; } return factory.create(TokenType.UnorderedLessOrEqual, index, line, col, 2); } return factory.create(TokenType.Not, index, line, col); case '~': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.TildeAssign, index, line, col, 2); } return factory.create(TokenType.Tilde, index, line, col); case '=': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.Equal, index, line, col, 2); case '>': in_stream.read(); return factory.create(TokenType.GoesTo, index, line, col, 2); } return factory.create(TokenType.Assign, index, line, col); case '<': switch (next) { case '<': in_stream.read(); switch (in_stream.peek()) { case '=': in_stream.read(); return factory.create(TokenType.ShiftLeftAssign, index, line, col, 3); default: break; } return factory.create(TokenType.ShiftLeft, index, line, col, 2); case '>': in_stream.read(); switch (in_stream.peek()) { case '=': in_stream.read(); return factory.create(TokenType.LessEqualOrGreater, index, line, col, 3); default: break; } return factory.create(TokenType.LessOrGreater, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.LessEqual, index, line, col, 2); } return factory.create(TokenType.LessThan, index, line, col); case '>': switch (next) { case '>': in_stream.read(); int p = in_stream.peek(); if (p != -1) { switch ((char)p) { case '=': in_stream.read(); return factory.create(TokenType.ShiftRightAssign, index, line, col, 3); case '>': in_stream.read(); int q = in_stream.peek(); if (q != -1 && q == '=') { in_stream.read(); return factory.create(TokenType.TripleRightShiftAssign, index, line, col, 4); } return factory.create(TokenType.ShiftRightUnsigned, index, line, col, 3); } } return factory.create(TokenType.ShiftRight, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.GreaterEqual, index, line, col, 2); } return factory.create(TokenType.GreaterThan, index, line, col); case '?': return factory.create(TokenType.Question, index, line, col); case '$': return factory.create(TokenType.Dollar, index, line, col); case ';': return factory.create(TokenType.Semicolon, index, line, col); case ':': return factory.create(TokenType.Colon, index, line, col); case ',': return factory.create(TokenType.Comma, index, line, col); case '.': if (next == '.') { in_stream.read(); int p = in_stream.peek(); if (p != -1 && p == '.') { in_stream.read(); return factory.create(TokenType.TripleDot, index, line, col, 3); } return factory.create(TokenType.DoubleDot, index, line, col, 2); } return factory.create(TokenType.Dot, index, line, col); case ')': return factory.create(TokenType.CloseParenthesis, index, line, col); case '(': return factory.create(TokenType.OpenParenthesis, index, line, col); case ']': return factory.create(TokenType.CloseSquareBracket, index, line, col); case '[': return factory.create(TokenType.OpenSquareBracket, index, line, col); case '}': return factory.create(TokenType.CloseCurlyBrace, index, line, col); case '{': return factory.create(TokenType.OpenCurlyBrace, index, line, col); case '#': return factory.create(TokenType.Hash, index, line, col); default: return null; } } LexOperator(TokenFactory factory, LexerStream in_stream); Token read(); }
@Test public void bitwise() throws Exception { String str = "a&=b|c|=d&e^=f^g;"; LexerStream ls = new LexerStream(new StringReader(str)); LexOperator lex = new LexOperator(new BaseTokenFactory(), ls); ls.read(); Token tok = lex.read(); assertEquals(TokenType.BitwiseAndAssign, tok.getType()); assertEquals(1, tok.getLine()); assertEquals(2, tok.getCol()); assertEquals(1, tok.getEndLine()); assertEquals(4, tok.getEndCol()); ls.read(); tok = lex.read(); assertEquals(TokenType.BitwiseOr, tok.getType()); assertEquals(1, tok.getLine()); assertEquals(5, tok.getCol()); assertEquals(1, tok.getEndLine()); assertEquals(6, tok.getEndCol()); ls.read(); tok = lex.read(); assertEquals(TokenType.BitwiseOrAssign, tok.getType()); assertEquals(1, tok.getLine()); assertEquals(7, tok.getCol()); assertEquals(1, tok.getEndLine()); assertEquals(9, tok.getEndCol()); ls.read(); tok = lex.read(); assertEquals(TokenType.BitwiseAnd, tok.getType()); assertEquals(1, tok.getLine()); assertEquals(10, tok.getCol()); assertEquals(1, tok.getEndLine()); assertEquals(11, tok.getEndCol()); ls.read(); tok = lex.read(); assertEquals(TokenType.XorAssign, tok.getType()); assertEquals(1, tok.getLine()); assertEquals(12, tok.getCol()); assertEquals(1, tok.getEndLine()); assertEquals(14, tok.getEndCol()); ls.read(); tok = lex.read(); assertEquals(TokenType.Xor, tok.getType()); assertEquals(1, tok.getLine()); assertEquals(15, tok.getCol()); assertEquals(1, tok.getEndLine()); assertEquals(16, tok.getEndCol()); assertEquals('g', ls.read()); tok = lex.read(); assertEquals(TokenType.Semicolon, tok.getType()); assertEquals(-1, ls.read()); }
public Token read() throws IOException, LexerException { int index = in_stream.getPosition(); int line = in_stream.getLine(); int col = in_stream.getCol(); int next = in_stream.peek(); if (next == -1) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Unexpected end of input stream when parsing operator"); } if (next == '.') { int tmp = in_stream.peek(2); if (tmp > 0 && Character.isDigit(tmp)) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Literal float encountered when operator '.' expected"); } } char cur = (char)in_stream.read(); next = in_stream.peek(); switch (cur) { case '+': switch (next) { case '+': in_stream.read(); return factory.create(TokenType.Increment, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.PlusAssign, index, line, col, 2); } return factory.create(TokenType.Plus, index, line, col); case '-': switch (next) { case '-': in_stream.read(); return factory.create(TokenType.Decrement, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.MinusAssign, index, line, col, 2); } return factory.create(TokenType.Minus, index, line, col); case '*': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.TimesAssign, index, line, col, 2); default: break; } return factory.create(TokenType.Times, index, line, col); case '/': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.DivAssign, index, line, col, 2); } return factory.create(TokenType.Div, index, line, col); case '%': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.ModAssign, index, line, col, 2); } return factory.create(TokenType.Mod, index, line, col); case '&': switch (next) { case '&': in_stream.read(); return factory.create(TokenType.LogicalAnd, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.BitwiseAndAssign, index, line, col, 2); } return factory.create(TokenType.BitwiseAnd, index, line, col); case '|': switch (next) { case '|': in_stream.read(); return factory.create(TokenType.LogicalOr, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.BitwiseOrAssign, index, line, col, 2); } return factory.create(TokenType.BitwiseOr, index, line, col); case '^': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.XorAssign, index, line, col, 2); case '^': in_stream.read(); if (in_stream.peek() == '=') { in_stream.read(); return factory.create(TokenType.PowAssign, index, line, col, 3); } return factory.create(TokenType.Pow, index, line, col, 2); } return factory.create(TokenType.Xor, index, line, col); case '!': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.NotEqual, index, line, col, 2); case '<': in_stream.read(); switch (in_stream.peek()) { case '=': in_stream.read(); return factory.create(TokenType.UnorderedOrGreater, index, line, col, 3); case '>': in_stream.read(); switch (in_stream.peek()) { case '=': in_stream.read(); return factory.create(TokenType.Unordered, index, line, col, 4); } return factory.create(TokenType.UnorderedOrEqual, index, line, col, 3); } return factory.create(TokenType.UnorderedGreaterOrEqual, index, line, col, 2); case '>': in_stream.read(); switch (in_stream.peek()) { case '=': in_stream.read(); return factory.create(TokenType.UnorderedOrLess, index, line, col, 3); default: break; } return factory.create(TokenType.UnorderedLessOrEqual, index, line, col, 2); } return factory.create(TokenType.Not, index, line, col); case '~': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.TildeAssign, index, line, col, 2); } return factory.create(TokenType.Tilde, index, line, col); case '=': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.Equal, index, line, col, 2); case '>': in_stream.read(); return factory.create(TokenType.GoesTo, index, line, col, 2); } return factory.create(TokenType.Assign, index, line, col); case '<': switch (next) { case '<': in_stream.read(); switch (in_stream.peek()) { case '=': in_stream.read(); return factory.create(TokenType.ShiftLeftAssign, index, line, col, 3); default: break; } return factory.create(TokenType.ShiftLeft, index, line, col, 2); case '>': in_stream.read(); switch (in_stream.peek()) { case '=': in_stream.read(); return factory.create(TokenType.LessEqualOrGreater, index, line, col, 3); default: break; } return factory.create(TokenType.LessOrGreater, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.LessEqual, index, line, col, 2); } return factory.create(TokenType.LessThan, index, line, col); case '>': switch (next) { case '>': in_stream.read(); int p = in_stream.peek(); if (p != -1) { switch ((char)p) { case '=': in_stream.read(); return factory.create(TokenType.ShiftRightAssign, index, line, col, 3); case '>': in_stream.read(); int q = in_stream.peek(); if (q != -1 && q == '=') { in_stream.read(); return factory.create(TokenType.TripleRightShiftAssign, index, line, col, 4); } return factory.create(TokenType.ShiftRightUnsigned, index, line, col, 3); } } return factory.create(TokenType.ShiftRight, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.GreaterEqual, index, line, col, 2); } return factory.create(TokenType.GreaterThan, index, line, col); case '?': return factory.create(TokenType.Question, index, line, col); case '$': return factory.create(TokenType.Dollar, index, line, col); case ';': return factory.create(TokenType.Semicolon, index, line, col); case ':': return factory.create(TokenType.Colon, index, line, col); case ',': return factory.create(TokenType.Comma, index, line, col); case '.': if (next == '.') { in_stream.read(); int p = in_stream.peek(); if (p != -1 && p == '.') { in_stream.read(); return factory.create(TokenType.TripleDot, index, line, col, 3); } return factory.create(TokenType.DoubleDot, index, line, col, 2); } return factory.create(TokenType.Dot, index, line, col); case ')': return factory.create(TokenType.CloseParenthesis, index, line, col); case '(': return factory.create(TokenType.OpenParenthesis, index, line, col); case ']': return factory.create(TokenType.CloseSquareBracket, index, line, col); case '[': return factory.create(TokenType.OpenSquareBracket, index, line, col); case '}': return factory.create(TokenType.CloseCurlyBrace, index, line, col); case '{': return factory.create(TokenType.OpenCurlyBrace, index, line, col); case '#': return factory.create(TokenType.Hash, index, line, col); default: return null; } }
LexOperator { public Token read() throws IOException, LexerException { int index = in_stream.getPosition(); int line = in_stream.getLine(); int col = in_stream.getCol(); int next = in_stream.peek(); if (next == -1) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Unexpected end of input stream when parsing operator"); } if (next == '.') { int tmp = in_stream.peek(2); if (tmp > 0 && Character.isDigit(tmp)) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Literal float encountered when operator '.' expected"); } } char cur = (char)in_stream.read(); next = in_stream.peek(); switch (cur) { case '+': switch (next) { case '+': in_stream.read(); return factory.create(TokenType.Increment, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.PlusAssign, index, line, col, 2); } return factory.create(TokenType.Plus, index, line, col); case '-': switch (next) { case '-': in_stream.read(); return factory.create(TokenType.Decrement, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.MinusAssign, index, line, col, 2); } return factory.create(TokenType.Minus, index, line, col); case '*': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.TimesAssign, index, line, col, 2); default: break; } return factory.create(TokenType.Times, index, line, col); case '/': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.DivAssign, index, line, col, 2); } return factory.create(TokenType.Div, index, line, col); case '%': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.ModAssign, index, line, col, 2); } return factory.create(TokenType.Mod, index, line, col); case '&': switch (next) { case '&': in_stream.read(); return factory.create(TokenType.LogicalAnd, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.BitwiseAndAssign, index, line, col, 2); } return factory.create(TokenType.BitwiseAnd, index, line, col); case '|': switch (next) { case '|': in_stream.read(); return factory.create(TokenType.LogicalOr, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.BitwiseOrAssign, index, line, col, 2); } return factory.create(TokenType.BitwiseOr, index, line, col); case '^': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.XorAssign, index, line, col, 2); case '^': in_stream.read(); if (in_stream.peek() == '=') { in_stream.read(); return factory.create(TokenType.PowAssign, index, line, col, 3); } return factory.create(TokenType.Pow, index, line, col, 2); } return factory.create(TokenType.Xor, index, line, col); case '!': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.NotEqual, index, line, col, 2); case '<': in_stream.read(); switch (in_stream.peek()) { case '=': in_stream.read(); return factory.create(TokenType.UnorderedOrGreater, index, line, col, 3); case '>': in_stream.read(); switch (in_stream.peek()) { case '=': in_stream.read(); return factory.create(TokenType.Unordered, index, line, col, 4); } return factory.create(TokenType.UnorderedOrEqual, index, line, col, 3); } return factory.create(TokenType.UnorderedGreaterOrEqual, index, line, col, 2); case '>': in_stream.read(); switch (in_stream.peek()) { case '=': in_stream.read(); return factory.create(TokenType.UnorderedOrLess, index, line, col, 3); default: break; } return factory.create(TokenType.UnorderedLessOrEqual, index, line, col, 2); } return factory.create(TokenType.Not, index, line, col); case '~': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.TildeAssign, index, line, col, 2); } return factory.create(TokenType.Tilde, index, line, col); case '=': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.Equal, index, line, col, 2); case '>': in_stream.read(); return factory.create(TokenType.GoesTo, index, line, col, 2); } return factory.create(TokenType.Assign, index, line, col); case '<': switch (next) { case '<': in_stream.read(); switch (in_stream.peek()) { case '=': in_stream.read(); return factory.create(TokenType.ShiftLeftAssign, index, line, col, 3); default: break; } return factory.create(TokenType.ShiftLeft, index, line, col, 2); case '>': in_stream.read(); switch (in_stream.peek()) { case '=': in_stream.read(); return factory.create(TokenType.LessEqualOrGreater, index, line, col, 3); default: break; } return factory.create(TokenType.LessOrGreater, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.LessEqual, index, line, col, 2); } return factory.create(TokenType.LessThan, index, line, col); case '>': switch (next) { case '>': in_stream.read(); int p = in_stream.peek(); if (p != -1) { switch ((char)p) { case '=': in_stream.read(); return factory.create(TokenType.ShiftRightAssign, index, line, col, 3); case '>': in_stream.read(); int q = in_stream.peek(); if (q != -1 && q == '=') { in_stream.read(); return factory.create(TokenType.TripleRightShiftAssign, index, line, col, 4); } return factory.create(TokenType.ShiftRightUnsigned, index, line, col, 3); } } return factory.create(TokenType.ShiftRight, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.GreaterEqual, index, line, col, 2); } return factory.create(TokenType.GreaterThan, index, line, col); case '?': return factory.create(TokenType.Question, index, line, col); case '$': return factory.create(TokenType.Dollar, index, line, col); case ';': return factory.create(TokenType.Semicolon, index, line, col); case ':': return factory.create(TokenType.Colon, index, line, col); case ',': return factory.create(TokenType.Comma, index, line, col); case '.': if (next == '.') { in_stream.read(); int p = in_stream.peek(); if (p != -1 && p == '.') { in_stream.read(); return factory.create(TokenType.TripleDot, index, line, col, 3); } return factory.create(TokenType.DoubleDot, index, line, col, 2); } return factory.create(TokenType.Dot, index, line, col); case ')': return factory.create(TokenType.CloseParenthesis, index, line, col); case '(': return factory.create(TokenType.OpenParenthesis, index, line, col); case ']': return factory.create(TokenType.CloseSquareBracket, index, line, col); case '[': return factory.create(TokenType.OpenSquareBracket, index, line, col); case '}': return factory.create(TokenType.CloseCurlyBrace, index, line, col); case '{': return factory.create(TokenType.OpenCurlyBrace, index, line, col); case '#': return factory.create(TokenType.Hash, index, line, col); default: return null; } } }
LexOperator { public Token read() throws IOException, LexerException { int index = in_stream.getPosition(); int line = in_stream.getLine(); int col = in_stream.getCol(); int next = in_stream.peek(); if (next == -1) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Unexpected end of input stream when parsing operator"); } if (next == '.') { int tmp = in_stream.peek(2); if (tmp > 0 && Character.isDigit(tmp)) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Literal float encountered when operator '.' expected"); } } char cur = (char)in_stream.read(); next = in_stream.peek(); switch (cur) { case '+': switch (next) { case '+': in_stream.read(); return factory.create(TokenType.Increment, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.PlusAssign, index, line, col, 2); } return factory.create(TokenType.Plus, index, line, col); case '-': switch (next) { case '-': in_stream.read(); return factory.create(TokenType.Decrement, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.MinusAssign, index, line, col, 2); } return factory.create(TokenType.Minus, index, line, col); case '*': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.TimesAssign, index, line, col, 2); default: break; } return factory.create(TokenType.Times, index, line, col); case '/': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.DivAssign, index, line, col, 2); } return factory.create(TokenType.Div, index, line, col); case '%': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.ModAssign, index, line, col, 2); } return factory.create(TokenType.Mod, index, line, col); case '&': switch (next) { case '&': in_stream.read(); return factory.create(TokenType.LogicalAnd, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.BitwiseAndAssign, index, line, col, 2); } return factory.create(TokenType.BitwiseAnd, index, line, col); case '|': switch (next) { case '|': in_stream.read(); return factory.create(TokenType.LogicalOr, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.BitwiseOrAssign, index, line, col, 2); } return factory.create(TokenType.BitwiseOr, index, line, col); case '^': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.XorAssign, index, line, col, 2); case '^': in_stream.read(); if (in_stream.peek() == '=') { in_stream.read(); return factory.create(TokenType.PowAssign, index, line, col, 3); } return factory.create(TokenType.Pow, index, line, col, 2); } return factory.create(TokenType.Xor, index, line, col); case '!': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.NotEqual, index, line, col, 2); case '<': in_stream.read(); switch (in_stream.peek()) { case '=': in_stream.read(); return factory.create(TokenType.UnorderedOrGreater, index, line, col, 3); case '>': in_stream.read(); switch (in_stream.peek()) { case '=': in_stream.read(); return factory.create(TokenType.Unordered, index, line, col, 4); } return factory.create(TokenType.UnorderedOrEqual, index, line, col, 3); } return factory.create(TokenType.UnorderedGreaterOrEqual, index, line, col, 2); case '>': in_stream.read(); switch (in_stream.peek()) { case '=': in_stream.read(); return factory.create(TokenType.UnorderedOrLess, index, line, col, 3); default: break; } return factory.create(TokenType.UnorderedLessOrEqual, index, line, col, 2); } return factory.create(TokenType.Not, index, line, col); case '~': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.TildeAssign, index, line, col, 2); } return factory.create(TokenType.Tilde, index, line, col); case '=': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.Equal, index, line, col, 2); case '>': in_stream.read(); return factory.create(TokenType.GoesTo, index, line, col, 2); } return factory.create(TokenType.Assign, index, line, col); case '<': switch (next) { case '<': in_stream.read(); switch (in_stream.peek()) { case '=': in_stream.read(); return factory.create(TokenType.ShiftLeftAssign, index, line, col, 3); default: break; } return factory.create(TokenType.ShiftLeft, index, line, col, 2); case '>': in_stream.read(); switch (in_stream.peek()) { case '=': in_stream.read(); return factory.create(TokenType.LessEqualOrGreater, index, line, col, 3); default: break; } return factory.create(TokenType.LessOrGreater, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.LessEqual, index, line, col, 2); } return factory.create(TokenType.LessThan, index, line, col); case '>': switch (next) { case '>': in_stream.read(); int p = in_stream.peek(); if (p != -1) { switch ((char)p) { case '=': in_stream.read(); return factory.create(TokenType.ShiftRightAssign, index, line, col, 3); case '>': in_stream.read(); int q = in_stream.peek(); if (q != -1 && q == '=') { in_stream.read(); return factory.create(TokenType.TripleRightShiftAssign, index, line, col, 4); } return factory.create(TokenType.ShiftRightUnsigned, index, line, col, 3); } } return factory.create(TokenType.ShiftRight, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.GreaterEqual, index, line, col, 2); } return factory.create(TokenType.GreaterThan, index, line, col); case '?': return factory.create(TokenType.Question, index, line, col); case '$': return factory.create(TokenType.Dollar, index, line, col); case ';': return factory.create(TokenType.Semicolon, index, line, col); case ':': return factory.create(TokenType.Colon, index, line, col); case ',': return factory.create(TokenType.Comma, index, line, col); case '.': if (next == '.') { in_stream.read(); int p = in_stream.peek(); if (p != -1 && p == '.') { in_stream.read(); return factory.create(TokenType.TripleDot, index, line, col, 3); } return factory.create(TokenType.DoubleDot, index, line, col, 2); } return factory.create(TokenType.Dot, index, line, col); case ')': return factory.create(TokenType.CloseParenthesis, index, line, col); case '(': return factory.create(TokenType.OpenParenthesis, index, line, col); case ']': return factory.create(TokenType.CloseSquareBracket, index, line, col); case '[': return factory.create(TokenType.OpenSquareBracket, index, line, col); case '}': return factory.create(TokenType.CloseCurlyBrace, index, line, col); case '{': return factory.create(TokenType.OpenCurlyBrace, index, line, col); case '#': return factory.create(TokenType.Hash, index, line, col); default: return null; } } LexOperator(TokenFactory factory, LexerStream in_stream); }
LexOperator { public Token read() throws IOException, LexerException { int index = in_stream.getPosition(); int line = in_stream.getLine(); int col = in_stream.getCol(); int next = in_stream.peek(); if (next == -1) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Unexpected end of input stream when parsing operator"); } if (next == '.') { int tmp = in_stream.peek(2); if (tmp > 0 && Character.isDigit(tmp)) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Literal float encountered when operator '.' expected"); } } char cur = (char)in_stream.read(); next = in_stream.peek(); switch (cur) { case '+': switch (next) { case '+': in_stream.read(); return factory.create(TokenType.Increment, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.PlusAssign, index, line, col, 2); } return factory.create(TokenType.Plus, index, line, col); case '-': switch (next) { case '-': in_stream.read(); return factory.create(TokenType.Decrement, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.MinusAssign, index, line, col, 2); } return factory.create(TokenType.Minus, index, line, col); case '*': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.TimesAssign, index, line, col, 2); default: break; } return factory.create(TokenType.Times, index, line, col); case '/': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.DivAssign, index, line, col, 2); } return factory.create(TokenType.Div, index, line, col); case '%': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.ModAssign, index, line, col, 2); } return factory.create(TokenType.Mod, index, line, col); case '&': switch (next) { case '&': in_stream.read(); return factory.create(TokenType.LogicalAnd, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.BitwiseAndAssign, index, line, col, 2); } return factory.create(TokenType.BitwiseAnd, index, line, col); case '|': switch (next) { case '|': in_stream.read(); return factory.create(TokenType.LogicalOr, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.BitwiseOrAssign, index, line, col, 2); } return factory.create(TokenType.BitwiseOr, index, line, col); case '^': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.XorAssign, index, line, col, 2); case '^': in_stream.read(); if (in_stream.peek() == '=') { in_stream.read(); return factory.create(TokenType.PowAssign, index, line, col, 3); } return factory.create(TokenType.Pow, index, line, col, 2); } return factory.create(TokenType.Xor, index, line, col); case '!': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.NotEqual, index, line, col, 2); case '<': in_stream.read(); switch (in_stream.peek()) { case '=': in_stream.read(); return factory.create(TokenType.UnorderedOrGreater, index, line, col, 3); case '>': in_stream.read(); switch (in_stream.peek()) { case '=': in_stream.read(); return factory.create(TokenType.Unordered, index, line, col, 4); } return factory.create(TokenType.UnorderedOrEqual, index, line, col, 3); } return factory.create(TokenType.UnorderedGreaterOrEqual, index, line, col, 2); case '>': in_stream.read(); switch (in_stream.peek()) { case '=': in_stream.read(); return factory.create(TokenType.UnorderedOrLess, index, line, col, 3); default: break; } return factory.create(TokenType.UnorderedLessOrEqual, index, line, col, 2); } return factory.create(TokenType.Not, index, line, col); case '~': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.TildeAssign, index, line, col, 2); } return factory.create(TokenType.Tilde, index, line, col); case '=': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.Equal, index, line, col, 2); case '>': in_stream.read(); return factory.create(TokenType.GoesTo, index, line, col, 2); } return factory.create(TokenType.Assign, index, line, col); case '<': switch (next) { case '<': in_stream.read(); switch (in_stream.peek()) { case '=': in_stream.read(); return factory.create(TokenType.ShiftLeftAssign, index, line, col, 3); default: break; } return factory.create(TokenType.ShiftLeft, index, line, col, 2); case '>': in_stream.read(); switch (in_stream.peek()) { case '=': in_stream.read(); return factory.create(TokenType.LessEqualOrGreater, index, line, col, 3); default: break; } return factory.create(TokenType.LessOrGreater, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.LessEqual, index, line, col, 2); } return factory.create(TokenType.LessThan, index, line, col); case '>': switch (next) { case '>': in_stream.read(); int p = in_stream.peek(); if (p != -1) { switch ((char)p) { case '=': in_stream.read(); return factory.create(TokenType.ShiftRightAssign, index, line, col, 3); case '>': in_stream.read(); int q = in_stream.peek(); if (q != -1 && q == '=') { in_stream.read(); return factory.create(TokenType.TripleRightShiftAssign, index, line, col, 4); } return factory.create(TokenType.ShiftRightUnsigned, index, line, col, 3); } } return factory.create(TokenType.ShiftRight, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.GreaterEqual, index, line, col, 2); } return factory.create(TokenType.GreaterThan, index, line, col); case '?': return factory.create(TokenType.Question, index, line, col); case '$': return factory.create(TokenType.Dollar, index, line, col); case ';': return factory.create(TokenType.Semicolon, index, line, col); case ':': return factory.create(TokenType.Colon, index, line, col); case ',': return factory.create(TokenType.Comma, index, line, col); case '.': if (next == '.') { in_stream.read(); int p = in_stream.peek(); if (p != -1 && p == '.') { in_stream.read(); return factory.create(TokenType.TripleDot, index, line, col, 3); } return factory.create(TokenType.DoubleDot, index, line, col, 2); } return factory.create(TokenType.Dot, index, line, col); case ')': return factory.create(TokenType.CloseParenthesis, index, line, col); case '(': return factory.create(TokenType.OpenParenthesis, index, line, col); case ']': return factory.create(TokenType.CloseSquareBracket, index, line, col); case '[': return factory.create(TokenType.OpenSquareBracket, index, line, col); case '}': return factory.create(TokenType.CloseCurlyBrace, index, line, col); case '{': return factory.create(TokenType.OpenCurlyBrace, index, line, col); case '#': return factory.create(TokenType.Hash, index, line, col); default: return null; } } LexOperator(TokenFactory factory, LexerStream in_stream); Token read(); }
LexOperator { public Token read() throws IOException, LexerException { int index = in_stream.getPosition(); int line = in_stream.getLine(); int col = in_stream.getCol(); int next = in_stream.peek(); if (next == -1) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Unexpected end of input stream when parsing operator"); } if (next == '.') { int tmp = in_stream.peek(2); if (tmp > 0 && Character.isDigit(tmp)) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Literal float encountered when operator '.' expected"); } } char cur = (char)in_stream.read(); next = in_stream.peek(); switch (cur) { case '+': switch (next) { case '+': in_stream.read(); return factory.create(TokenType.Increment, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.PlusAssign, index, line, col, 2); } return factory.create(TokenType.Plus, index, line, col); case '-': switch (next) { case '-': in_stream.read(); return factory.create(TokenType.Decrement, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.MinusAssign, index, line, col, 2); } return factory.create(TokenType.Minus, index, line, col); case '*': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.TimesAssign, index, line, col, 2); default: break; } return factory.create(TokenType.Times, index, line, col); case '/': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.DivAssign, index, line, col, 2); } return factory.create(TokenType.Div, index, line, col); case '%': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.ModAssign, index, line, col, 2); } return factory.create(TokenType.Mod, index, line, col); case '&': switch (next) { case '&': in_stream.read(); return factory.create(TokenType.LogicalAnd, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.BitwiseAndAssign, index, line, col, 2); } return factory.create(TokenType.BitwiseAnd, index, line, col); case '|': switch (next) { case '|': in_stream.read(); return factory.create(TokenType.LogicalOr, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.BitwiseOrAssign, index, line, col, 2); } return factory.create(TokenType.BitwiseOr, index, line, col); case '^': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.XorAssign, index, line, col, 2); case '^': in_stream.read(); if (in_stream.peek() == '=') { in_stream.read(); return factory.create(TokenType.PowAssign, index, line, col, 3); } return factory.create(TokenType.Pow, index, line, col, 2); } return factory.create(TokenType.Xor, index, line, col); case '!': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.NotEqual, index, line, col, 2); case '<': in_stream.read(); switch (in_stream.peek()) { case '=': in_stream.read(); return factory.create(TokenType.UnorderedOrGreater, index, line, col, 3); case '>': in_stream.read(); switch (in_stream.peek()) { case '=': in_stream.read(); return factory.create(TokenType.Unordered, index, line, col, 4); } return factory.create(TokenType.UnorderedOrEqual, index, line, col, 3); } return factory.create(TokenType.UnorderedGreaterOrEqual, index, line, col, 2); case '>': in_stream.read(); switch (in_stream.peek()) { case '=': in_stream.read(); return factory.create(TokenType.UnorderedOrLess, index, line, col, 3); default: break; } return factory.create(TokenType.UnorderedLessOrEqual, index, line, col, 2); } return factory.create(TokenType.Not, index, line, col); case '~': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.TildeAssign, index, line, col, 2); } return factory.create(TokenType.Tilde, index, line, col); case '=': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.Equal, index, line, col, 2); case '>': in_stream.read(); return factory.create(TokenType.GoesTo, index, line, col, 2); } return factory.create(TokenType.Assign, index, line, col); case '<': switch (next) { case '<': in_stream.read(); switch (in_stream.peek()) { case '=': in_stream.read(); return factory.create(TokenType.ShiftLeftAssign, index, line, col, 3); default: break; } return factory.create(TokenType.ShiftLeft, index, line, col, 2); case '>': in_stream.read(); switch (in_stream.peek()) { case '=': in_stream.read(); return factory.create(TokenType.LessEqualOrGreater, index, line, col, 3); default: break; } return factory.create(TokenType.LessOrGreater, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.LessEqual, index, line, col, 2); } return factory.create(TokenType.LessThan, index, line, col); case '>': switch (next) { case '>': in_stream.read(); int p = in_stream.peek(); if (p != -1) { switch ((char)p) { case '=': in_stream.read(); return factory.create(TokenType.ShiftRightAssign, index, line, col, 3); case '>': in_stream.read(); int q = in_stream.peek(); if (q != -1 && q == '=') { in_stream.read(); return factory.create(TokenType.TripleRightShiftAssign, index, line, col, 4); } return factory.create(TokenType.ShiftRightUnsigned, index, line, col, 3); } } return factory.create(TokenType.ShiftRight, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.GreaterEqual, index, line, col, 2); } return factory.create(TokenType.GreaterThan, index, line, col); case '?': return factory.create(TokenType.Question, index, line, col); case '$': return factory.create(TokenType.Dollar, index, line, col); case ';': return factory.create(TokenType.Semicolon, index, line, col); case ':': return factory.create(TokenType.Colon, index, line, col); case ',': return factory.create(TokenType.Comma, index, line, col); case '.': if (next == '.') { in_stream.read(); int p = in_stream.peek(); if (p != -1 && p == '.') { in_stream.read(); return factory.create(TokenType.TripleDot, index, line, col, 3); } return factory.create(TokenType.DoubleDot, index, line, col, 2); } return factory.create(TokenType.Dot, index, line, col); case ')': return factory.create(TokenType.CloseParenthesis, index, line, col); case '(': return factory.create(TokenType.OpenParenthesis, index, line, col); case ']': return factory.create(TokenType.CloseSquareBracket, index, line, col); case '[': return factory.create(TokenType.OpenSquareBracket, index, line, col); case '}': return factory.create(TokenType.CloseCurlyBrace, index, line, col); case '{': return factory.create(TokenType.OpenCurlyBrace, index, line, col); case '#': return factory.create(TokenType.Hash, index, line, col); default: return null; } } LexOperator(TokenFactory factory, LexerStream in_stream); Token read(); }
@Test public void logical() throws Exception { String str = "a&&b ||!c;"; LexerStream ls = new LexerStream(new StringReader(str)); LexOperator lex = new LexOperator(new BaseTokenFactory(), ls); ls.read(); Token tok = lex.read(); assertEquals(TokenType.LogicalAnd, tok.getType()); assertEquals(1, tok.getLine()); assertEquals(2, tok.getCol()); assertEquals(1, tok.getEndLine()); assertEquals(4, tok.getEndCol()); ls.read(); ls.read(); tok = lex.read(); assertEquals(TokenType.LogicalOr, tok.getType()); assertEquals(1, tok.getLine()); assertEquals(6, tok.getCol()); assertEquals(1, tok.getEndLine()); assertEquals(8, tok.getEndCol()); tok = lex.read(); assertEquals(TokenType.Not, tok.getType()); assertEquals(1, tok.getLine()); assertEquals(8, tok.getCol()); assertEquals(1, tok.getEndLine()); assertEquals(9, tok.getEndCol()); ls.read(); tok = lex.read(); assertEquals(TokenType.Semicolon, tok.getType()); }
public Token read() throws IOException, LexerException { int index = in_stream.getPosition(); int line = in_stream.getLine(); int col = in_stream.getCol(); int next = in_stream.peek(); if (next == -1) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Unexpected end of input stream when parsing operator"); } if (next == '.') { int tmp = in_stream.peek(2); if (tmp > 0 && Character.isDigit(tmp)) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Literal float encountered when operator '.' expected"); } } char cur = (char)in_stream.read(); next = in_stream.peek(); switch (cur) { case '+': switch (next) { case '+': in_stream.read(); return factory.create(TokenType.Increment, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.PlusAssign, index, line, col, 2); } return factory.create(TokenType.Plus, index, line, col); case '-': switch (next) { case '-': in_stream.read(); return factory.create(TokenType.Decrement, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.MinusAssign, index, line, col, 2); } return factory.create(TokenType.Minus, index, line, col); case '*': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.TimesAssign, index, line, col, 2); default: break; } return factory.create(TokenType.Times, index, line, col); case '/': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.DivAssign, index, line, col, 2); } return factory.create(TokenType.Div, index, line, col); case '%': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.ModAssign, index, line, col, 2); } return factory.create(TokenType.Mod, index, line, col); case '&': switch (next) { case '&': in_stream.read(); return factory.create(TokenType.LogicalAnd, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.BitwiseAndAssign, index, line, col, 2); } return factory.create(TokenType.BitwiseAnd, index, line, col); case '|': switch (next) { case '|': in_stream.read(); return factory.create(TokenType.LogicalOr, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.BitwiseOrAssign, index, line, col, 2); } return factory.create(TokenType.BitwiseOr, index, line, col); case '^': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.XorAssign, index, line, col, 2); case '^': in_stream.read(); if (in_stream.peek() == '=') { in_stream.read(); return factory.create(TokenType.PowAssign, index, line, col, 3); } return factory.create(TokenType.Pow, index, line, col, 2); } return factory.create(TokenType.Xor, index, line, col); case '!': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.NotEqual, index, line, col, 2); case '<': in_stream.read(); switch (in_stream.peek()) { case '=': in_stream.read(); return factory.create(TokenType.UnorderedOrGreater, index, line, col, 3); case '>': in_stream.read(); switch (in_stream.peek()) { case '=': in_stream.read(); return factory.create(TokenType.Unordered, index, line, col, 4); } return factory.create(TokenType.UnorderedOrEqual, index, line, col, 3); } return factory.create(TokenType.UnorderedGreaterOrEqual, index, line, col, 2); case '>': in_stream.read(); switch (in_stream.peek()) { case '=': in_stream.read(); return factory.create(TokenType.UnorderedOrLess, index, line, col, 3); default: break; } return factory.create(TokenType.UnorderedLessOrEqual, index, line, col, 2); } return factory.create(TokenType.Not, index, line, col); case '~': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.TildeAssign, index, line, col, 2); } return factory.create(TokenType.Tilde, index, line, col); case '=': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.Equal, index, line, col, 2); case '>': in_stream.read(); return factory.create(TokenType.GoesTo, index, line, col, 2); } return factory.create(TokenType.Assign, index, line, col); case '<': switch (next) { case '<': in_stream.read(); switch (in_stream.peek()) { case '=': in_stream.read(); return factory.create(TokenType.ShiftLeftAssign, index, line, col, 3); default: break; } return factory.create(TokenType.ShiftLeft, index, line, col, 2); case '>': in_stream.read(); switch (in_stream.peek()) { case '=': in_stream.read(); return factory.create(TokenType.LessEqualOrGreater, index, line, col, 3); default: break; } return factory.create(TokenType.LessOrGreater, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.LessEqual, index, line, col, 2); } return factory.create(TokenType.LessThan, index, line, col); case '>': switch (next) { case '>': in_stream.read(); int p = in_stream.peek(); if (p != -1) { switch ((char)p) { case '=': in_stream.read(); return factory.create(TokenType.ShiftRightAssign, index, line, col, 3); case '>': in_stream.read(); int q = in_stream.peek(); if (q != -1 && q == '=') { in_stream.read(); return factory.create(TokenType.TripleRightShiftAssign, index, line, col, 4); } return factory.create(TokenType.ShiftRightUnsigned, index, line, col, 3); } } return factory.create(TokenType.ShiftRight, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.GreaterEqual, index, line, col, 2); } return factory.create(TokenType.GreaterThan, index, line, col); case '?': return factory.create(TokenType.Question, index, line, col); case '$': return factory.create(TokenType.Dollar, index, line, col); case ';': return factory.create(TokenType.Semicolon, index, line, col); case ':': return factory.create(TokenType.Colon, index, line, col); case ',': return factory.create(TokenType.Comma, index, line, col); case '.': if (next == '.') { in_stream.read(); int p = in_stream.peek(); if (p != -1 && p == '.') { in_stream.read(); return factory.create(TokenType.TripleDot, index, line, col, 3); } return factory.create(TokenType.DoubleDot, index, line, col, 2); } return factory.create(TokenType.Dot, index, line, col); case ')': return factory.create(TokenType.CloseParenthesis, index, line, col); case '(': return factory.create(TokenType.OpenParenthesis, index, line, col); case ']': return factory.create(TokenType.CloseSquareBracket, index, line, col); case '[': return factory.create(TokenType.OpenSquareBracket, index, line, col); case '}': return factory.create(TokenType.CloseCurlyBrace, index, line, col); case '{': return factory.create(TokenType.OpenCurlyBrace, index, line, col); case '#': return factory.create(TokenType.Hash, index, line, col); default: return null; } }
LexOperator { public Token read() throws IOException, LexerException { int index = in_stream.getPosition(); int line = in_stream.getLine(); int col = in_stream.getCol(); int next = in_stream.peek(); if (next == -1) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Unexpected end of input stream when parsing operator"); } if (next == '.') { int tmp = in_stream.peek(2); if (tmp > 0 && Character.isDigit(tmp)) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Literal float encountered when operator '.' expected"); } } char cur = (char)in_stream.read(); next = in_stream.peek(); switch (cur) { case '+': switch (next) { case '+': in_stream.read(); return factory.create(TokenType.Increment, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.PlusAssign, index, line, col, 2); } return factory.create(TokenType.Plus, index, line, col); case '-': switch (next) { case '-': in_stream.read(); return factory.create(TokenType.Decrement, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.MinusAssign, index, line, col, 2); } return factory.create(TokenType.Minus, index, line, col); case '*': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.TimesAssign, index, line, col, 2); default: break; } return factory.create(TokenType.Times, index, line, col); case '/': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.DivAssign, index, line, col, 2); } return factory.create(TokenType.Div, index, line, col); case '%': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.ModAssign, index, line, col, 2); } return factory.create(TokenType.Mod, index, line, col); case '&': switch (next) { case '&': in_stream.read(); return factory.create(TokenType.LogicalAnd, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.BitwiseAndAssign, index, line, col, 2); } return factory.create(TokenType.BitwiseAnd, index, line, col); case '|': switch (next) { case '|': in_stream.read(); return factory.create(TokenType.LogicalOr, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.BitwiseOrAssign, index, line, col, 2); } return factory.create(TokenType.BitwiseOr, index, line, col); case '^': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.XorAssign, index, line, col, 2); case '^': in_stream.read(); if (in_stream.peek() == '=') { in_stream.read(); return factory.create(TokenType.PowAssign, index, line, col, 3); } return factory.create(TokenType.Pow, index, line, col, 2); } return factory.create(TokenType.Xor, index, line, col); case '!': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.NotEqual, index, line, col, 2); case '<': in_stream.read(); switch (in_stream.peek()) { case '=': in_stream.read(); return factory.create(TokenType.UnorderedOrGreater, index, line, col, 3); case '>': in_stream.read(); switch (in_stream.peek()) { case '=': in_stream.read(); return factory.create(TokenType.Unordered, index, line, col, 4); } return factory.create(TokenType.UnorderedOrEqual, index, line, col, 3); } return factory.create(TokenType.UnorderedGreaterOrEqual, index, line, col, 2); case '>': in_stream.read(); switch (in_stream.peek()) { case '=': in_stream.read(); return factory.create(TokenType.UnorderedOrLess, index, line, col, 3); default: break; } return factory.create(TokenType.UnorderedLessOrEqual, index, line, col, 2); } return factory.create(TokenType.Not, index, line, col); case '~': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.TildeAssign, index, line, col, 2); } return factory.create(TokenType.Tilde, index, line, col); case '=': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.Equal, index, line, col, 2); case '>': in_stream.read(); return factory.create(TokenType.GoesTo, index, line, col, 2); } return factory.create(TokenType.Assign, index, line, col); case '<': switch (next) { case '<': in_stream.read(); switch (in_stream.peek()) { case '=': in_stream.read(); return factory.create(TokenType.ShiftLeftAssign, index, line, col, 3); default: break; } return factory.create(TokenType.ShiftLeft, index, line, col, 2); case '>': in_stream.read(); switch (in_stream.peek()) { case '=': in_stream.read(); return factory.create(TokenType.LessEqualOrGreater, index, line, col, 3); default: break; } return factory.create(TokenType.LessOrGreater, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.LessEqual, index, line, col, 2); } return factory.create(TokenType.LessThan, index, line, col); case '>': switch (next) { case '>': in_stream.read(); int p = in_stream.peek(); if (p != -1) { switch ((char)p) { case '=': in_stream.read(); return factory.create(TokenType.ShiftRightAssign, index, line, col, 3); case '>': in_stream.read(); int q = in_stream.peek(); if (q != -1 && q == '=') { in_stream.read(); return factory.create(TokenType.TripleRightShiftAssign, index, line, col, 4); } return factory.create(TokenType.ShiftRightUnsigned, index, line, col, 3); } } return factory.create(TokenType.ShiftRight, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.GreaterEqual, index, line, col, 2); } return factory.create(TokenType.GreaterThan, index, line, col); case '?': return factory.create(TokenType.Question, index, line, col); case '$': return factory.create(TokenType.Dollar, index, line, col); case ';': return factory.create(TokenType.Semicolon, index, line, col); case ':': return factory.create(TokenType.Colon, index, line, col); case ',': return factory.create(TokenType.Comma, index, line, col); case '.': if (next == '.') { in_stream.read(); int p = in_stream.peek(); if (p != -1 && p == '.') { in_stream.read(); return factory.create(TokenType.TripleDot, index, line, col, 3); } return factory.create(TokenType.DoubleDot, index, line, col, 2); } return factory.create(TokenType.Dot, index, line, col); case ')': return factory.create(TokenType.CloseParenthesis, index, line, col); case '(': return factory.create(TokenType.OpenParenthesis, index, line, col); case ']': return factory.create(TokenType.CloseSquareBracket, index, line, col); case '[': return factory.create(TokenType.OpenSquareBracket, index, line, col); case '}': return factory.create(TokenType.CloseCurlyBrace, index, line, col); case '{': return factory.create(TokenType.OpenCurlyBrace, index, line, col); case '#': return factory.create(TokenType.Hash, index, line, col); default: return null; } } }
LexOperator { public Token read() throws IOException, LexerException { int index = in_stream.getPosition(); int line = in_stream.getLine(); int col = in_stream.getCol(); int next = in_stream.peek(); if (next == -1) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Unexpected end of input stream when parsing operator"); } if (next == '.') { int tmp = in_stream.peek(2); if (tmp > 0 && Character.isDigit(tmp)) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Literal float encountered when operator '.' expected"); } } char cur = (char)in_stream.read(); next = in_stream.peek(); switch (cur) { case '+': switch (next) { case '+': in_stream.read(); return factory.create(TokenType.Increment, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.PlusAssign, index, line, col, 2); } return factory.create(TokenType.Plus, index, line, col); case '-': switch (next) { case '-': in_stream.read(); return factory.create(TokenType.Decrement, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.MinusAssign, index, line, col, 2); } return factory.create(TokenType.Minus, index, line, col); case '*': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.TimesAssign, index, line, col, 2); default: break; } return factory.create(TokenType.Times, index, line, col); case '/': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.DivAssign, index, line, col, 2); } return factory.create(TokenType.Div, index, line, col); case '%': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.ModAssign, index, line, col, 2); } return factory.create(TokenType.Mod, index, line, col); case '&': switch (next) { case '&': in_stream.read(); return factory.create(TokenType.LogicalAnd, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.BitwiseAndAssign, index, line, col, 2); } return factory.create(TokenType.BitwiseAnd, index, line, col); case '|': switch (next) { case '|': in_stream.read(); return factory.create(TokenType.LogicalOr, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.BitwiseOrAssign, index, line, col, 2); } return factory.create(TokenType.BitwiseOr, index, line, col); case '^': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.XorAssign, index, line, col, 2); case '^': in_stream.read(); if (in_stream.peek() == '=') { in_stream.read(); return factory.create(TokenType.PowAssign, index, line, col, 3); } return factory.create(TokenType.Pow, index, line, col, 2); } return factory.create(TokenType.Xor, index, line, col); case '!': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.NotEqual, index, line, col, 2); case '<': in_stream.read(); switch (in_stream.peek()) { case '=': in_stream.read(); return factory.create(TokenType.UnorderedOrGreater, index, line, col, 3); case '>': in_stream.read(); switch (in_stream.peek()) { case '=': in_stream.read(); return factory.create(TokenType.Unordered, index, line, col, 4); } return factory.create(TokenType.UnorderedOrEqual, index, line, col, 3); } return factory.create(TokenType.UnorderedGreaterOrEqual, index, line, col, 2); case '>': in_stream.read(); switch (in_stream.peek()) { case '=': in_stream.read(); return factory.create(TokenType.UnorderedOrLess, index, line, col, 3); default: break; } return factory.create(TokenType.UnorderedLessOrEqual, index, line, col, 2); } return factory.create(TokenType.Not, index, line, col); case '~': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.TildeAssign, index, line, col, 2); } return factory.create(TokenType.Tilde, index, line, col); case '=': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.Equal, index, line, col, 2); case '>': in_stream.read(); return factory.create(TokenType.GoesTo, index, line, col, 2); } return factory.create(TokenType.Assign, index, line, col); case '<': switch (next) { case '<': in_stream.read(); switch (in_stream.peek()) { case '=': in_stream.read(); return factory.create(TokenType.ShiftLeftAssign, index, line, col, 3); default: break; } return factory.create(TokenType.ShiftLeft, index, line, col, 2); case '>': in_stream.read(); switch (in_stream.peek()) { case '=': in_stream.read(); return factory.create(TokenType.LessEqualOrGreater, index, line, col, 3); default: break; } return factory.create(TokenType.LessOrGreater, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.LessEqual, index, line, col, 2); } return factory.create(TokenType.LessThan, index, line, col); case '>': switch (next) { case '>': in_stream.read(); int p = in_stream.peek(); if (p != -1) { switch ((char)p) { case '=': in_stream.read(); return factory.create(TokenType.ShiftRightAssign, index, line, col, 3); case '>': in_stream.read(); int q = in_stream.peek(); if (q != -1 && q == '=') { in_stream.read(); return factory.create(TokenType.TripleRightShiftAssign, index, line, col, 4); } return factory.create(TokenType.ShiftRightUnsigned, index, line, col, 3); } } return factory.create(TokenType.ShiftRight, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.GreaterEqual, index, line, col, 2); } return factory.create(TokenType.GreaterThan, index, line, col); case '?': return factory.create(TokenType.Question, index, line, col); case '$': return factory.create(TokenType.Dollar, index, line, col); case ';': return factory.create(TokenType.Semicolon, index, line, col); case ':': return factory.create(TokenType.Colon, index, line, col); case ',': return factory.create(TokenType.Comma, index, line, col); case '.': if (next == '.') { in_stream.read(); int p = in_stream.peek(); if (p != -1 && p == '.') { in_stream.read(); return factory.create(TokenType.TripleDot, index, line, col, 3); } return factory.create(TokenType.DoubleDot, index, line, col, 2); } return factory.create(TokenType.Dot, index, line, col); case ')': return factory.create(TokenType.CloseParenthesis, index, line, col); case '(': return factory.create(TokenType.OpenParenthesis, index, line, col); case ']': return factory.create(TokenType.CloseSquareBracket, index, line, col); case '[': return factory.create(TokenType.OpenSquareBracket, index, line, col); case '}': return factory.create(TokenType.CloseCurlyBrace, index, line, col); case '{': return factory.create(TokenType.OpenCurlyBrace, index, line, col); case '#': return factory.create(TokenType.Hash, index, line, col); default: return null; } } LexOperator(TokenFactory factory, LexerStream in_stream); }
LexOperator { public Token read() throws IOException, LexerException { int index = in_stream.getPosition(); int line = in_stream.getLine(); int col = in_stream.getCol(); int next = in_stream.peek(); if (next == -1) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Unexpected end of input stream when parsing operator"); } if (next == '.') { int tmp = in_stream.peek(2); if (tmp > 0 && Character.isDigit(tmp)) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Literal float encountered when operator '.' expected"); } } char cur = (char)in_stream.read(); next = in_stream.peek(); switch (cur) { case '+': switch (next) { case '+': in_stream.read(); return factory.create(TokenType.Increment, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.PlusAssign, index, line, col, 2); } return factory.create(TokenType.Plus, index, line, col); case '-': switch (next) { case '-': in_stream.read(); return factory.create(TokenType.Decrement, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.MinusAssign, index, line, col, 2); } return factory.create(TokenType.Minus, index, line, col); case '*': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.TimesAssign, index, line, col, 2); default: break; } return factory.create(TokenType.Times, index, line, col); case '/': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.DivAssign, index, line, col, 2); } return factory.create(TokenType.Div, index, line, col); case '%': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.ModAssign, index, line, col, 2); } return factory.create(TokenType.Mod, index, line, col); case '&': switch (next) { case '&': in_stream.read(); return factory.create(TokenType.LogicalAnd, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.BitwiseAndAssign, index, line, col, 2); } return factory.create(TokenType.BitwiseAnd, index, line, col); case '|': switch (next) { case '|': in_stream.read(); return factory.create(TokenType.LogicalOr, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.BitwiseOrAssign, index, line, col, 2); } return factory.create(TokenType.BitwiseOr, index, line, col); case '^': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.XorAssign, index, line, col, 2); case '^': in_stream.read(); if (in_stream.peek() == '=') { in_stream.read(); return factory.create(TokenType.PowAssign, index, line, col, 3); } return factory.create(TokenType.Pow, index, line, col, 2); } return factory.create(TokenType.Xor, index, line, col); case '!': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.NotEqual, index, line, col, 2); case '<': in_stream.read(); switch (in_stream.peek()) { case '=': in_stream.read(); return factory.create(TokenType.UnorderedOrGreater, index, line, col, 3); case '>': in_stream.read(); switch (in_stream.peek()) { case '=': in_stream.read(); return factory.create(TokenType.Unordered, index, line, col, 4); } return factory.create(TokenType.UnorderedOrEqual, index, line, col, 3); } return factory.create(TokenType.UnorderedGreaterOrEqual, index, line, col, 2); case '>': in_stream.read(); switch (in_stream.peek()) { case '=': in_stream.read(); return factory.create(TokenType.UnorderedOrLess, index, line, col, 3); default: break; } return factory.create(TokenType.UnorderedLessOrEqual, index, line, col, 2); } return factory.create(TokenType.Not, index, line, col); case '~': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.TildeAssign, index, line, col, 2); } return factory.create(TokenType.Tilde, index, line, col); case '=': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.Equal, index, line, col, 2); case '>': in_stream.read(); return factory.create(TokenType.GoesTo, index, line, col, 2); } return factory.create(TokenType.Assign, index, line, col); case '<': switch (next) { case '<': in_stream.read(); switch (in_stream.peek()) { case '=': in_stream.read(); return factory.create(TokenType.ShiftLeftAssign, index, line, col, 3); default: break; } return factory.create(TokenType.ShiftLeft, index, line, col, 2); case '>': in_stream.read(); switch (in_stream.peek()) { case '=': in_stream.read(); return factory.create(TokenType.LessEqualOrGreater, index, line, col, 3); default: break; } return factory.create(TokenType.LessOrGreater, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.LessEqual, index, line, col, 2); } return factory.create(TokenType.LessThan, index, line, col); case '>': switch (next) { case '>': in_stream.read(); int p = in_stream.peek(); if (p != -1) { switch ((char)p) { case '=': in_stream.read(); return factory.create(TokenType.ShiftRightAssign, index, line, col, 3); case '>': in_stream.read(); int q = in_stream.peek(); if (q != -1 && q == '=') { in_stream.read(); return factory.create(TokenType.TripleRightShiftAssign, index, line, col, 4); } return factory.create(TokenType.ShiftRightUnsigned, index, line, col, 3); } } return factory.create(TokenType.ShiftRight, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.GreaterEqual, index, line, col, 2); } return factory.create(TokenType.GreaterThan, index, line, col); case '?': return factory.create(TokenType.Question, index, line, col); case '$': return factory.create(TokenType.Dollar, index, line, col); case ';': return factory.create(TokenType.Semicolon, index, line, col); case ':': return factory.create(TokenType.Colon, index, line, col); case ',': return factory.create(TokenType.Comma, index, line, col); case '.': if (next == '.') { in_stream.read(); int p = in_stream.peek(); if (p != -1 && p == '.') { in_stream.read(); return factory.create(TokenType.TripleDot, index, line, col, 3); } return factory.create(TokenType.DoubleDot, index, line, col, 2); } return factory.create(TokenType.Dot, index, line, col); case ')': return factory.create(TokenType.CloseParenthesis, index, line, col); case '(': return factory.create(TokenType.OpenParenthesis, index, line, col); case ']': return factory.create(TokenType.CloseSquareBracket, index, line, col); case '[': return factory.create(TokenType.OpenSquareBracket, index, line, col); case '}': return factory.create(TokenType.CloseCurlyBrace, index, line, col); case '{': return factory.create(TokenType.OpenCurlyBrace, index, line, col); case '#': return factory.create(TokenType.Hash, index, line, col); default: return null; } } LexOperator(TokenFactory factory, LexerStream in_stream); Token read(); }
LexOperator { public Token read() throws IOException, LexerException { int index = in_stream.getPosition(); int line = in_stream.getLine(); int col = in_stream.getCol(); int next = in_stream.peek(); if (next == -1) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Unexpected end of input stream when parsing operator"); } if (next == '.') { int tmp = in_stream.peek(2); if (tmp > 0 && Character.isDigit(tmp)) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Literal float encountered when operator '.' expected"); } } char cur = (char)in_stream.read(); next = in_stream.peek(); switch (cur) { case '+': switch (next) { case '+': in_stream.read(); return factory.create(TokenType.Increment, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.PlusAssign, index, line, col, 2); } return factory.create(TokenType.Plus, index, line, col); case '-': switch (next) { case '-': in_stream.read(); return factory.create(TokenType.Decrement, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.MinusAssign, index, line, col, 2); } return factory.create(TokenType.Minus, index, line, col); case '*': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.TimesAssign, index, line, col, 2); default: break; } return factory.create(TokenType.Times, index, line, col); case '/': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.DivAssign, index, line, col, 2); } return factory.create(TokenType.Div, index, line, col); case '%': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.ModAssign, index, line, col, 2); } return factory.create(TokenType.Mod, index, line, col); case '&': switch (next) { case '&': in_stream.read(); return factory.create(TokenType.LogicalAnd, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.BitwiseAndAssign, index, line, col, 2); } return factory.create(TokenType.BitwiseAnd, index, line, col); case '|': switch (next) { case '|': in_stream.read(); return factory.create(TokenType.LogicalOr, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.BitwiseOrAssign, index, line, col, 2); } return factory.create(TokenType.BitwiseOr, index, line, col); case '^': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.XorAssign, index, line, col, 2); case '^': in_stream.read(); if (in_stream.peek() == '=') { in_stream.read(); return factory.create(TokenType.PowAssign, index, line, col, 3); } return factory.create(TokenType.Pow, index, line, col, 2); } return factory.create(TokenType.Xor, index, line, col); case '!': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.NotEqual, index, line, col, 2); case '<': in_stream.read(); switch (in_stream.peek()) { case '=': in_stream.read(); return factory.create(TokenType.UnorderedOrGreater, index, line, col, 3); case '>': in_stream.read(); switch (in_stream.peek()) { case '=': in_stream.read(); return factory.create(TokenType.Unordered, index, line, col, 4); } return factory.create(TokenType.UnorderedOrEqual, index, line, col, 3); } return factory.create(TokenType.UnorderedGreaterOrEqual, index, line, col, 2); case '>': in_stream.read(); switch (in_stream.peek()) { case '=': in_stream.read(); return factory.create(TokenType.UnorderedOrLess, index, line, col, 3); default: break; } return factory.create(TokenType.UnorderedLessOrEqual, index, line, col, 2); } return factory.create(TokenType.Not, index, line, col); case '~': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.TildeAssign, index, line, col, 2); } return factory.create(TokenType.Tilde, index, line, col); case '=': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.Equal, index, line, col, 2); case '>': in_stream.read(); return factory.create(TokenType.GoesTo, index, line, col, 2); } return factory.create(TokenType.Assign, index, line, col); case '<': switch (next) { case '<': in_stream.read(); switch (in_stream.peek()) { case '=': in_stream.read(); return factory.create(TokenType.ShiftLeftAssign, index, line, col, 3); default: break; } return factory.create(TokenType.ShiftLeft, index, line, col, 2); case '>': in_stream.read(); switch (in_stream.peek()) { case '=': in_stream.read(); return factory.create(TokenType.LessEqualOrGreater, index, line, col, 3); default: break; } return factory.create(TokenType.LessOrGreater, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.LessEqual, index, line, col, 2); } return factory.create(TokenType.LessThan, index, line, col); case '>': switch (next) { case '>': in_stream.read(); int p = in_stream.peek(); if (p != -1) { switch ((char)p) { case '=': in_stream.read(); return factory.create(TokenType.ShiftRightAssign, index, line, col, 3); case '>': in_stream.read(); int q = in_stream.peek(); if (q != -1 && q == '=') { in_stream.read(); return factory.create(TokenType.TripleRightShiftAssign, index, line, col, 4); } return factory.create(TokenType.ShiftRightUnsigned, index, line, col, 3); } } return factory.create(TokenType.ShiftRight, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.GreaterEqual, index, line, col, 2); } return factory.create(TokenType.GreaterThan, index, line, col); case '?': return factory.create(TokenType.Question, index, line, col); case '$': return factory.create(TokenType.Dollar, index, line, col); case ';': return factory.create(TokenType.Semicolon, index, line, col); case ':': return factory.create(TokenType.Colon, index, line, col); case ',': return factory.create(TokenType.Comma, index, line, col); case '.': if (next == '.') { in_stream.read(); int p = in_stream.peek(); if (p != -1 && p == '.') { in_stream.read(); return factory.create(TokenType.TripleDot, index, line, col, 3); } return factory.create(TokenType.DoubleDot, index, line, col, 2); } return factory.create(TokenType.Dot, index, line, col); case ')': return factory.create(TokenType.CloseParenthesis, index, line, col); case '(': return factory.create(TokenType.OpenParenthesis, index, line, col); case ']': return factory.create(TokenType.CloseSquareBracket, index, line, col); case '[': return factory.create(TokenType.OpenSquareBracket, index, line, col); case '}': return factory.create(TokenType.CloseCurlyBrace, index, line, col); case '{': return factory.create(TokenType.OpenCurlyBrace, index, line, col); case '#': return factory.create(TokenType.Hash, index, line, col); default: return null; } } LexOperator(TokenFactory factory, LexerStream in_stream); Token read(); }
@Test public void pow() throws Exception { String str = "a^^=b^^c;"; LexerStream ls = new LexerStream(new StringReader(str)); LexOperator lex = new LexOperator(new BaseTokenFactory(), ls); ls.read(); Token tok = lex.read(); assertEquals(TokenType.PowAssign, tok.getType()); assertEquals(1, tok.getLine()); assertEquals(2, tok.getCol()); assertEquals(1, tok.getEndLine()); assertEquals(5, tok.getEndCol()); ls.read(); tok = lex.read(); assertEquals(TokenType.Pow, tok.getType()); assertEquals(1, tok.getLine()); assertEquals(6, tok.getCol()); assertEquals(1, tok.getEndLine()); assertEquals(8, tok.getEndCol()); ls.read(); tok = lex.read(); assertEquals(TokenType.Semicolon, tok.getType()); }
public Token read() throws IOException, LexerException { int index = in_stream.getPosition(); int line = in_stream.getLine(); int col = in_stream.getCol(); int next = in_stream.peek(); if (next == -1) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Unexpected end of input stream when parsing operator"); } if (next == '.') { int tmp = in_stream.peek(2); if (tmp > 0 && Character.isDigit(tmp)) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Literal float encountered when operator '.' expected"); } } char cur = (char)in_stream.read(); next = in_stream.peek(); switch (cur) { case '+': switch (next) { case '+': in_stream.read(); return factory.create(TokenType.Increment, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.PlusAssign, index, line, col, 2); } return factory.create(TokenType.Plus, index, line, col); case '-': switch (next) { case '-': in_stream.read(); return factory.create(TokenType.Decrement, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.MinusAssign, index, line, col, 2); } return factory.create(TokenType.Minus, index, line, col); case '*': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.TimesAssign, index, line, col, 2); default: break; } return factory.create(TokenType.Times, index, line, col); case '/': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.DivAssign, index, line, col, 2); } return factory.create(TokenType.Div, index, line, col); case '%': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.ModAssign, index, line, col, 2); } return factory.create(TokenType.Mod, index, line, col); case '&': switch (next) { case '&': in_stream.read(); return factory.create(TokenType.LogicalAnd, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.BitwiseAndAssign, index, line, col, 2); } return factory.create(TokenType.BitwiseAnd, index, line, col); case '|': switch (next) { case '|': in_stream.read(); return factory.create(TokenType.LogicalOr, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.BitwiseOrAssign, index, line, col, 2); } return factory.create(TokenType.BitwiseOr, index, line, col); case '^': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.XorAssign, index, line, col, 2); case '^': in_stream.read(); if (in_stream.peek() == '=') { in_stream.read(); return factory.create(TokenType.PowAssign, index, line, col, 3); } return factory.create(TokenType.Pow, index, line, col, 2); } return factory.create(TokenType.Xor, index, line, col); case '!': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.NotEqual, index, line, col, 2); case '<': in_stream.read(); switch (in_stream.peek()) { case '=': in_stream.read(); return factory.create(TokenType.UnorderedOrGreater, index, line, col, 3); case '>': in_stream.read(); switch (in_stream.peek()) { case '=': in_stream.read(); return factory.create(TokenType.Unordered, index, line, col, 4); } return factory.create(TokenType.UnorderedOrEqual, index, line, col, 3); } return factory.create(TokenType.UnorderedGreaterOrEqual, index, line, col, 2); case '>': in_stream.read(); switch (in_stream.peek()) { case '=': in_stream.read(); return factory.create(TokenType.UnorderedOrLess, index, line, col, 3); default: break; } return factory.create(TokenType.UnorderedLessOrEqual, index, line, col, 2); } return factory.create(TokenType.Not, index, line, col); case '~': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.TildeAssign, index, line, col, 2); } return factory.create(TokenType.Tilde, index, line, col); case '=': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.Equal, index, line, col, 2); case '>': in_stream.read(); return factory.create(TokenType.GoesTo, index, line, col, 2); } return factory.create(TokenType.Assign, index, line, col); case '<': switch (next) { case '<': in_stream.read(); switch (in_stream.peek()) { case '=': in_stream.read(); return factory.create(TokenType.ShiftLeftAssign, index, line, col, 3); default: break; } return factory.create(TokenType.ShiftLeft, index, line, col, 2); case '>': in_stream.read(); switch (in_stream.peek()) { case '=': in_stream.read(); return factory.create(TokenType.LessEqualOrGreater, index, line, col, 3); default: break; } return factory.create(TokenType.LessOrGreater, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.LessEqual, index, line, col, 2); } return factory.create(TokenType.LessThan, index, line, col); case '>': switch (next) { case '>': in_stream.read(); int p = in_stream.peek(); if (p != -1) { switch ((char)p) { case '=': in_stream.read(); return factory.create(TokenType.ShiftRightAssign, index, line, col, 3); case '>': in_stream.read(); int q = in_stream.peek(); if (q != -1 && q == '=') { in_stream.read(); return factory.create(TokenType.TripleRightShiftAssign, index, line, col, 4); } return factory.create(TokenType.ShiftRightUnsigned, index, line, col, 3); } } return factory.create(TokenType.ShiftRight, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.GreaterEqual, index, line, col, 2); } return factory.create(TokenType.GreaterThan, index, line, col); case '?': return factory.create(TokenType.Question, index, line, col); case '$': return factory.create(TokenType.Dollar, index, line, col); case ';': return factory.create(TokenType.Semicolon, index, line, col); case ':': return factory.create(TokenType.Colon, index, line, col); case ',': return factory.create(TokenType.Comma, index, line, col); case '.': if (next == '.') { in_stream.read(); int p = in_stream.peek(); if (p != -1 && p == '.') { in_stream.read(); return factory.create(TokenType.TripleDot, index, line, col, 3); } return factory.create(TokenType.DoubleDot, index, line, col, 2); } return factory.create(TokenType.Dot, index, line, col); case ')': return factory.create(TokenType.CloseParenthesis, index, line, col); case '(': return factory.create(TokenType.OpenParenthesis, index, line, col); case ']': return factory.create(TokenType.CloseSquareBracket, index, line, col); case '[': return factory.create(TokenType.OpenSquareBracket, index, line, col); case '}': return factory.create(TokenType.CloseCurlyBrace, index, line, col); case '{': return factory.create(TokenType.OpenCurlyBrace, index, line, col); case '#': return factory.create(TokenType.Hash, index, line, col); default: return null; } }
LexOperator { public Token read() throws IOException, LexerException { int index = in_stream.getPosition(); int line = in_stream.getLine(); int col = in_stream.getCol(); int next = in_stream.peek(); if (next == -1) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Unexpected end of input stream when parsing operator"); } if (next == '.') { int tmp = in_stream.peek(2); if (tmp > 0 && Character.isDigit(tmp)) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Literal float encountered when operator '.' expected"); } } char cur = (char)in_stream.read(); next = in_stream.peek(); switch (cur) { case '+': switch (next) { case '+': in_stream.read(); return factory.create(TokenType.Increment, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.PlusAssign, index, line, col, 2); } return factory.create(TokenType.Plus, index, line, col); case '-': switch (next) { case '-': in_stream.read(); return factory.create(TokenType.Decrement, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.MinusAssign, index, line, col, 2); } return factory.create(TokenType.Minus, index, line, col); case '*': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.TimesAssign, index, line, col, 2); default: break; } return factory.create(TokenType.Times, index, line, col); case '/': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.DivAssign, index, line, col, 2); } return factory.create(TokenType.Div, index, line, col); case '%': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.ModAssign, index, line, col, 2); } return factory.create(TokenType.Mod, index, line, col); case '&': switch (next) { case '&': in_stream.read(); return factory.create(TokenType.LogicalAnd, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.BitwiseAndAssign, index, line, col, 2); } return factory.create(TokenType.BitwiseAnd, index, line, col); case '|': switch (next) { case '|': in_stream.read(); return factory.create(TokenType.LogicalOr, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.BitwiseOrAssign, index, line, col, 2); } return factory.create(TokenType.BitwiseOr, index, line, col); case '^': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.XorAssign, index, line, col, 2); case '^': in_stream.read(); if (in_stream.peek() == '=') { in_stream.read(); return factory.create(TokenType.PowAssign, index, line, col, 3); } return factory.create(TokenType.Pow, index, line, col, 2); } return factory.create(TokenType.Xor, index, line, col); case '!': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.NotEqual, index, line, col, 2); case '<': in_stream.read(); switch (in_stream.peek()) { case '=': in_stream.read(); return factory.create(TokenType.UnorderedOrGreater, index, line, col, 3); case '>': in_stream.read(); switch (in_stream.peek()) { case '=': in_stream.read(); return factory.create(TokenType.Unordered, index, line, col, 4); } return factory.create(TokenType.UnorderedOrEqual, index, line, col, 3); } return factory.create(TokenType.UnorderedGreaterOrEqual, index, line, col, 2); case '>': in_stream.read(); switch (in_stream.peek()) { case '=': in_stream.read(); return factory.create(TokenType.UnorderedOrLess, index, line, col, 3); default: break; } return factory.create(TokenType.UnorderedLessOrEqual, index, line, col, 2); } return factory.create(TokenType.Not, index, line, col); case '~': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.TildeAssign, index, line, col, 2); } return factory.create(TokenType.Tilde, index, line, col); case '=': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.Equal, index, line, col, 2); case '>': in_stream.read(); return factory.create(TokenType.GoesTo, index, line, col, 2); } return factory.create(TokenType.Assign, index, line, col); case '<': switch (next) { case '<': in_stream.read(); switch (in_stream.peek()) { case '=': in_stream.read(); return factory.create(TokenType.ShiftLeftAssign, index, line, col, 3); default: break; } return factory.create(TokenType.ShiftLeft, index, line, col, 2); case '>': in_stream.read(); switch (in_stream.peek()) { case '=': in_stream.read(); return factory.create(TokenType.LessEqualOrGreater, index, line, col, 3); default: break; } return factory.create(TokenType.LessOrGreater, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.LessEqual, index, line, col, 2); } return factory.create(TokenType.LessThan, index, line, col); case '>': switch (next) { case '>': in_stream.read(); int p = in_stream.peek(); if (p != -1) { switch ((char)p) { case '=': in_stream.read(); return factory.create(TokenType.ShiftRightAssign, index, line, col, 3); case '>': in_stream.read(); int q = in_stream.peek(); if (q != -1 && q == '=') { in_stream.read(); return factory.create(TokenType.TripleRightShiftAssign, index, line, col, 4); } return factory.create(TokenType.ShiftRightUnsigned, index, line, col, 3); } } return factory.create(TokenType.ShiftRight, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.GreaterEqual, index, line, col, 2); } return factory.create(TokenType.GreaterThan, index, line, col); case '?': return factory.create(TokenType.Question, index, line, col); case '$': return factory.create(TokenType.Dollar, index, line, col); case ';': return factory.create(TokenType.Semicolon, index, line, col); case ':': return factory.create(TokenType.Colon, index, line, col); case ',': return factory.create(TokenType.Comma, index, line, col); case '.': if (next == '.') { in_stream.read(); int p = in_stream.peek(); if (p != -1 && p == '.') { in_stream.read(); return factory.create(TokenType.TripleDot, index, line, col, 3); } return factory.create(TokenType.DoubleDot, index, line, col, 2); } return factory.create(TokenType.Dot, index, line, col); case ')': return factory.create(TokenType.CloseParenthesis, index, line, col); case '(': return factory.create(TokenType.OpenParenthesis, index, line, col); case ']': return factory.create(TokenType.CloseSquareBracket, index, line, col); case '[': return factory.create(TokenType.OpenSquareBracket, index, line, col); case '}': return factory.create(TokenType.CloseCurlyBrace, index, line, col); case '{': return factory.create(TokenType.OpenCurlyBrace, index, line, col); case '#': return factory.create(TokenType.Hash, index, line, col); default: return null; } } }
LexOperator { public Token read() throws IOException, LexerException { int index = in_stream.getPosition(); int line = in_stream.getLine(); int col = in_stream.getCol(); int next = in_stream.peek(); if (next == -1) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Unexpected end of input stream when parsing operator"); } if (next == '.') { int tmp = in_stream.peek(2); if (tmp > 0 && Character.isDigit(tmp)) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Literal float encountered when operator '.' expected"); } } char cur = (char)in_stream.read(); next = in_stream.peek(); switch (cur) { case '+': switch (next) { case '+': in_stream.read(); return factory.create(TokenType.Increment, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.PlusAssign, index, line, col, 2); } return factory.create(TokenType.Plus, index, line, col); case '-': switch (next) { case '-': in_stream.read(); return factory.create(TokenType.Decrement, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.MinusAssign, index, line, col, 2); } return factory.create(TokenType.Minus, index, line, col); case '*': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.TimesAssign, index, line, col, 2); default: break; } return factory.create(TokenType.Times, index, line, col); case '/': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.DivAssign, index, line, col, 2); } return factory.create(TokenType.Div, index, line, col); case '%': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.ModAssign, index, line, col, 2); } return factory.create(TokenType.Mod, index, line, col); case '&': switch (next) { case '&': in_stream.read(); return factory.create(TokenType.LogicalAnd, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.BitwiseAndAssign, index, line, col, 2); } return factory.create(TokenType.BitwiseAnd, index, line, col); case '|': switch (next) { case '|': in_stream.read(); return factory.create(TokenType.LogicalOr, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.BitwiseOrAssign, index, line, col, 2); } return factory.create(TokenType.BitwiseOr, index, line, col); case '^': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.XorAssign, index, line, col, 2); case '^': in_stream.read(); if (in_stream.peek() == '=') { in_stream.read(); return factory.create(TokenType.PowAssign, index, line, col, 3); } return factory.create(TokenType.Pow, index, line, col, 2); } return factory.create(TokenType.Xor, index, line, col); case '!': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.NotEqual, index, line, col, 2); case '<': in_stream.read(); switch (in_stream.peek()) { case '=': in_stream.read(); return factory.create(TokenType.UnorderedOrGreater, index, line, col, 3); case '>': in_stream.read(); switch (in_stream.peek()) { case '=': in_stream.read(); return factory.create(TokenType.Unordered, index, line, col, 4); } return factory.create(TokenType.UnorderedOrEqual, index, line, col, 3); } return factory.create(TokenType.UnorderedGreaterOrEqual, index, line, col, 2); case '>': in_stream.read(); switch (in_stream.peek()) { case '=': in_stream.read(); return factory.create(TokenType.UnorderedOrLess, index, line, col, 3); default: break; } return factory.create(TokenType.UnorderedLessOrEqual, index, line, col, 2); } return factory.create(TokenType.Not, index, line, col); case '~': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.TildeAssign, index, line, col, 2); } return factory.create(TokenType.Tilde, index, line, col); case '=': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.Equal, index, line, col, 2); case '>': in_stream.read(); return factory.create(TokenType.GoesTo, index, line, col, 2); } return factory.create(TokenType.Assign, index, line, col); case '<': switch (next) { case '<': in_stream.read(); switch (in_stream.peek()) { case '=': in_stream.read(); return factory.create(TokenType.ShiftLeftAssign, index, line, col, 3); default: break; } return factory.create(TokenType.ShiftLeft, index, line, col, 2); case '>': in_stream.read(); switch (in_stream.peek()) { case '=': in_stream.read(); return factory.create(TokenType.LessEqualOrGreater, index, line, col, 3); default: break; } return factory.create(TokenType.LessOrGreater, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.LessEqual, index, line, col, 2); } return factory.create(TokenType.LessThan, index, line, col); case '>': switch (next) { case '>': in_stream.read(); int p = in_stream.peek(); if (p != -1) { switch ((char)p) { case '=': in_stream.read(); return factory.create(TokenType.ShiftRightAssign, index, line, col, 3); case '>': in_stream.read(); int q = in_stream.peek(); if (q != -1 && q == '=') { in_stream.read(); return factory.create(TokenType.TripleRightShiftAssign, index, line, col, 4); } return factory.create(TokenType.ShiftRightUnsigned, index, line, col, 3); } } return factory.create(TokenType.ShiftRight, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.GreaterEqual, index, line, col, 2); } return factory.create(TokenType.GreaterThan, index, line, col); case '?': return factory.create(TokenType.Question, index, line, col); case '$': return factory.create(TokenType.Dollar, index, line, col); case ';': return factory.create(TokenType.Semicolon, index, line, col); case ':': return factory.create(TokenType.Colon, index, line, col); case ',': return factory.create(TokenType.Comma, index, line, col); case '.': if (next == '.') { in_stream.read(); int p = in_stream.peek(); if (p != -1 && p == '.') { in_stream.read(); return factory.create(TokenType.TripleDot, index, line, col, 3); } return factory.create(TokenType.DoubleDot, index, line, col, 2); } return factory.create(TokenType.Dot, index, line, col); case ')': return factory.create(TokenType.CloseParenthesis, index, line, col); case '(': return factory.create(TokenType.OpenParenthesis, index, line, col); case ']': return factory.create(TokenType.CloseSquareBracket, index, line, col); case '[': return factory.create(TokenType.OpenSquareBracket, index, line, col); case '}': return factory.create(TokenType.CloseCurlyBrace, index, line, col); case '{': return factory.create(TokenType.OpenCurlyBrace, index, line, col); case '#': return factory.create(TokenType.Hash, index, line, col); default: return null; } } LexOperator(TokenFactory factory, LexerStream in_stream); }
LexOperator { public Token read() throws IOException, LexerException { int index = in_stream.getPosition(); int line = in_stream.getLine(); int col = in_stream.getCol(); int next = in_stream.peek(); if (next == -1) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Unexpected end of input stream when parsing operator"); } if (next == '.') { int tmp = in_stream.peek(2); if (tmp > 0 && Character.isDigit(tmp)) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Literal float encountered when operator '.' expected"); } } char cur = (char)in_stream.read(); next = in_stream.peek(); switch (cur) { case '+': switch (next) { case '+': in_stream.read(); return factory.create(TokenType.Increment, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.PlusAssign, index, line, col, 2); } return factory.create(TokenType.Plus, index, line, col); case '-': switch (next) { case '-': in_stream.read(); return factory.create(TokenType.Decrement, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.MinusAssign, index, line, col, 2); } return factory.create(TokenType.Minus, index, line, col); case '*': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.TimesAssign, index, line, col, 2); default: break; } return factory.create(TokenType.Times, index, line, col); case '/': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.DivAssign, index, line, col, 2); } return factory.create(TokenType.Div, index, line, col); case '%': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.ModAssign, index, line, col, 2); } return factory.create(TokenType.Mod, index, line, col); case '&': switch (next) { case '&': in_stream.read(); return factory.create(TokenType.LogicalAnd, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.BitwiseAndAssign, index, line, col, 2); } return factory.create(TokenType.BitwiseAnd, index, line, col); case '|': switch (next) { case '|': in_stream.read(); return factory.create(TokenType.LogicalOr, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.BitwiseOrAssign, index, line, col, 2); } return factory.create(TokenType.BitwiseOr, index, line, col); case '^': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.XorAssign, index, line, col, 2); case '^': in_stream.read(); if (in_stream.peek() == '=') { in_stream.read(); return factory.create(TokenType.PowAssign, index, line, col, 3); } return factory.create(TokenType.Pow, index, line, col, 2); } return factory.create(TokenType.Xor, index, line, col); case '!': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.NotEqual, index, line, col, 2); case '<': in_stream.read(); switch (in_stream.peek()) { case '=': in_stream.read(); return factory.create(TokenType.UnorderedOrGreater, index, line, col, 3); case '>': in_stream.read(); switch (in_stream.peek()) { case '=': in_stream.read(); return factory.create(TokenType.Unordered, index, line, col, 4); } return factory.create(TokenType.UnorderedOrEqual, index, line, col, 3); } return factory.create(TokenType.UnorderedGreaterOrEqual, index, line, col, 2); case '>': in_stream.read(); switch (in_stream.peek()) { case '=': in_stream.read(); return factory.create(TokenType.UnorderedOrLess, index, line, col, 3); default: break; } return factory.create(TokenType.UnorderedLessOrEqual, index, line, col, 2); } return factory.create(TokenType.Not, index, line, col); case '~': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.TildeAssign, index, line, col, 2); } return factory.create(TokenType.Tilde, index, line, col); case '=': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.Equal, index, line, col, 2); case '>': in_stream.read(); return factory.create(TokenType.GoesTo, index, line, col, 2); } return factory.create(TokenType.Assign, index, line, col); case '<': switch (next) { case '<': in_stream.read(); switch (in_stream.peek()) { case '=': in_stream.read(); return factory.create(TokenType.ShiftLeftAssign, index, line, col, 3); default: break; } return factory.create(TokenType.ShiftLeft, index, line, col, 2); case '>': in_stream.read(); switch (in_stream.peek()) { case '=': in_stream.read(); return factory.create(TokenType.LessEqualOrGreater, index, line, col, 3); default: break; } return factory.create(TokenType.LessOrGreater, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.LessEqual, index, line, col, 2); } return factory.create(TokenType.LessThan, index, line, col); case '>': switch (next) { case '>': in_stream.read(); int p = in_stream.peek(); if (p != -1) { switch ((char)p) { case '=': in_stream.read(); return factory.create(TokenType.ShiftRightAssign, index, line, col, 3); case '>': in_stream.read(); int q = in_stream.peek(); if (q != -1 && q == '=') { in_stream.read(); return factory.create(TokenType.TripleRightShiftAssign, index, line, col, 4); } return factory.create(TokenType.ShiftRightUnsigned, index, line, col, 3); } } return factory.create(TokenType.ShiftRight, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.GreaterEqual, index, line, col, 2); } return factory.create(TokenType.GreaterThan, index, line, col); case '?': return factory.create(TokenType.Question, index, line, col); case '$': return factory.create(TokenType.Dollar, index, line, col); case ';': return factory.create(TokenType.Semicolon, index, line, col); case ':': return factory.create(TokenType.Colon, index, line, col); case ',': return factory.create(TokenType.Comma, index, line, col); case '.': if (next == '.') { in_stream.read(); int p = in_stream.peek(); if (p != -1 && p == '.') { in_stream.read(); return factory.create(TokenType.TripleDot, index, line, col, 3); } return factory.create(TokenType.DoubleDot, index, line, col, 2); } return factory.create(TokenType.Dot, index, line, col); case ')': return factory.create(TokenType.CloseParenthesis, index, line, col); case '(': return factory.create(TokenType.OpenParenthesis, index, line, col); case ']': return factory.create(TokenType.CloseSquareBracket, index, line, col); case '[': return factory.create(TokenType.OpenSquareBracket, index, line, col); case '}': return factory.create(TokenType.CloseCurlyBrace, index, line, col); case '{': return factory.create(TokenType.OpenCurlyBrace, index, line, col); case '#': return factory.create(TokenType.Hash, index, line, col); default: return null; } } LexOperator(TokenFactory factory, LexerStream in_stream); Token read(); }
LexOperator { public Token read() throws IOException, LexerException { int index = in_stream.getPosition(); int line = in_stream.getLine(); int col = in_stream.getCol(); int next = in_stream.peek(); if (next == -1) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Unexpected end of input stream when parsing operator"); } if (next == '.') { int tmp = in_stream.peek(2); if (tmp > 0 && Character.isDigit(tmp)) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Literal float encountered when operator '.' expected"); } } char cur = (char)in_stream.read(); next = in_stream.peek(); switch (cur) { case '+': switch (next) { case '+': in_stream.read(); return factory.create(TokenType.Increment, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.PlusAssign, index, line, col, 2); } return factory.create(TokenType.Plus, index, line, col); case '-': switch (next) { case '-': in_stream.read(); return factory.create(TokenType.Decrement, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.MinusAssign, index, line, col, 2); } return factory.create(TokenType.Minus, index, line, col); case '*': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.TimesAssign, index, line, col, 2); default: break; } return factory.create(TokenType.Times, index, line, col); case '/': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.DivAssign, index, line, col, 2); } return factory.create(TokenType.Div, index, line, col); case '%': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.ModAssign, index, line, col, 2); } return factory.create(TokenType.Mod, index, line, col); case '&': switch (next) { case '&': in_stream.read(); return factory.create(TokenType.LogicalAnd, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.BitwiseAndAssign, index, line, col, 2); } return factory.create(TokenType.BitwiseAnd, index, line, col); case '|': switch (next) { case '|': in_stream.read(); return factory.create(TokenType.LogicalOr, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.BitwiseOrAssign, index, line, col, 2); } return factory.create(TokenType.BitwiseOr, index, line, col); case '^': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.XorAssign, index, line, col, 2); case '^': in_stream.read(); if (in_stream.peek() == '=') { in_stream.read(); return factory.create(TokenType.PowAssign, index, line, col, 3); } return factory.create(TokenType.Pow, index, line, col, 2); } return factory.create(TokenType.Xor, index, line, col); case '!': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.NotEqual, index, line, col, 2); case '<': in_stream.read(); switch (in_stream.peek()) { case '=': in_stream.read(); return factory.create(TokenType.UnorderedOrGreater, index, line, col, 3); case '>': in_stream.read(); switch (in_stream.peek()) { case '=': in_stream.read(); return factory.create(TokenType.Unordered, index, line, col, 4); } return factory.create(TokenType.UnorderedOrEqual, index, line, col, 3); } return factory.create(TokenType.UnorderedGreaterOrEqual, index, line, col, 2); case '>': in_stream.read(); switch (in_stream.peek()) { case '=': in_stream.read(); return factory.create(TokenType.UnorderedOrLess, index, line, col, 3); default: break; } return factory.create(TokenType.UnorderedLessOrEqual, index, line, col, 2); } return factory.create(TokenType.Not, index, line, col); case '~': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.TildeAssign, index, line, col, 2); } return factory.create(TokenType.Tilde, index, line, col); case '=': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.Equal, index, line, col, 2); case '>': in_stream.read(); return factory.create(TokenType.GoesTo, index, line, col, 2); } return factory.create(TokenType.Assign, index, line, col); case '<': switch (next) { case '<': in_stream.read(); switch (in_stream.peek()) { case '=': in_stream.read(); return factory.create(TokenType.ShiftLeftAssign, index, line, col, 3); default: break; } return factory.create(TokenType.ShiftLeft, index, line, col, 2); case '>': in_stream.read(); switch (in_stream.peek()) { case '=': in_stream.read(); return factory.create(TokenType.LessEqualOrGreater, index, line, col, 3); default: break; } return factory.create(TokenType.LessOrGreater, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.LessEqual, index, line, col, 2); } return factory.create(TokenType.LessThan, index, line, col); case '>': switch (next) { case '>': in_stream.read(); int p = in_stream.peek(); if (p != -1) { switch ((char)p) { case '=': in_stream.read(); return factory.create(TokenType.ShiftRightAssign, index, line, col, 3); case '>': in_stream.read(); int q = in_stream.peek(); if (q != -1 && q == '=') { in_stream.read(); return factory.create(TokenType.TripleRightShiftAssign, index, line, col, 4); } return factory.create(TokenType.ShiftRightUnsigned, index, line, col, 3); } } return factory.create(TokenType.ShiftRight, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.GreaterEqual, index, line, col, 2); } return factory.create(TokenType.GreaterThan, index, line, col); case '?': return factory.create(TokenType.Question, index, line, col); case '$': return factory.create(TokenType.Dollar, index, line, col); case ';': return factory.create(TokenType.Semicolon, index, line, col); case ':': return factory.create(TokenType.Colon, index, line, col); case ',': return factory.create(TokenType.Comma, index, line, col); case '.': if (next == '.') { in_stream.read(); int p = in_stream.peek(); if (p != -1 && p == '.') { in_stream.read(); return factory.create(TokenType.TripleDot, index, line, col, 3); } return factory.create(TokenType.DoubleDot, index, line, col, 2); } return factory.create(TokenType.Dot, index, line, col); case ')': return factory.create(TokenType.CloseParenthesis, index, line, col); case '(': return factory.create(TokenType.OpenParenthesis, index, line, col); case ']': return factory.create(TokenType.CloseSquareBracket, index, line, col); case '[': return factory.create(TokenType.OpenSquareBracket, index, line, col); case '}': return factory.create(TokenType.CloseCurlyBrace, index, line, col); case '{': return factory.create(TokenType.OpenCurlyBrace, index, line, col); case '#': return factory.create(TokenType.Hash, index, line, col); default: return null; } } LexOperator(TokenFactory factory, LexerStream in_stream); Token read(); }
@Test public void unordered() throws Exception { String str = "a !< b !<> c !<>= d !<= e !> f !>= g"; LexerStream ls = new LexerStream(new StringReader(str)); LexOperator lex = new LexOperator(new BaseTokenFactory(), ls); ls.read(); ls.read(); Token tok = lex.read(); assertEquals(TokenType.UnorderedGreaterOrEqual, tok.getType()); assertTrue(tok.getLine() == 1 && tok.getEndLine() == 1); assertEquals(3, tok.getCol()); assertEquals(5, tok.getEndCol()); ls.read(); ls.read(); ls.read(); tok = lex.read(); assertEquals(TokenType.UnorderedOrEqual, tok.getType()); assertTrue(tok.getLine() == 1 && tok.getEndLine() == 1); assertEquals(8, tok.getCol()); assertEquals(11, tok.getEndCol()); ls.read(); ls.read(); ls.read(); tok = lex.read(); assertEquals(TokenType.Unordered, tok.getType()); assertTrue(tok.getLine() == 1 && tok.getEndLine() == 1); assertEquals(14, tok.getCol()); assertEquals(18, tok.getEndCol()); ls.read(); ls.read(); ls.read(); tok = lex.read(); assertEquals(TokenType.UnorderedOrGreater, tok.getType()); assertTrue(tok.getLine() == 1 && tok.getEndLine() == 1); assertEquals(21, tok.getCol()); assertEquals(24, tok.getEndCol()); ls.read(); ls.read(); ls.read(); tok = lex.read(); assertEquals(TokenType.UnorderedLessOrEqual, tok.getType()); assertTrue(tok.getLine() == 1 && tok.getEndLine() == 1); assertEquals(27, tok.getCol()); assertEquals(29, tok.getEndCol()); ls.read(); ls.read(); ls.read(); tok = lex.read(); assertEquals(TokenType.UnorderedOrLess, tok.getType()); assertTrue(tok.getLine() == 1 && tok.getEndLine() == 1); assertEquals(32, tok.getCol()); assertEquals(35, tok.getEndCol()); }
public Token read() throws IOException, LexerException { int index = in_stream.getPosition(); int line = in_stream.getLine(); int col = in_stream.getCol(); int next = in_stream.peek(); if (next == -1) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Unexpected end of input stream when parsing operator"); } if (next == '.') { int tmp = in_stream.peek(2); if (tmp > 0 && Character.isDigit(tmp)) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Literal float encountered when operator '.' expected"); } } char cur = (char)in_stream.read(); next = in_stream.peek(); switch (cur) { case '+': switch (next) { case '+': in_stream.read(); return factory.create(TokenType.Increment, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.PlusAssign, index, line, col, 2); } return factory.create(TokenType.Plus, index, line, col); case '-': switch (next) { case '-': in_stream.read(); return factory.create(TokenType.Decrement, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.MinusAssign, index, line, col, 2); } return factory.create(TokenType.Minus, index, line, col); case '*': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.TimesAssign, index, line, col, 2); default: break; } return factory.create(TokenType.Times, index, line, col); case '/': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.DivAssign, index, line, col, 2); } return factory.create(TokenType.Div, index, line, col); case '%': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.ModAssign, index, line, col, 2); } return factory.create(TokenType.Mod, index, line, col); case '&': switch (next) { case '&': in_stream.read(); return factory.create(TokenType.LogicalAnd, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.BitwiseAndAssign, index, line, col, 2); } return factory.create(TokenType.BitwiseAnd, index, line, col); case '|': switch (next) { case '|': in_stream.read(); return factory.create(TokenType.LogicalOr, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.BitwiseOrAssign, index, line, col, 2); } return factory.create(TokenType.BitwiseOr, index, line, col); case '^': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.XorAssign, index, line, col, 2); case '^': in_stream.read(); if (in_stream.peek() == '=') { in_stream.read(); return factory.create(TokenType.PowAssign, index, line, col, 3); } return factory.create(TokenType.Pow, index, line, col, 2); } return factory.create(TokenType.Xor, index, line, col); case '!': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.NotEqual, index, line, col, 2); case '<': in_stream.read(); switch (in_stream.peek()) { case '=': in_stream.read(); return factory.create(TokenType.UnorderedOrGreater, index, line, col, 3); case '>': in_stream.read(); switch (in_stream.peek()) { case '=': in_stream.read(); return factory.create(TokenType.Unordered, index, line, col, 4); } return factory.create(TokenType.UnorderedOrEqual, index, line, col, 3); } return factory.create(TokenType.UnorderedGreaterOrEqual, index, line, col, 2); case '>': in_stream.read(); switch (in_stream.peek()) { case '=': in_stream.read(); return factory.create(TokenType.UnorderedOrLess, index, line, col, 3); default: break; } return factory.create(TokenType.UnorderedLessOrEqual, index, line, col, 2); } return factory.create(TokenType.Not, index, line, col); case '~': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.TildeAssign, index, line, col, 2); } return factory.create(TokenType.Tilde, index, line, col); case '=': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.Equal, index, line, col, 2); case '>': in_stream.read(); return factory.create(TokenType.GoesTo, index, line, col, 2); } return factory.create(TokenType.Assign, index, line, col); case '<': switch (next) { case '<': in_stream.read(); switch (in_stream.peek()) { case '=': in_stream.read(); return factory.create(TokenType.ShiftLeftAssign, index, line, col, 3); default: break; } return factory.create(TokenType.ShiftLeft, index, line, col, 2); case '>': in_stream.read(); switch (in_stream.peek()) { case '=': in_stream.read(); return factory.create(TokenType.LessEqualOrGreater, index, line, col, 3); default: break; } return factory.create(TokenType.LessOrGreater, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.LessEqual, index, line, col, 2); } return factory.create(TokenType.LessThan, index, line, col); case '>': switch (next) { case '>': in_stream.read(); int p = in_stream.peek(); if (p != -1) { switch ((char)p) { case '=': in_stream.read(); return factory.create(TokenType.ShiftRightAssign, index, line, col, 3); case '>': in_stream.read(); int q = in_stream.peek(); if (q != -1 && q == '=') { in_stream.read(); return factory.create(TokenType.TripleRightShiftAssign, index, line, col, 4); } return factory.create(TokenType.ShiftRightUnsigned, index, line, col, 3); } } return factory.create(TokenType.ShiftRight, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.GreaterEqual, index, line, col, 2); } return factory.create(TokenType.GreaterThan, index, line, col); case '?': return factory.create(TokenType.Question, index, line, col); case '$': return factory.create(TokenType.Dollar, index, line, col); case ';': return factory.create(TokenType.Semicolon, index, line, col); case ':': return factory.create(TokenType.Colon, index, line, col); case ',': return factory.create(TokenType.Comma, index, line, col); case '.': if (next == '.') { in_stream.read(); int p = in_stream.peek(); if (p != -1 && p == '.') { in_stream.read(); return factory.create(TokenType.TripleDot, index, line, col, 3); } return factory.create(TokenType.DoubleDot, index, line, col, 2); } return factory.create(TokenType.Dot, index, line, col); case ')': return factory.create(TokenType.CloseParenthesis, index, line, col); case '(': return factory.create(TokenType.OpenParenthesis, index, line, col); case ']': return factory.create(TokenType.CloseSquareBracket, index, line, col); case '[': return factory.create(TokenType.OpenSquareBracket, index, line, col); case '}': return factory.create(TokenType.CloseCurlyBrace, index, line, col); case '{': return factory.create(TokenType.OpenCurlyBrace, index, line, col); case '#': return factory.create(TokenType.Hash, index, line, col); default: return null; } }
LexOperator { public Token read() throws IOException, LexerException { int index = in_stream.getPosition(); int line = in_stream.getLine(); int col = in_stream.getCol(); int next = in_stream.peek(); if (next == -1) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Unexpected end of input stream when parsing operator"); } if (next == '.') { int tmp = in_stream.peek(2); if (tmp > 0 && Character.isDigit(tmp)) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Literal float encountered when operator '.' expected"); } } char cur = (char)in_stream.read(); next = in_stream.peek(); switch (cur) { case '+': switch (next) { case '+': in_stream.read(); return factory.create(TokenType.Increment, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.PlusAssign, index, line, col, 2); } return factory.create(TokenType.Plus, index, line, col); case '-': switch (next) { case '-': in_stream.read(); return factory.create(TokenType.Decrement, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.MinusAssign, index, line, col, 2); } return factory.create(TokenType.Minus, index, line, col); case '*': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.TimesAssign, index, line, col, 2); default: break; } return factory.create(TokenType.Times, index, line, col); case '/': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.DivAssign, index, line, col, 2); } return factory.create(TokenType.Div, index, line, col); case '%': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.ModAssign, index, line, col, 2); } return factory.create(TokenType.Mod, index, line, col); case '&': switch (next) { case '&': in_stream.read(); return factory.create(TokenType.LogicalAnd, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.BitwiseAndAssign, index, line, col, 2); } return factory.create(TokenType.BitwiseAnd, index, line, col); case '|': switch (next) { case '|': in_stream.read(); return factory.create(TokenType.LogicalOr, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.BitwiseOrAssign, index, line, col, 2); } return factory.create(TokenType.BitwiseOr, index, line, col); case '^': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.XorAssign, index, line, col, 2); case '^': in_stream.read(); if (in_stream.peek() == '=') { in_stream.read(); return factory.create(TokenType.PowAssign, index, line, col, 3); } return factory.create(TokenType.Pow, index, line, col, 2); } return factory.create(TokenType.Xor, index, line, col); case '!': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.NotEqual, index, line, col, 2); case '<': in_stream.read(); switch (in_stream.peek()) { case '=': in_stream.read(); return factory.create(TokenType.UnorderedOrGreater, index, line, col, 3); case '>': in_stream.read(); switch (in_stream.peek()) { case '=': in_stream.read(); return factory.create(TokenType.Unordered, index, line, col, 4); } return factory.create(TokenType.UnorderedOrEqual, index, line, col, 3); } return factory.create(TokenType.UnorderedGreaterOrEqual, index, line, col, 2); case '>': in_stream.read(); switch (in_stream.peek()) { case '=': in_stream.read(); return factory.create(TokenType.UnorderedOrLess, index, line, col, 3); default: break; } return factory.create(TokenType.UnorderedLessOrEqual, index, line, col, 2); } return factory.create(TokenType.Not, index, line, col); case '~': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.TildeAssign, index, line, col, 2); } return factory.create(TokenType.Tilde, index, line, col); case '=': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.Equal, index, line, col, 2); case '>': in_stream.read(); return factory.create(TokenType.GoesTo, index, line, col, 2); } return factory.create(TokenType.Assign, index, line, col); case '<': switch (next) { case '<': in_stream.read(); switch (in_stream.peek()) { case '=': in_stream.read(); return factory.create(TokenType.ShiftLeftAssign, index, line, col, 3); default: break; } return factory.create(TokenType.ShiftLeft, index, line, col, 2); case '>': in_stream.read(); switch (in_stream.peek()) { case '=': in_stream.read(); return factory.create(TokenType.LessEqualOrGreater, index, line, col, 3); default: break; } return factory.create(TokenType.LessOrGreater, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.LessEqual, index, line, col, 2); } return factory.create(TokenType.LessThan, index, line, col); case '>': switch (next) { case '>': in_stream.read(); int p = in_stream.peek(); if (p != -1) { switch ((char)p) { case '=': in_stream.read(); return factory.create(TokenType.ShiftRightAssign, index, line, col, 3); case '>': in_stream.read(); int q = in_stream.peek(); if (q != -1 && q == '=') { in_stream.read(); return factory.create(TokenType.TripleRightShiftAssign, index, line, col, 4); } return factory.create(TokenType.ShiftRightUnsigned, index, line, col, 3); } } return factory.create(TokenType.ShiftRight, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.GreaterEqual, index, line, col, 2); } return factory.create(TokenType.GreaterThan, index, line, col); case '?': return factory.create(TokenType.Question, index, line, col); case '$': return factory.create(TokenType.Dollar, index, line, col); case ';': return factory.create(TokenType.Semicolon, index, line, col); case ':': return factory.create(TokenType.Colon, index, line, col); case ',': return factory.create(TokenType.Comma, index, line, col); case '.': if (next == '.') { in_stream.read(); int p = in_stream.peek(); if (p != -1 && p == '.') { in_stream.read(); return factory.create(TokenType.TripleDot, index, line, col, 3); } return factory.create(TokenType.DoubleDot, index, line, col, 2); } return factory.create(TokenType.Dot, index, line, col); case ')': return factory.create(TokenType.CloseParenthesis, index, line, col); case '(': return factory.create(TokenType.OpenParenthesis, index, line, col); case ']': return factory.create(TokenType.CloseSquareBracket, index, line, col); case '[': return factory.create(TokenType.OpenSquareBracket, index, line, col); case '}': return factory.create(TokenType.CloseCurlyBrace, index, line, col); case '{': return factory.create(TokenType.OpenCurlyBrace, index, line, col); case '#': return factory.create(TokenType.Hash, index, line, col); default: return null; } } }
LexOperator { public Token read() throws IOException, LexerException { int index = in_stream.getPosition(); int line = in_stream.getLine(); int col = in_stream.getCol(); int next = in_stream.peek(); if (next == -1) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Unexpected end of input stream when parsing operator"); } if (next == '.') { int tmp = in_stream.peek(2); if (tmp > 0 && Character.isDigit(tmp)) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Literal float encountered when operator '.' expected"); } } char cur = (char)in_stream.read(); next = in_stream.peek(); switch (cur) { case '+': switch (next) { case '+': in_stream.read(); return factory.create(TokenType.Increment, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.PlusAssign, index, line, col, 2); } return factory.create(TokenType.Plus, index, line, col); case '-': switch (next) { case '-': in_stream.read(); return factory.create(TokenType.Decrement, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.MinusAssign, index, line, col, 2); } return factory.create(TokenType.Minus, index, line, col); case '*': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.TimesAssign, index, line, col, 2); default: break; } return factory.create(TokenType.Times, index, line, col); case '/': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.DivAssign, index, line, col, 2); } return factory.create(TokenType.Div, index, line, col); case '%': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.ModAssign, index, line, col, 2); } return factory.create(TokenType.Mod, index, line, col); case '&': switch (next) { case '&': in_stream.read(); return factory.create(TokenType.LogicalAnd, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.BitwiseAndAssign, index, line, col, 2); } return factory.create(TokenType.BitwiseAnd, index, line, col); case '|': switch (next) { case '|': in_stream.read(); return factory.create(TokenType.LogicalOr, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.BitwiseOrAssign, index, line, col, 2); } return factory.create(TokenType.BitwiseOr, index, line, col); case '^': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.XorAssign, index, line, col, 2); case '^': in_stream.read(); if (in_stream.peek() == '=') { in_stream.read(); return factory.create(TokenType.PowAssign, index, line, col, 3); } return factory.create(TokenType.Pow, index, line, col, 2); } return factory.create(TokenType.Xor, index, line, col); case '!': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.NotEqual, index, line, col, 2); case '<': in_stream.read(); switch (in_stream.peek()) { case '=': in_stream.read(); return factory.create(TokenType.UnorderedOrGreater, index, line, col, 3); case '>': in_stream.read(); switch (in_stream.peek()) { case '=': in_stream.read(); return factory.create(TokenType.Unordered, index, line, col, 4); } return factory.create(TokenType.UnorderedOrEqual, index, line, col, 3); } return factory.create(TokenType.UnorderedGreaterOrEqual, index, line, col, 2); case '>': in_stream.read(); switch (in_stream.peek()) { case '=': in_stream.read(); return factory.create(TokenType.UnorderedOrLess, index, line, col, 3); default: break; } return factory.create(TokenType.UnorderedLessOrEqual, index, line, col, 2); } return factory.create(TokenType.Not, index, line, col); case '~': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.TildeAssign, index, line, col, 2); } return factory.create(TokenType.Tilde, index, line, col); case '=': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.Equal, index, line, col, 2); case '>': in_stream.read(); return factory.create(TokenType.GoesTo, index, line, col, 2); } return factory.create(TokenType.Assign, index, line, col); case '<': switch (next) { case '<': in_stream.read(); switch (in_stream.peek()) { case '=': in_stream.read(); return factory.create(TokenType.ShiftLeftAssign, index, line, col, 3); default: break; } return factory.create(TokenType.ShiftLeft, index, line, col, 2); case '>': in_stream.read(); switch (in_stream.peek()) { case '=': in_stream.read(); return factory.create(TokenType.LessEqualOrGreater, index, line, col, 3); default: break; } return factory.create(TokenType.LessOrGreater, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.LessEqual, index, line, col, 2); } return factory.create(TokenType.LessThan, index, line, col); case '>': switch (next) { case '>': in_stream.read(); int p = in_stream.peek(); if (p != -1) { switch ((char)p) { case '=': in_stream.read(); return factory.create(TokenType.ShiftRightAssign, index, line, col, 3); case '>': in_stream.read(); int q = in_stream.peek(); if (q != -1 && q == '=') { in_stream.read(); return factory.create(TokenType.TripleRightShiftAssign, index, line, col, 4); } return factory.create(TokenType.ShiftRightUnsigned, index, line, col, 3); } } return factory.create(TokenType.ShiftRight, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.GreaterEqual, index, line, col, 2); } return factory.create(TokenType.GreaterThan, index, line, col); case '?': return factory.create(TokenType.Question, index, line, col); case '$': return factory.create(TokenType.Dollar, index, line, col); case ';': return factory.create(TokenType.Semicolon, index, line, col); case ':': return factory.create(TokenType.Colon, index, line, col); case ',': return factory.create(TokenType.Comma, index, line, col); case '.': if (next == '.') { in_stream.read(); int p = in_stream.peek(); if (p != -1 && p == '.') { in_stream.read(); return factory.create(TokenType.TripleDot, index, line, col, 3); } return factory.create(TokenType.DoubleDot, index, line, col, 2); } return factory.create(TokenType.Dot, index, line, col); case ')': return factory.create(TokenType.CloseParenthesis, index, line, col); case '(': return factory.create(TokenType.OpenParenthesis, index, line, col); case ']': return factory.create(TokenType.CloseSquareBracket, index, line, col); case '[': return factory.create(TokenType.OpenSquareBracket, index, line, col); case '}': return factory.create(TokenType.CloseCurlyBrace, index, line, col); case '{': return factory.create(TokenType.OpenCurlyBrace, index, line, col); case '#': return factory.create(TokenType.Hash, index, line, col); default: return null; } } LexOperator(TokenFactory factory, LexerStream in_stream); }
LexOperator { public Token read() throws IOException, LexerException { int index = in_stream.getPosition(); int line = in_stream.getLine(); int col = in_stream.getCol(); int next = in_stream.peek(); if (next == -1) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Unexpected end of input stream when parsing operator"); } if (next == '.') { int tmp = in_stream.peek(2); if (tmp > 0 && Character.isDigit(tmp)) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Literal float encountered when operator '.' expected"); } } char cur = (char)in_stream.read(); next = in_stream.peek(); switch (cur) { case '+': switch (next) { case '+': in_stream.read(); return factory.create(TokenType.Increment, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.PlusAssign, index, line, col, 2); } return factory.create(TokenType.Plus, index, line, col); case '-': switch (next) { case '-': in_stream.read(); return factory.create(TokenType.Decrement, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.MinusAssign, index, line, col, 2); } return factory.create(TokenType.Minus, index, line, col); case '*': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.TimesAssign, index, line, col, 2); default: break; } return factory.create(TokenType.Times, index, line, col); case '/': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.DivAssign, index, line, col, 2); } return factory.create(TokenType.Div, index, line, col); case '%': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.ModAssign, index, line, col, 2); } return factory.create(TokenType.Mod, index, line, col); case '&': switch (next) { case '&': in_stream.read(); return factory.create(TokenType.LogicalAnd, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.BitwiseAndAssign, index, line, col, 2); } return factory.create(TokenType.BitwiseAnd, index, line, col); case '|': switch (next) { case '|': in_stream.read(); return factory.create(TokenType.LogicalOr, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.BitwiseOrAssign, index, line, col, 2); } return factory.create(TokenType.BitwiseOr, index, line, col); case '^': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.XorAssign, index, line, col, 2); case '^': in_stream.read(); if (in_stream.peek() == '=') { in_stream.read(); return factory.create(TokenType.PowAssign, index, line, col, 3); } return factory.create(TokenType.Pow, index, line, col, 2); } return factory.create(TokenType.Xor, index, line, col); case '!': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.NotEqual, index, line, col, 2); case '<': in_stream.read(); switch (in_stream.peek()) { case '=': in_stream.read(); return factory.create(TokenType.UnorderedOrGreater, index, line, col, 3); case '>': in_stream.read(); switch (in_stream.peek()) { case '=': in_stream.read(); return factory.create(TokenType.Unordered, index, line, col, 4); } return factory.create(TokenType.UnorderedOrEqual, index, line, col, 3); } return factory.create(TokenType.UnorderedGreaterOrEqual, index, line, col, 2); case '>': in_stream.read(); switch (in_stream.peek()) { case '=': in_stream.read(); return factory.create(TokenType.UnorderedOrLess, index, line, col, 3); default: break; } return factory.create(TokenType.UnorderedLessOrEqual, index, line, col, 2); } return factory.create(TokenType.Not, index, line, col); case '~': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.TildeAssign, index, line, col, 2); } return factory.create(TokenType.Tilde, index, line, col); case '=': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.Equal, index, line, col, 2); case '>': in_stream.read(); return factory.create(TokenType.GoesTo, index, line, col, 2); } return factory.create(TokenType.Assign, index, line, col); case '<': switch (next) { case '<': in_stream.read(); switch (in_stream.peek()) { case '=': in_stream.read(); return factory.create(TokenType.ShiftLeftAssign, index, line, col, 3); default: break; } return factory.create(TokenType.ShiftLeft, index, line, col, 2); case '>': in_stream.read(); switch (in_stream.peek()) { case '=': in_stream.read(); return factory.create(TokenType.LessEqualOrGreater, index, line, col, 3); default: break; } return factory.create(TokenType.LessOrGreater, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.LessEqual, index, line, col, 2); } return factory.create(TokenType.LessThan, index, line, col); case '>': switch (next) { case '>': in_stream.read(); int p = in_stream.peek(); if (p != -1) { switch ((char)p) { case '=': in_stream.read(); return factory.create(TokenType.ShiftRightAssign, index, line, col, 3); case '>': in_stream.read(); int q = in_stream.peek(); if (q != -1 && q == '=') { in_stream.read(); return factory.create(TokenType.TripleRightShiftAssign, index, line, col, 4); } return factory.create(TokenType.ShiftRightUnsigned, index, line, col, 3); } } return factory.create(TokenType.ShiftRight, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.GreaterEqual, index, line, col, 2); } return factory.create(TokenType.GreaterThan, index, line, col); case '?': return factory.create(TokenType.Question, index, line, col); case '$': return factory.create(TokenType.Dollar, index, line, col); case ';': return factory.create(TokenType.Semicolon, index, line, col); case ':': return factory.create(TokenType.Colon, index, line, col); case ',': return factory.create(TokenType.Comma, index, line, col); case '.': if (next == '.') { in_stream.read(); int p = in_stream.peek(); if (p != -1 && p == '.') { in_stream.read(); return factory.create(TokenType.TripleDot, index, line, col, 3); } return factory.create(TokenType.DoubleDot, index, line, col, 2); } return factory.create(TokenType.Dot, index, line, col); case ')': return factory.create(TokenType.CloseParenthesis, index, line, col); case '(': return factory.create(TokenType.OpenParenthesis, index, line, col); case ']': return factory.create(TokenType.CloseSquareBracket, index, line, col); case '[': return factory.create(TokenType.OpenSquareBracket, index, line, col); case '}': return factory.create(TokenType.CloseCurlyBrace, index, line, col); case '{': return factory.create(TokenType.OpenCurlyBrace, index, line, col); case '#': return factory.create(TokenType.Hash, index, line, col); default: return null; } } LexOperator(TokenFactory factory, LexerStream in_stream); Token read(); }
LexOperator { public Token read() throws IOException, LexerException { int index = in_stream.getPosition(); int line = in_stream.getLine(); int col = in_stream.getCol(); int next = in_stream.peek(); if (next == -1) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Unexpected end of input stream when parsing operator"); } if (next == '.') { int tmp = in_stream.peek(2); if (tmp > 0 && Character.isDigit(tmp)) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Literal float encountered when operator '.' expected"); } } char cur = (char)in_stream.read(); next = in_stream.peek(); switch (cur) { case '+': switch (next) { case '+': in_stream.read(); return factory.create(TokenType.Increment, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.PlusAssign, index, line, col, 2); } return factory.create(TokenType.Plus, index, line, col); case '-': switch (next) { case '-': in_stream.read(); return factory.create(TokenType.Decrement, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.MinusAssign, index, line, col, 2); } return factory.create(TokenType.Minus, index, line, col); case '*': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.TimesAssign, index, line, col, 2); default: break; } return factory.create(TokenType.Times, index, line, col); case '/': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.DivAssign, index, line, col, 2); } return factory.create(TokenType.Div, index, line, col); case '%': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.ModAssign, index, line, col, 2); } return factory.create(TokenType.Mod, index, line, col); case '&': switch (next) { case '&': in_stream.read(); return factory.create(TokenType.LogicalAnd, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.BitwiseAndAssign, index, line, col, 2); } return factory.create(TokenType.BitwiseAnd, index, line, col); case '|': switch (next) { case '|': in_stream.read(); return factory.create(TokenType.LogicalOr, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.BitwiseOrAssign, index, line, col, 2); } return factory.create(TokenType.BitwiseOr, index, line, col); case '^': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.XorAssign, index, line, col, 2); case '^': in_stream.read(); if (in_stream.peek() == '=') { in_stream.read(); return factory.create(TokenType.PowAssign, index, line, col, 3); } return factory.create(TokenType.Pow, index, line, col, 2); } return factory.create(TokenType.Xor, index, line, col); case '!': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.NotEqual, index, line, col, 2); case '<': in_stream.read(); switch (in_stream.peek()) { case '=': in_stream.read(); return factory.create(TokenType.UnorderedOrGreater, index, line, col, 3); case '>': in_stream.read(); switch (in_stream.peek()) { case '=': in_stream.read(); return factory.create(TokenType.Unordered, index, line, col, 4); } return factory.create(TokenType.UnorderedOrEqual, index, line, col, 3); } return factory.create(TokenType.UnorderedGreaterOrEqual, index, line, col, 2); case '>': in_stream.read(); switch (in_stream.peek()) { case '=': in_stream.read(); return factory.create(TokenType.UnorderedOrLess, index, line, col, 3); default: break; } return factory.create(TokenType.UnorderedLessOrEqual, index, line, col, 2); } return factory.create(TokenType.Not, index, line, col); case '~': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.TildeAssign, index, line, col, 2); } return factory.create(TokenType.Tilde, index, line, col); case '=': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.Equal, index, line, col, 2); case '>': in_stream.read(); return factory.create(TokenType.GoesTo, index, line, col, 2); } return factory.create(TokenType.Assign, index, line, col); case '<': switch (next) { case '<': in_stream.read(); switch (in_stream.peek()) { case '=': in_stream.read(); return factory.create(TokenType.ShiftLeftAssign, index, line, col, 3); default: break; } return factory.create(TokenType.ShiftLeft, index, line, col, 2); case '>': in_stream.read(); switch (in_stream.peek()) { case '=': in_stream.read(); return factory.create(TokenType.LessEqualOrGreater, index, line, col, 3); default: break; } return factory.create(TokenType.LessOrGreater, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.LessEqual, index, line, col, 2); } return factory.create(TokenType.LessThan, index, line, col); case '>': switch (next) { case '>': in_stream.read(); int p = in_stream.peek(); if (p != -1) { switch ((char)p) { case '=': in_stream.read(); return factory.create(TokenType.ShiftRightAssign, index, line, col, 3); case '>': in_stream.read(); int q = in_stream.peek(); if (q != -1 && q == '=') { in_stream.read(); return factory.create(TokenType.TripleRightShiftAssign, index, line, col, 4); } return factory.create(TokenType.ShiftRightUnsigned, index, line, col, 3); } } return factory.create(TokenType.ShiftRight, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.GreaterEqual, index, line, col, 2); } return factory.create(TokenType.GreaterThan, index, line, col); case '?': return factory.create(TokenType.Question, index, line, col); case '$': return factory.create(TokenType.Dollar, index, line, col); case ';': return factory.create(TokenType.Semicolon, index, line, col); case ':': return factory.create(TokenType.Colon, index, line, col); case ',': return factory.create(TokenType.Comma, index, line, col); case '.': if (next == '.') { in_stream.read(); int p = in_stream.peek(); if (p != -1 && p == '.') { in_stream.read(); return factory.create(TokenType.TripleDot, index, line, col, 3); } return factory.create(TokenType.DoubleDot, index, line, col, 2); } return factory.create(TokenType.Dot, index, line, col); case ')': return factory.create(TokenType.CloseParenthesis, index, line, col); case '(': return factory.create(TokenType.OpenParenthesis, index, line, col); case ']': return factory.create(TokenType.CloseSquareBracket, index, line, col); case '[': return factory.create(TokenType.OpenSquareBracket, index, line, col); case '}': return factory.create(TokenType.CloseCurlyBrace, index, line, col); case '{': return factory.create(TokenType.OpenCurlyBrace, index, line, col); case '#': return factory.create(TokenType.Hash, index, line, col); default: return null; } } LexOperator(TokenFactory factory, LexerStream in_stream); Token read(); }
@Test public void identifier() throws Exception { String str = "foo _bar a_2"; LexerStream ls = new LexerStream(new StringReader(str)); LexIdentifier lex = new LexIdentifier(new BaseTokenFactory(), ls); Token tok = lex.read(); assertEquals(TokenType.Identifier, tok.getType()); assertEquals("foo", tok.getValue()); assertEquals(1, tok.getLine()); assertEquals(1, tok.getCol()); assertEquals(1, tok.getEndLine()); assertEquals(4, tok.getEndCol()); ls.read(); tok = lex.read(); assertEquals(TokenType.Identifier, tok.getType()); assertEquals("_bar", tok.getValue()); assertEquals(1, tok.getLine()); assertEquals(5, tok.getCol()); assertEquals(1, tok.getEndLine()); assertEquals(9, tok.getEndCol()); ls.read(); tok = lex.read(); assertEquals(TokenType.Identifier, tok.getType()); assertEquals("a_2", tok.getValue()); assertEquals(1, tok.getLine()); assertEquals(10, tok.getCol()); assertEquals(1, tok.getEndLine()); assertEquals(13, tok.getEndCol()); }
public Token read() throws IOException, LexerException { int start_index = in_stream.getPosition(); int start_line = in_stream.getLine(); int start_col = in_stream.getCol(); int next = in_stream.peek(); if (next == -1) { throw new LexerException(start_line, start_col, "Unexpected end of input stream when parsing identifier"); } if (!Character.isLetter(next) && next != '_') { throw new LexerException(start_line, start_col, "Invalid character for start of identifier: "+(char)next); } StringBuilder sb = new StringBuilder(); while (valid_ident_char(next)) { sb.append(Character.toChars(in_stream.read())); next = in_stream.peek(); } String result = sb.toString(); TokenType type = TokenType.forValue(result); if (type == null) { type = TokenType.Identifier; } if (type == TokenType.Identifier && result.startsWith("__")) { throw new LexerException(start_line, start_col, "Unknown reserved identifier"); } return factory.create(type, start_index, start_line, start_col, in_stream.getPosition(), in_stream.getLine(), in_stream.getCol(), result); }
LexIdentifier { public Token read() throws IOException, LexerException { int start_index = in_stream.getPosition(); int start_line = in_stream.getLine(); int start_col = in_stream.getCol(); int next = in_stream.peek(); if (next == -1) { throw new LexerException(start_line, start_col, "Unexpected end of input stream when parsing identifier"); } if (!Character.isLetter(next) && next != '_') { throw new LexerException(start_line, start_col, "Invalid character for start of identifier: "+(char)next); } StringBuilder sb = new StringBuilder(); while (valid_ident_char(next)) { sb.append(Character.toChars(in_stream.read())); next = in_stream.peek(); } String result = sb.toString(); TokenType type = TokenType.forValue(result); if (type == null) { type = TokenType.Identifier; } if (type == TokenType.Identifier && result.startsWith("__")) { throw new LexerException(start_line, start_col, "Unknown reserved identifier"); } return factory.create(type, start_index, start_line, start_col, in_stream.getPosition(), in_stream.getLine(), in_stream.getCol(), result); } }
LexIdentifier { public Token read() throws IOException, LexerException { int start_index = in_stream.getPosition(); int start_line = in_stream.getLine(); int start_col = in_stream.getCol(); int next = in_stream.peek(); if (next == -1) { throw new LexerException(start_line, start_col, "Unexpected end of input stream when parsing identifier"); } if (!Character.isLetter(next) && next != '_') { throw new LexerException(start_line, start_col, "Invalid character for start of identifier: "+(char)next); } StringBuilder sb = new StringBuilder(); while (valid_ident_char(next)) { sb.append(Character.toChars(in_stream.read())); next = in_stream.peek(); } String result = sb.toString(); TokenType type = TokenType.forValue(result); if (type == null) { type = TokenType.Identifier; } if (type == TokenType.Identifier && result.startsWith("__")) { throw new LexerException(start_line, start_col, "Unknown reserved identifier"); } return factory.create(type, start_index, start_line, start_col, in_stream.getPosition(), in_stream.getLine(), in_stream.getCol(), result); } LexIdentifier(TokenFactory factory, LexerStream in_stream); }
LexIdentifier { public Token read() throws IOException, LexerException { int start_index = in_stream.getPosition(); int start_line = in_stream.getLine(); int start_col = in_stream.getCol(); int next = in_stream.peek(); if (next == -1) { throw new LexerException(start_line, start_col, "Unexpected end of input stream when parsing identifier"); } if (!Character.isLetter(next) && next != '_') { throw new LexerException(start_line, start_col, "Invalid character for start of identifier: "+(char)next); } StringBuilder sb = new StringBuilder(); while (valid_ident_char(next)) { sb.append(Character.toChars(in_stream.read())); next = in_stream.peek(); } String result = sb.toString(); TokenType type = TokenType.forValue(result); if (type == null) { type = TokenType.Identifier; } if (type == TokenType.Identifier && result.startsWith("__")) { throw new LexerException(start_line, start_col, "Unknown reserved identifier"); } return factory.create(type, start_index, start_line, start_col, in_stream.getPosition(), in_stream.getLine(), in_stream.getCol(), result); } LexIdentifier(TokenFactory factory, LexerStream in_stream); Token read(); }
LexIdentifier { public Token read() throws IOException, LexerException { int start_index = in_stream.getPosition(); int start_line = in_stream.getLine(); int start_col = in_stream.getCol(); int next = in_stream.peek(); if (next == -1) { throw new LexerException(start_line, start_col, "Unexpected end of input stream when parsing identifier"); } if (!Character.isLetter(next) && next != '_') { throw new LexerException(start_line, start_col, "Invalid character for start of identifier: "+(char)next); } StringBuilder sb = new StringBuilder(); while (valid_ident_char(next)) { sb.append(Character.toChars(in_stream.read())); next = in_stream.peek(); } String result = sb.toString(); TokenType type = TokenType.forValue(result); if (type == null) { type = TokenType.Identifier; } if (type == TokenType.Identifier && result.startsWith("__")) { throw new LexerException(start_line, start_col, "Unknown reserved identifier"); } return factory.create(type, start_index, start_line, start_col, in_stream.getPosition(), in_stream.getLine(), in_stream.getCol(), result); } LexIdentifier(TokenFactory factory, LexerStream in_stream); Token read(); }
@Test public void tilde() throws Exception { String str = "a~=b~c;"; LexerStream ls = new LexerStream(new StringReader(str)); LexOperator lex = new LexOperator(new BaseTokenFactory(), ls); ls.read(); Token tok = lex.read(); assertEquals(TokenType.TildeAssign, tok.getType()); assertTrue(tok.getLine() == 1 && tok.getEndLine() == 1); assertEquals(2, tok.getCol()); assertEquals(4, tok.getEndCol()); ls.read(); tok = lex.read(); assertEquals(TokenType.Tilde, tok.getType()); assertTrue(tok.getLine() == 1 && tok.getEndLine() == 1); assertEquals(5, tok.getCol()); assertEquals(6, tok.getEndCol()); }
public Token read() throws IOException, LexerException { int index = in_stream.getPosition(); int line = in_stream.getLine(); int col = in_stream.getCol(); int next = in_stream.peek(); if (next == -1) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Unexpected end of input stream when parsing operator"); } if (next == '.') { int tmp = in_stream.peek(2); if (tmp > 0 && Character.isDigit(tmp)) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Literal float encountered when operator '.' expected"); } } char cur = (char)in_stream.read(); next = in_stream.peek(); switch (cur) { case '+': switch (next) { case '+': in_stream.read(); return factory.create(TokenType.Increment, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.PlusAssign, index, line, col, 2); } return factory.create(TokenType.Plus, index, line, col); case '-': switch (next) { case '-': in_stream.read(); return factory.create(TokenType.Decrement, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.MinusAssign, index, line, col, 2); } return factory.create(TokenType.Minus, index, line, col); case '*': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.TimesAssign, index, line, col, 2); default: break; } return factory.create(TokenType.Times, index, line, col); case '/': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.DivAssign, index, line, col, 2); } return factory.create(TokenType.Div, index, line, col); case '%': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.ModAssign, index, line, col, 2); } return factory.create(TokenType.Mod, index, line, col); case '&': switch (next) { case '&': in_stream.read(); return factory.create(TokenType.LogicalAnd, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.BitwiseAndAssign, index, line, col, 2); } return factory.create(TokenType.BitwiseAnd, index, line, col); case '|': switch (next) { case '|': in_stream.read(); return factory.create(TokenType.LogicalOr, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.BitwiseOrAssign, index, line, col, 2); } return factory.create(TokenType.BitwiseOr, index, line, col); case '^': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.XorAssign, index, line, col, 2); case '^': in_stream.read(); if (in_stream.peek() == '=') { in_stream.read(); return factory.create(TokenType.PowAssign, index, line, col, 3); } return factory.create(TokenType.Pow, index, line, col, 2); } return factory.create(TokenType.Xor, index, line, col); case '!': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.NotEqual, index, line, col, 2); case '<': in_stream.read(); switch (in_stream.peek()) { case '=': in_stream.read(); return factory.create(TokenType.UnorderedOrGreater, index, line, col, 3); case '>': in_stream.read(); switch (in_stream.peek()) { case '=': in_stream.read(); return factory.create(TokenType.Unordered, index, line, col, 4); } return factory.create(TokenType.UnorderedOrEqual, index, line, col, 3); } return factory.create(TokenType.UnorderedGreaterOrEqual, index, line, col, 2); case '>': in_stream.read(); switch (in_stream.peek()) { case '=': in_stream.read(); return factory.create(TokenType.UnorderedOrLess, index, line, col, 3); default: break; } return factory.create(TokenType.UnorderedLessOrEqual, index, line, col, 2); } return factory.create(TokenType.Not, index, line, col); case '~': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.TildeAssign, index, line, col, 2); } return factory.create(TokenType.Tilde, index, line, col); case '=': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.Equal, index, line, col, 2); case '>': in_stream.read(); return factory.create(TokenType.GoesTo, index, line, col, 2); } return factory.create(TokenType.Assign, index, line, col); case '<': switch (next) { case '<': in_stream.read(); switch (in_stream.peek()) { case '=': in_stream.read(); return factory.create(TokenType.ShiftLeftAssign, index, line, col, 3); default: break; } return factory.create(TokenType.ShiftLeft, index, line, col, 2); case '>': in_stream.read(); switch (in_stream.peek()) { case '=': in_stream.read(); return factory.create(TokenType.LessEqualOrGreater, index, line, col, 3); default: break; } return factory.create(TokenType.LessOrGreater, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.LessEqual, index, line, col, 2); } return factory.create(TokenType.LessThan, index, line, col); case '>': switch (next) { case '>': in_stream.read(); int p = in_stream.peek(); if (p != -1) { switch ((char)p) { case '=': in_stream.read(); return factory.create(TokenType.ShiftRightAssign, index, line, col, 3); case '>': in_stream.read(); int q = in_stream.peek(); if (q != -1 && q == '=') { in_stream.read(); return factory.create(TokenType.TripleRightShiftAssign, index, line, col, 4); } return factory.create(TokenType.ShiftRightUnsigned, index, line, col, 3); } } return factory.create(TokenType.ShiftRight, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.GreaterEqual, index, line, col, 2); } return factory.create(TokenType.GreaterThan, index, line, col); case '?': return factory.create(TokenType.Question, index, line, col); case '$': return factory.create(TokenType.Dollar, index, line, col); case ';': return factory.create(TokenType.Semicolon, index, line, col); case ':': return factory.create(TokenType.Colon, index, line, col); case ',': return factory.create(TokenType.Comma, index, line, col); case '.': if (next == '.') { in_stream.read(); int p = in_stream.peek(); if (p != -1 && p == '.') { in_stream.read(); return factory.create(TokenType.TripleDot, index, line, col, 3); } return factory.create(TokenType.DoubleDot, index, line, col, 2); } return factory.create(TokenType.Dot, index, line, col); case ')': return factory.create(TokenType.CloseParenthesis, index, line, col); case '(': return factory.create(TokenType.OpenParenthesis, index, line, col); case ']': return factory.create(TokenType.CloseSquareBracket, index, line, col); case '[': return factory.create(TokenType.OpenSquareBracket, index, line, col); case '}': return factory.create(TokenType.CloseCurlyBrace, index, line, col); case '{': return factory.create(TokenType.OpenCurlyBrace, index, line, col); case '#': return factory.create(TokenType.Hash, index, line, col); default: return null; } }
LexOperator { public Token read() throws IOException, LexerException { int index = in_stream.getPosition(); int line = in_stream.getLine(); int col = in_stream.getCol(); int next = in_stream.peek(); if (next == -1) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Unexpected end of input stream when parsing operator"); } if (next == '.') { int tmp = in_stream.peek(2); if (tmp > 0 && Character.isDigit(tmp)) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Literal float encountered when operator '.' expected"); } } char cur = (char)in_stream.read(); next = in_stream.peek(); switch (cur) { case '+': switch (next) { case '+': in_stream.read(); return factory.create(TokenType.Increment, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.PlusAssign, index, line, col, 2); } return factory.create(TokenType.Plus, index, line, col); case '-': switch (next) { case '-': in_stream.read(); return factory.create(TokenType.Decrement, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.MinusAssign, index, line, col, 2); } return factory.create(TokenType.Minus, index, line, col); case '*': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.TimesAssign, index, line, col, 2); default: break; } return factory.create(TokenType.Times, index, line, col); case '/': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.DivAssign, index, line, col, 2); } return factory.create(TokenType.Div, index, line, col); case '%': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.ModAssign, index, line, col, 2); } return factory.create(TokenType.Mod, index, line, col); case '&': switch (next) { case '&': in_stream.read(); return factory.create(TokenType.LogicalAnd, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.BitwiseAndAssign, index, line, col, 2); } return factory.create(TokenType.BitwiseAnd, index, line, col); case '|': switch (next) { case '|': in_stream.read(); return factory.create(TokenType.LogicalOr, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.BitwiseOrAssign, index, line, col, 2); } return factory.create(TokenType.BitwiseOr, index, line, col); case '^': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.XorAssign, index, line, col, 2); case '^': in_stream.read(); if (in_stream.peek() == '=') { in_stream.read(); return factory.create(TokenType.PowAssign, index, line, col, 3); } return factory.create(TokenType.Pow, index, line, col, 2); } return factory.create(TokenType.Xor, index, line, col); case '!': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.NotEqual, index, line, col, 2); case '<': in_stream.read(); switch (in_stream.peek()) { case '=': in_stream.read(); return factory.create(TokenType.UnorderedOrGreater, index, line, col, 3); case '>': in_stream.read(); switch (in_stream.peek()) { case '=': in_stream.read(); return factory.create(TokenType.Unordered, index, line, col, 4); } return factory.create(TokenType.UnorderedOrEqual, index, line, col, 3); } return factory.create(TokenType.UnorderedGreaterOrEqual, index, line, col, 2); case '>': in_stream.read(); switch (in_stream.peek()) { case '=': in_stream.read(); return factory.create(TokenType.UnorderedOrLess, index, line, col, 3); default: break; } return factory.create(TokenType.UnorderedLessOrEqual, index, line, col, 2); } return factory.create(TokenType.Not, index, line, col); case '~': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.TildeAssign, index, line, col, 2); } return factory.create(TokenType.Tilde, index, line, col); case '=': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.Equal, index, line, col, 2); case '>': in_stream.read(); return factory.create(TokenType.GoesTo, index, line, col, 2); } return factory.create(TokenType.Assign, index, line, col); case '<': switch (next) { case '<': in_stream.read(); switch (in_stream.peek()) { case '=': in_stream.read(); return factory.create(TokenType.ShiftLeftAssign, index, line, col, 3); default: break; } return factory.create(TokenType.ShiftLeft, index, line, col, 2); case '>': in_stream.read(); switch (in_stream.peek()) { case '=': in_stream.read(); return factory.create(TokenType.LessEqualOrGreater, index, line, col, 3); default: break; } return factory.create(TokenType.LessOrGreater, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.LessEqual, index, line, col, 2); } return factory.create(TokenType.LessThan, index, line, col); case '>': switch (next) { case '>': in_stream.read(); int p = in_stream.peek(); if (p != -1) { switch ((char)p) { case '=': in_stream.read(); return factory.create(TokenType.ShiftRightAssign, index, line, col, 3); case '>': in_stream.read(); int q = in_stream.peek(); if (q != -1 && q == '=') { in_stream.read(); return factory.create(TokenType.TripleRightShiftAssign, index, line, col, 4); } return factory.create(TokenType.ShiftRightUnsigned, index, line, col, 3); } } return factory.create(TokenType.ShiftRight, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.GreaterEqual, index, line, col, 2); } return factory.create(TokenType.GreaterThan, index, line, col); case '?': return factory.create(TokenType.Question, index, line, col); case '$': return factory.create(TokenType.Dollar, index, line, col); case ';': return factory.create(TokenType.Semicolon, index, line, col); case ':': return factory.create(TokenType.Colon, index, line, col); case ',': return factory.create(TokenType.Comma, index, line, col); case '.': if (next == '.') { in_stream.read(); int p = in_stream.peek(); if (p != -1 && p == '.') { in_stream.read(); return factory.create(TokenType.TripleDot, index, line, col, 3); } return factory.create(TokenType.DoubleDot, index, line, col, 2); } return factory.create(TokenType.Dot, index, line, col); case ')': return factory.create(TokenType.CloseParenthesis, index, line, col); case '(': return factory.create(TokenType.OpenParenthesis, index, line, col); case ']': return factory.create(TokenType.CloseSquareBracket, index, line, col); case '[': return factory.create(TokenType.OpenSquareBracket, index, line, col); case '}': return factory.create(TokenType.CloseCurlyBrace, index, line, col); case '{': return factory.create(TokenType.OpenCurlyBrace, index, line, col); case '#': return factory.create(TokenType.Hash, index, line, col); default: return null; } } }
LexOperator { public Token read() throws IOException, LexerException { int index = in_stream.getPosition(); int line = in_stream.getLine(); int col = in_stream.getCol(); int next = in_stream.peek(); if (next == -1) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Unexpected end of input stream when parsing operator"); } if (next == '.') { int tmp = in_stream.peek(2); if (tmp > 0 && Character.isDigit(tmp)) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Literal float encountered when operator '.' expected"); } } char cur = (char)in_stream.read(); next = in_stream.peek(); switch (cur) { case '+': switch (next) { case '+': in_stream.read(); return factory.create(TokenType.Increment, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.PlusAssign, index, line, col, 2); } return factory.create(TokenType.Plus, index, line, col); case '-': switch (next) { case '-': in_stream.read(); return factory.create(TokenType.Decrement, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.MinusAssign, index, line, col, 2); } return factory.create(TokenType.Minus, index, line, col); case '*': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.TimesAssign, index, line, col, 2); default: break; } return factory.create(TokenType.Times, index, line, col); case '/': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.DivAssign, index, line, col, 2); } return factory.create(TokenType.Div, index, line, col); case '%': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.ModAssign, index, line, col, 2); } return factory.create(TokenType.Mod, index, line, col); case '&': switch (next) { case '&': in_stream.read(); return factory.create(TokenType.LogicalAnd, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.BitwiseAndAssign, index, line, col, 2); } return factory.create(TokenType.BitwiseAnd, index, line, col); case '|': switch (next) { case '|': in_stream.read(); return factory.create(TokenType.LogicalOr, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.BitwiseOrAssign, index, line, col, 2); } return factory.create(TokenType.BitwiseOr, index, line, col); case '^': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.XorAssign, index, line, col, 2); case '^': in_stream.read(); if (in_stream.peek() == '=') { in_stream.read(); return factory.create(TokenType.PowAssign, index, line, col, 3); } return factory.create(TokenType.Pow, index, line, col, 2); } return factory.create(TokenType.Xor, index, line, col); case '!': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.NotEqual, index, line, col, 2); case '<': in_stream.read(); switch (in_stream.peek()) { case '=': in_stream.read(); return factory.create(TokenType.UnorderedOrGreater, index, line, col, 3); case '>': in_stream.read(); switch (in_stream.peek()) { case '=': in_stream.read(); return factory.create(TokenType.Unordered, index, line, col, 4); } return factory.create(TokenType.UnorderedOrEqual, index, line, col, 3); } return factory.create(TokenType.UnorderedGreaterOrEqual, index, line, col, 2); case '>': in_stream.read(); switch (in_stream.peek()) { case '=': in_stream.read(); return factory.create(TokenType.UnorderedOrLess, index, line, col, 3); default: break; } return factory.create(TokenType.UnorderedLessOrEqual, index, line, col, 2); } return factory.create(TokenType.Not, index, line, col); case '~': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.TildeAssign, index, line, col, 2); } return factory.create(TokenType.Tilde, index, line, col); case '=': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.Equal, index, line, col, 2); case '>': in_stream.read(); return factory.create(TokenType.GoesTo, index, line, col, 2); } return factory.create(TokenType.Assign, index, line, col); case '<': switch (next) { case '<': in_stream.read(); switch (in_stream.peek()) { case '=': in_stream.read(); return factory.create(TokenType.ShiftLeftAssign, index, line, col, 3); default: break; } return factory.create(TokenType.ShiftLeft, index, line, col, 2); case '>': in_stream.read(); switch (in_stream.peek()) { case '=': in_stream.read(); return factory.create(TokenType.LessEqualOrGreater, index, line, col, 3); default: break; } return factory.create(TokenType.LessOrGreater, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.LessEqual, index, line, col, 2); } return factory.create(TokenType.LessThan, index, line, col); case '>': switch (next) { case '>': in_stream.read(); int p = in_stream.peek(); if (p != -1) { switch ((char)p) { case '=': in_stream.read(); return factory.create(TokenType.ShiftRightAssign, index, line, col, 3); case '>': in_stream.read(); int q = in_stream.peek(); if (q != -1 && q == '=') { in_stream.read(); return factory.create(TokenType.TripleRightShiftAssign, index, line, col, 4); } return factory.create(TokenType.ShiftRightUnsigned, index, line, col, 3); } } return factory.create(TokenType.ShiftRight, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.GreaterEqual, index, line, col, 2); } return factory.create(TokenType.GreaterThan, index, line, col); case '?': return factory.create(TokenType.Question, index, line, col); case '$': return factory.create(TokenType.Dollar, index, line, col); case ';': return factory.create(TokenType.Semicolon, index, line, col); case ':': return factory.create(TokenType.Colon, index, line, col); case ',': return factory.create(TokenType.Comma, index, line, col); case '.': if (next == '.') { in_stream.read(); int p = in_stream.peek(); if (p != -1 && p == '.') { in_stream.read(); return factory.create(TokenType.TripleDot, index, line, col, 3); } return factory.create(TokenType.DoubleDot, index, line, col, 2); } return factory.create(TokenType.Dot, index, line, col); case ')': return factory.create(TokenType.CloseParenthesis, index, line, col); case '(': return factory.create(TokenType.OpenParenthesis, index, line, col); case ']': return factory.create(TokenType.CloseSquareBracket, index, line, col); case '[': return factory.create(TokenType.OpenSquareBracket, index, line, col); case '}': return factory.create(TokenType.CloseCurlyBrace, index, line, col); case '{': return factory.create(TokenType.OpenCurlyBrace, index, line, col); case '#': return factory.create(TokenType.Hash, index, line, col); default: return null; } } LexOperator(TokenFactory factory, LexerStream in_stream); }
LexOperator { public Token read() throws IOException, LexerException { int index = in_stream.getPosition(); int line = in_stream.getLine(); int col = in_stream.getCol(); int next = in_stream.peek(); if (next == -1) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Unexpected end of input stream when parsing operator"); } if (next == '.') { int tmp = in_stream.peek(2); if (tmp > 0 && Character.isDigit(tmp)) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Literal float encountered when operator '.' expected"); } } char cur = (char)in_stream.read(); next = in_stream.peek(); switch (cur) { case '+': switch (next) { case '+': in_stream.read(); return factory.create(TokenType.Increment, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.PlusAssign, index, line, col, 2); } return factory.create(TokenType.Plus, index, line, col); case '-': switch (next) { case '-': in_stream.read(); return factory.create(TokenType.Decrement, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.MinusAssign, index, line, col, 2); } return factory.create(TokenType.Minus, index, line, col); case '*': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.TimesAssign, index, line, col, 2); default: break; } return factory.create(TokenType.Times, index, line, col); case '/': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.DivAssign, index, line, col, 2); } return factory.create(TokenType.Div, index, line, col); case '%': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.ModAssign, index, line, col, 2); } return factory.create(TokenType.Mod, index, line, col); case '&': switch (next) { case '&': in_stream.read(); return factory.create(TokenType.LogicalAnd, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.BitwiseAndAssign, index, line, col, 2); } return factory.create(TokenType.BitwiseAnd, index, line, col); case '|': switch (next) { case '|': in_stream.read(); return factory.create(TokenType.LogicalOr, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.BitwiseOrAssign, index, line, col, 2); } return factory.create(TokenType.BitwiseOr, index, line, col); case '^': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.XorAssign, index, line, col, 2); case '^': in_stream.read(); if (in_stream.peek() == '=') { in_stream.read(); return factory.create(TokenType.PowAssign, index, line, col, 3); } return factory.create(TokenType.Pow, index, line, col, 2); } return factory.create(TokenType.Xor, index, line, col); case '!': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.NotEqual, index, line, col, 2); case '<': in_stream.read(); switch (in_stream.peek()) { case '=': in_stream.read(); return factory.create(TokenType.UnorderedOrGreater, index, line, col, 3); case '>': in_stream.read(); switch (in_stream.peek()) { case '=': in_stream.read(); return factory.create(TokenType.Unordered, index, line, col, 4); } return factory.create(TokenType.UnorderedOrEqual, index, line, col, 3); } return factory.create(TokenType.UnorderedGreaterOrEqual, index, line, col, 2); case '>': in_stream.read(); switch (in_stream.peek()) { case '=': in_stream.read(); return factory.create(TokenType.UnorderedOrLess, index, line, col, 3); default: break; } return factory.create(TokenType.UnorderedLessOrEqual, index, line, col, 2); } return factory.create(TokenType.Not, index, line, col); case '~': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.TildeAssign, index, line, col, 2); } return factory.create(TokenType.Tilde, index, line, col); case '=': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.Equal, index, line, col, 2); case '>': in_stream.read(); return factory.create(TokenType.GoesTo, index, line, col, 2); } return factory.create(TokenType.Assign, index, line, col); case '<': switch (next) { case '<': in_stream.read(); switch (in_stream.peek()) { case '=': in_stream.read(); return factory.create(TokenType.ShiftLeftAssign, index, line, col, 3); default: break; } return factory.create(TokenType.ShiftLeft, index, line, col, 2); case '>': in_stream.read(); switch (in_stream.peek()) { case '=': in_stream.read(); return factory.create(TokenType.LessEqualOrGreater, index, line, col, 3); default: break; } return factory.create(TokenType.LessOrGreater, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.LessEqual, index, line, col, 2); } return factory.create(TokenType.LessThan, index, line, col); case '>': switch (next) { case '>': in_stream.read(); int p = in_stream.peek(); if (p != -1) { switch ((char)p) { case '=': in_stream.read(); return factory.create(TokenType.ShiftRightAssign, index, line, col, 3); case '>': in_stream.read(); int q = in_stream.peek(); if (q != -1 && q == '=') { in_stream.read(); return factory.create(TokenType.TripleRightShiftAssign, index, line, col, 4); } return factory.create(TokenType.ShiftRightUnsigned, index, line, col, 3); } } return factory.create(TokenType.ShiftRight, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.GreaterEqual, index, line, col, 2); } return factory.create(TokenType.GreaterThan, index, line, col); case '?': return factory.create(TokenType.Question, index, line, col); case '$': return factory.create(TokenType.Dollar, index, line, col); case ';': return factory.create(TokenType.Semicolon, index, line, col); case ':': return factory.create(TokenType.Colon, index, line, col); case ',': return factory.create(TokenType.Comma, index, line, col); case '.': if (next == '.') { in_stream.read(); int p = in_stream.peek(); if (p != -1 && p == '.') { in_stream.read(); return factory.create(TokenType.TripleDot, index, line, col, 3); } return factory.create(TokenType.DoubleDot, index, line, col, 2); } return factory.create(TokenType.Dot, index, line, col); case ')': return factory.create(TokenType.CloseParenthesis, index, line, col); case '(': return factory.create(TokenType.OpenParenthesis, index, line, col); case ']': return factory.create(TokenType.CloseSquareBracket, index, line, col); case '[': return factory.create(TokenType.OpenSquareBracket, index, line, col); case '}': return factory.create(TokenType.CloseCurlyBrace, index, line, col); case '{': return factory.create(TokenType.OpenCurlyBrace, index, line, col); case '#': return factory.create(TokenType.Hash, index, line, col); default: return null; } } LexOperator(TokenFactory factory, LexerStream in_stream); Token read(); }
LexOperator { public Token read() throws IOException, LexerException { int index = in_stream.getPosition(); int line = in_stream.getLine(); int col = in_stream.getCol(); int next = in_stream.peek(); if (next == -1) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Unexpected end of input stream when parsing operator"); } if (next == '.') { int tmp = in_stream.peek(2); if (tmp > 0 && Character.isDigit(tmp)) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Literal float encountered when operator '.' expected"); } } char cur = (char)in_stream.read(); next = in_stream.peek(); switch (cur) { case '+': switch (next) { case '+': in_stream.read(); return factory.create(TokenType.Increment, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.PlusAssign, index, line, col, 2); } return factory.create(TokenType.Plus, index, line, col); case '-': switch (next) { case '-': in_stream.read(); return factory.create(TokenType.Decrement, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.MinusAssign, index, line, col, 2); } return factory.create(TokenType.Minus, index, line, col); case '*': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.TimesAssign, index, line, col, 2); default: break; } return factory.create(TokenType.Times, index, line, col); case '/': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.DivAssign, index, line, col, 2); } return factory.create(TokenType.Div, index, line, col); case '%': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.ModAssign, index, line, col, 2); } return factory.create(TokenType.Mod, index, line, col); case '&': switch (next) { case '&': in_stream.read(); return factory.create(TokenType.LogicalAnd, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.BitwiseAndAssign, index, line, col, 2); } return factory.create(TokenType.BitwiseAnd, index, line, col); case '|': switch (next) { case '|': in_stream.read(); return factory.create(TokenType.LogicalOr, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.BitwiseOrAssign, index, line, col, 2); } return factory.create(TokenType.BitwiseOr, index, line, col); case '^': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.XorAssign, index, line, col, 2); case '^': in_stream.read(); if (in_stream.peek() == '=') { in_stream.read(); return factory.create(TokenType.PowAssign, index, line, col, 3); } return factory.create(TokenType.Pow, index, line, col, 2); } return factory.create(TokenType.Xor, index, line, col); case '!': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.NotEqual, index, line, col, 2); case '<': in_stream.read(); switch (in_stream.peek()) { case '=': in_stream.read(); return factory.create(TokenType.UnorderedOrGreater, index, line, col, 3); case '>': in_stream.read(); switch (in_stream.peek()) { case '=': in_stream.read(); return factory.create(TokenType.Unordered, index, line, col, 4); } return factory.create(TokenType.UnorderedOrEqual, index, line, col, 3); } return factory.create(TokenType.UnorderedGreaterOrEqual, index, line, col, 2); case '>': in_stream.read(); switch (in_stream.peek()) { case '=': in_stream.read(); return factory.create(TokenType.UnorderedOrLess, index, line, col, 3); default: break; } return factory.create(TokenType.UnorderedLessOrEqual, index, line, col, 2); } return factory.create(TokenType.Not, index, line, col); case '~': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.TildeAssign, index, line, col, 2); } return factory.create(TokenType.Tilde, index, line, col); case '=': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.Equal, index, line, col, 2); case '>': in_stream.read(); return factory.create(TokenType.GoesTo, index, line, col, 2); } return factory.create(TokenType.Assign, index, line, col); case '<': switch (next) { case '<': in_stream.read(); switch (in_stream.peek()) { case '=': in_stream.read(); return factory.create(TokenType.ShiftLeftAssign, index, line, col, 3); default: break; } return factory.create(TokenType.ShiftLeft, index, line, col, 2); case '>': in_stream.read(); switch (in_stream.peek()) { case '=': in_stream.read(); return factory.create(TokenType.LessEqualOrGreater, index, line, col, 3); default: break; } return factory.create(TokenType.LessOrGreater, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.LessEqual, index, line, col, 2); } return factory.create(TokenType.LessThan, index, line, col); case '>': switch (next) { case '>': in_stream.read(); int p = in_stream.peek(); if (p != -1) { switch ((char)p) { case '=': in_stream.read(); return factory.create(TokenType.ShiftRightAssign, index, line, col, 3); case '>': in_stream.read(); int q = in_stream.peek(); if (q != -1 && q == '=') { in_stream.read(); return factory.create(TokenType.TripleRightShiftAssign, index, line, col, 4); } return factory.create(TokenType.ShiftRightUnsigned, index, line, col, 3); } } return factory.create(TokenType.ShiftRight, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.GreaterEqual, index, line, col, 2); } return factory.create(TokenType.GreaterThan, index, line, col); case '?': return factory.create(TokenType.Question, index, line, col); case '$': return factory.create(TokenType.Dollar, index, line, col); case ';': return factory.create(TokenType.Semicolon, index, line, col); case ':': return factory.create(TokenType.Colon, index, line, col); case ',': return factory.create(TokenType.Comma, index, line, col); case '.': if (next == '.') { in_stream.read(); int p = in_stream.peek(); if (p != -1 && p == '.') { in_stream.read(); return factory.create(TokenType.TripleDot, index, line, col, 3); } return factory.create(TokenType.DoubleDot, index, line, col, 2); } return factory.create(TokenType.Dot, index, line, col); case ')': return factory.create(TokenType.CloseParenthesis, index, line, col); case '(': return factory.create(TokenType.OpenParenthesis, index, line, col); case ']': return factory.create(TokenType.CloseSquareBracket, index, line, col); case '[': return factory.create(TokenType.OpenSquareBracket, index, line, col); case '}': return factory.create(TokenType.CloseCurlyBrace, index, line, col); case '{': return factory.create(TokenType.OpenCurlyBrace, index, line, col); case '#': return factory.create(TokenType.Hash, index, line, col); default: return null; } } LexOperator(TokenFactory factory, LexerStream in_stream); Token read(); }
@Test public void eleven() throws Exception { String str = "v=>11;"; LexerStream ls = new LexerStream(new StringReader(str)); LexOperator lex = new LexOperator(new BaseTokenFactory(), ls); ls.read(); Token tok = lex.read(); assertEquals(TokenType.GoesTo, tok.getType()); assertTrue(tok.getLine() == 1 && tok.getEndLine() == 1); assertEquals(2, tok.getCol()); assertEquals(4, tok.getEndCol()); }
public Token read() throws IOException, LexerException { int index = in_stream.getPosition(); int line = in_stream.getLine(); int col = in_stream.getCol(); int next = in_stream.peek(); if (next == -1) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Unexpected end of input stream when parsing operator"); } if (next == '.') { int tmp = in_stream.peek(2); if (tmp > 0 && Character.isDigit(tmp)) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Literal float encountered when operator '.' expected"); } } char cur = (char)in_stream.read(); next = in_stream.peek(); switch (cur) { case '+': switch (next) { case '+': in_stream.read(); return factory.create(TokenType.Increment, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.PlusAssign, index, line, col, 2); } return factory.create(TokenType.Plus, index, line, col); case '-': switch (next) { case '-': in_stream.read(); return factory.create(TokenType.Decrement, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.MinusAssign, index, line, col, 2); } return factory.create(TokenType.Minus, index, line, col); case '*': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.TimesAssign, index, line, col, 2); default: break; } return factory.create(TokenType.Times, index, line, col); case '/': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.DivAssign, index, line, col, 2); } return factory.create(TokenType.Div, index, line, col); case '%': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.ModAssign, index, line, col, 2); } return factory.create(TokenType.Mod, index, line, col); case '&': switch (next) { case '&': in_stream.read(); return factory.create(TokenType.LogicalAnd, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.BitwiseAndAssign, index, line, col, 2); } return factory.create(TokenType.BitwiseAnd, index, line, col); case '|': switch (next) { case '|': in_stream.read(); return factory.create(TokenType.LogicalOr, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.BitwiseOrAssign, index, line, col, 2); } return factory.create(TokenType.BitwiseOr, index, line, col); case '^': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.XorAssign, index, line, col, 2); case '^': in_stream.read(); if (in_stream.peek() == '=') { in_stream.read(); return factory.create(TokenType.PowAssign, index, line, col, 3); } return factory.create(TokenType.Pow, index, line, col, 2); } return factory.create(TokenType.Xor, index, line, col); case '!': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.NotEqual, index, line, col, 2); case '<': in_stream.read(); switch (in_stream.peek()) { case '=': in_stream.read(); return factory.create(TokenType.UnorderedOrGreater, index, line, col, 3); case '>': in_stream.read(); switch (in_stream.peek()) { case '=': in_stream.read(); return factory.create(TokenType.Unordered, index, line, col, 4); } return factory.create(TokenType.UnorderedOrEqual, index, line, col, 3); } return factory.create(TokenType.UnorderedGreaterOrEqual, index, line, col, 2); case '>': in_stream.read(); switch (in_stream.peek()) { case '=': in_stream.read(); return factory.create(TokenType.UnorderedOrLess, index, line, col, 3); default: break; } return factory.create(TokenType.UnorderedLessOrEqual, index, line, col, 2); } return factory.create(TokenType.Not, index, line, col); case '~': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.TildeAssign, index, line, col, 2); } return factory.create(TokenType.Tilde, index, line, col); case '=': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.Equal, index, line, col, 2); case '>': in_stream.read(); return factory.create(TokenType.GoesTo, index, line, col, 2); } return factory.create(TokenType.Assign, index, line, col); case '<': switch (next) { case '<': in_stream.read(); switch (in_stream.peek()) { case '=': in_stream.read(); return factory.create(TokenType.ShiftLeftAssign, index, line, col, 3); default: break; } return factory.create(TokenType.ShiftLeft, index, line, col, 2); case '>': in_stream.read(); switch (in_stream.peek()) { case '=': in_stream.read(); return factory.create(TokenType.LessEqualOrGreater, index, line, col, 3); default: break; } return factory.create(TokenType.LessOrGreater, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.LessEqual, index, line, col, 2); } return factory.create(TokenType.LessThan, index, line, col); case '>': switch (next) { case '>': in_stream.read(); int p = in_stream.peek(); if (p != -1) { switch ((char)p) { case '=': in_stream.read(); return factory.create(TokenType.ShiftRightAssign, index, line, col, 3); case '>': in_stream.read(); int q = in_stream.peek(); if (q != -1 && q == '=') { in_stream.read(); return factory.create(TokenType.TripleRightShiftAssign, index, line, col, 4); } return factory.create(TokenType.ShiftRightUnsigned, index, line, col, 3); } } return factory.create(TokenType.ShiftRight, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.GreaterEqual, index, line, col, 2); } return factory.create(TokenType.GreaterThan, index, line, col); case '?': return factory.create(TokenType.Question, index, line, col); case '$': return factory.create(TokenType.Dollar, index, line, col); case ';': return factory.create(TokenType.Semicolon, index, line, col); case ':': return factory.create(TokenType.Colon, index, line, col); case ',': return factory.create(TokenType.Comma, index, line, col); case '.': if (next == '.') { in_stream.read(); int p = in_stream.peek(); if (p != -1 && p == '.') { in_stream.read(); return factory.create(TokenType.TripleDot, index, line, col, 3); } return factory.create(TokenType.DoubleDot, index, line, col, 2); } return factory.create(TokenType.Dot, index, line, col); case ')': return factory.create(TokenType.CloseParenthesis, index, line, col); case '(': return factory.create(TokenType.OpenParenthesis, index, line, col); case ']': return factory.create(TokenType.CloseSquareBracket, index, line, col); case '[': return factory.create(TokenType.OpenSquareBracket, index, line, col); case '}': return factory.create(TokenType.CloseCurlyBrace, index, line, col); case '{': return factory.create(TokenType.OpenCurlyBrace, index, line, col); case '#': return factory.create(TokenType.Hash, index, line, col); default: return null; } }
LexOperator { public Token read() throws IOException, LexerException { int index = in_stream.getPosition(); int line = in_stream.getLine(); int col = in_stream.getCol(); int next = in_stream.peek(); if (next == -1) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Unexpected end of input stream when parsing operator"); } if (next == '.') { int tmp = in_stream.peek(2); if (tmp > 0 && Character.isDigit(tmp)) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Literal float encountered when operator '.' expected"); } } char cur = (char)in_stream.read(); next = in_stream.peek(); switch (cur) { case '+': switch (next) { case '+': in_stream.read(); return factory.create(TokenType.Increment, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.PlusAssign, index, line, col, 2); } return factory.create(TokenType.Plus, index, line, col); case '-': switch (next) { case '-': in_stream.read(); return factory.create(TokenType.Decrement, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.MinusAssign, index, line, col, 2); } return factory.create(TokenType.Minus, index, line, col); case '*': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.TimesAssign, index, line, col, 2); default: break; } return factory.create(TokenType.Times, index, line, col); case '/': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.DivAssign, index, line, col, 2); } return factory.create(TokenType.Div, index, line, col); case '%': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.ModAssign, index, line, col, 2); } return factory.create(TokenType.Mod, index, line, col); case '&': switch (next) { case '&': in_stream.read(); return factory.create(TokenType.LogicalAnd, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.BitwiseAndAssign, index, line, col, 2); } return factory.create(TokenType.BitwiseAnd, index, line, col); case '|': switch (next) { case '|': in_stream.read(); return factory.create(TokenType.LogicalOr, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.BitwiseOrAssign, index, line, col, 2); } return factory.create(TokenType.BitwiseOr, index, line, col); case '^': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.XorAssign, index, line, col, 2); case '^': in_stream.read(); if (in_stream.peek() == '=') { in_stream.read(); return factory.create(TokenType.PowAssign, index, line, col, 3); } return factory.create(TokenType.Pow, index, line, col, 2); } return factory.create(TokenType.Xor, index, line, col); case '!': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.NotEqual, index, line, col, 2); case '<': in_stream.read(); switch (in_stream.peek()) { case '=': in_stream.read(); return factory.create(TokenType.UnorderedOrGreater, index, line, col, 3); case '>': in_stream.read(); switch (in_stream.peek()) { case '=': in_stream.read(); return factory.create(TokenType.Unordered, index, line, col, 4); } return factory.create(TokenType.UnorderedOrEqual, index, line, col, 3); } return factory.create(TokenType.UnorderedGreaterOrEqual, index, line, col, 2); case '>': in_stream.read(); switch (in_stream.peek()) { case '=': in_stream.read(); return factory.create(TokenType.UnorderedOrLess, index, line, col, 3); default: break; } return factory.create(TokenType.UnorderedLessOrEqual, index, line, col, 2); } return factory.create(TokenType.Not, index, line, col); case '~': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.TildeAssign, index, line, col, 2); } return factory.create(TokenType.Tilde, index, line, col); case '=': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.Equal, index, line, col, 2); case '>': in_stream.read(); return factory.create(TokenType.GoesTo, index, line, col, 2); } return factory.create(TokenType.Assign, index, line, col); case '<': switch (next) { case '<': in_stream.read(); switch (in_stream.peek()) { case '=': in_stream.read(); return factory.create(TokenType.ShiftLeftAssign, index, line, col, 3); default: break; } return factory.create(TokenType.ShiftLeft, index, line, col, 2); case '>': in_stream.read(); switch (in_stream.peek()) { case '=': in_stream.read(); return factory.create(TokenType.LessEqualOrGreater, index, line, col, 3); default: break; } return factory.create(TokenType.LessOrGreater, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.LessEqual, index, line, col, 2); } return factory.create(TokenType.LessThan, index, line, col); case '>': switch (next) { case '>': in_stream.read(); int p = in_stream.peek(); if (p != -1) { switch ((char)p) { case '=': in_stream.read(); return factory.create(TokenType.ShiftRightAssign, index, line, col, 3); case '>': in_stream.read(); int q = in_stream.peek(); if (q != -1 && q == '=') { in_stream.read(); return factory.create(TokenType.TripleRightShiftAssign, index, line, col, 4); } return factory.create(TokenType.ShiftRightUnsigned, index, line, col, 3); } } return factory.create(TokenType.ShiftRight, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.GreaterEqual, index, line, col, 2); } return factory.create(TokenType.GreaterThan, index, line, col); case '?': return factory.create(TokenType.Question, index, line, col); case '$': return factory.create(TokenType.Dollar, index, line, col); case ';': return factory.create(TokenType.Semicolon, index, line, col); case ':': return factory.create(TokenType.Colon, index, line, col); case ',': return factory.create(TokenType.Comma, index, line, col); case '.': if (next == '.') { in_stream.read(); int p = in_stream.peek(); if (p != -1 && p == '.') { in_stream.read(); return factory.create(TokenType.TripleDot, index, line, col, 3); } return factory.create(TokenType.DoubleDot, index, line, col, 2); } return factory.create(TokenType.Dot, index, line, col); case ')': return factory.create(TokenType.CloseParenthesis, index, line, col); case '(': return factory.create(TokenType.OpenParenthesis, index, line, col); case ']': return factory.create(TokenType.CloseSquareBracket, index, line, col); case '[': return factory.create(TokenType.OpenSquareBracket, index, line, col); case '}': return factory.create(TokenType.CloseCurlyBrace, index, line, col); case '{': return factory.create(TokenType.OpenCurlyBrace, index, line, col); case '#': return factory.create(TokenType.Hash, index, line, col); default: return null; } } }
LexOperator { public Token read() throws IOException, LexerException { int index = in_stream.getPosition(); int line = in_stream.getLine(); int col = in_stream.getCol(); int next = in_stream.peek(); if (next == -1) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Unexpected end of input stream when parsing operator"); } if (next == '.') { int tmp = in_stream.peek(2); if (tmp > 0 && Character.isDigit(tmp)) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Literal float encountered when operator '.' expected"); } } char cur = (char)in_stream.read(); next = in_stream.peek(); switch (cur) { case '+': switch (next) { case '+': in_stream.read(); return factory.create(TokenType.Increment, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.PlusAssign, index, line, col, 2); } return factory.create(TokenType.Plus, index, line, col); case '-': switch (next) { case '-': in_stream.read(); return factory.create(TokenType.Decrement, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.MinusAssign, index, line, col, 2); } return factory.create(TokenType.Minus, index, line, col); case '*': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.TimesAssign, index, line, col, 2); default: break; } return factory.create(TokenType.Times, index, line, col); case '/': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.DivAssign, index, line, col, 2); } return factory.create(TokenType.Div, index, line, col); case '%': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.ModAssign, index, line, col, 2); } return factory.create(TokenType.Mod, index, line, col); case '&': switch (next) { case '&': in_stream.read(); return factory.create(TokenType.LogicalAnd, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.BitwiseAndAssign, index, line, col, 2); } return factory.create(TokenType.BitwiseAnd, index, line, col); case '|': switch (next) { case '|': in_stream.read(); return factory.create(TokenType.LogicalOr, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.BitwiseOrAssign, index, line, col, 2); } return factory.create(TokenType.BitwiseOr, index, line, col); case '^': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.XorAssign, index, line, col, 2); case '^': in_stream.read(); if (in_stream.peek() == '=') { in_stream.read(); return factory.create(TokenType.PowAssign, index, line, col, 3); } return factory.create(TokenType.Pow, index, line, col, 2); } return factory.create(TokenType.Xor, index, line, col); case '!': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.NotEqual, index, line, col, 2); case '<': in_stream.read(); switch (in_stream.peek()) { case '=': in_stream.read(); return factory.create(TokenType.UnorderedOrGreater, index, line, col, 3); case '>': in_stream.read(); switch (in_stream.peek()) { case '=': in_stream.read(); return factory.create(TokenType.Unordered, index, line, col, 4); } return factory.create(TokenType.UnorderedOrEqual, index, line, col, 3); } return factory.create(TokenType.UnorderedGreaterOrEqual, index, line, col, 2); case '>': in_stream.read(); switch (in_stream.peek()) { case '=': in_stream.read(); return factory.create(TokenType.UnorderedOrLess, index, line, col, 3); default: break; } return factory.create(TokenType.UnorderedLessOrEqual, index, line, col, 2); } return factory.create(TokenType.Not, index, line, col); case '~': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.TildeAssign, index, line, col, 2); } return factory.create(TokenType.Tilde, index, line, col); case '=': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.Equal, index, line, col, 2); case '>': in_stream.read(); return factory.create(TokenType.GoesTo, index, line, col, 2); } return factory.create(TokenType.Assign, index, line, col); case '<': switch (next) { case '<': in_stream.read(); switch (in_stream.peek()) { case '=': in_stream.read(); return factory.create(TokenType.ShiftLeftAssign, index, line, col, 3); default: break; } return factory.create(TokenType.ShiftLeft, index, line, col, 2); case '>': in_stream.read(); switch (in_stream.peek()) { case '=': in_stream.read(); return factory.create(TokenType.LessEqualOrGreater, index, line, col, 3); default: break; } return factory.create(TokenType.LessOrGreater, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.LessEqual, index, line, col, 2); } return factory.create(TokenType.LessThan, index, line, col); case '>': switch (next) { case '>': in_stream.read(); int p = in_stream.peek(); if (p != -1) { switch ((char)p) { case '=': in_stream.read(); return factory.create(TokenType.ShiftRightAssign, index, line, col, 3); case '>': in_stream.read(); int q = in_stream.peek(); if (q != -1 && q == '=') { in_stream.read(); return factory.create(TokenType.TripleRightShiftAssign, index, line, col, 4); } return factory.create(TokenType.ShiftRightUnsigned, index, line, col, 3); } } return factory.create(TokenType.ShiftRight, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.GreaterEqual, index, line, col, 2); } return factory.create(TokenType.GreaterThan, index, line, col); case '?': return factory.create(TokenType.Question, index, line, col); case '$': return factory.create(TokenType.Dollar, index, line, col); case ';': return factory.create(TokenType.Semicolon, index, line, col); case ':': return factory.create(TokenType.Colon, index, line, col); case ',': return factory.create(TokenType.Comma, index, line, col); case '.': if (next == '.') { in_stream.read(); int p = in_stream.peek(); if (p != -1 && p == '.') { in_stream.read(); return factory.create(TokenType.TripleDot, index, line, col, 3); } return factory.create(TokenType.DoubleDot, index, line, col, 2); } return factory.create(TokenType.Dot, index, line, col); case ')': return factory.create(TokenType.CloseParenthesis, index, line, col); case '(': return factory.create(TokenType.OpenParenthesis, index, line, col); case ']': return factory.create(TokenType.CloseSquareBracket, index, line, col); case '[': return factory.create(TokenType.OpenSquareBracket, index, line, col); case '}': return factory.create(TokenType.CloseCurlyBrace, index, line, col); case '{': return factory.create(TokenType.OpenCurlyBrace, index, line, col); case '#': return factory.create(TokenType.Hash, index, line, col); default: return null; } } LexOperator(TokenFactory factory, LexerStream in_stream); }
LexOperator { public Token read() throws IOException, LexerException { int index = in_stream.getPosition(); int line = in_stream.getLine(); int col = in_stream.getCol(); int next = in_stream.peek(); if (next == -1) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Unexpected end of input stream when parsing operator"); } if (next == '.') { int tmp = in_stream.peek(2); if (tmp > 0 && Character.isDigit(tmp)) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Literal float encountered when operator '.' expected"); } } char cur = (char)in_stream.read(); next = in_stream.peek(); switch (cur) { case '+': switch (next) { case '+': in_stream.read(); return factory.create(TokenType.Increment, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.PlusAssign, index, line, col, 2); } return factory.create(TokenType.Plus, index, line, col); case '-': switch (next) { case '-': in_stream.read(); return factory.create(TokenType.Decrement, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.MinusAssign, index, line, col, 2); } return factory.create(TokenType.Minus, index, line, col); case '*': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.TimesAssign, index, line, col, 2); default: break; } return factory.create(TokenType.Times, index, line, col); case '/': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.DivAssign, index, line, col, 2); } return factory.create(TokenType.Div, index, line, col); case '%': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.ModAssign, index, line, col, 2); } return factory.create(TokenType.Mod, index, line, col); case '&': switch (next) { case '&': in_stream.read(); return factory.create(TokenType.LogicalAnd, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.BitwiseAndAssign, index, line, col, 2); } return factory.create(TokenType.BitwiseAnd, index, line, col); case '|': switch (next) { case '|': in_stream.read(); return factory.create(TokenType.LogicalOr, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.BitwiseOrAssign, index, line, col, 2); } return factory.create(TokenType.BitwiseOr, index, line, col); case '^': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.XorAssign, index, line, col, 2); case '^': in_stream.read(); if (in_stream.peek() == '=') { in_stream.read(); return factory.create(TokenType.PowAssign, index, line, col, 3); } return factory.create(TokenType.Pow, index, line, col, 2); } return factory.create(TokenType.Xor, index, line, col); case '!': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.NotEqual, index, line, col, 2); case '<': in_stream.read(); switch (in_stream.peek()) { case '=': in_stream.read(); return factory.create(TokenType.UnorderedOrGreater, index, line, col, 3); case '>': in_stream.read(); switch (in_stream.peek()) { case '=': in_stream.read(); return factory.create(TokenType.Unordered, index, line, col, 4); } return factory.create(TokenType.UnorderedOrEqual, index, line, col, 3); } return factory.create(TokenType.UnorderedGreaterOrEqual, index, line, col, 2); case '>': in_stream.read(); switch (in_stream.peek()) { case '=': in_stream.read(); return factory.create(TokenType.UnorderedOrLess, index, line, col, 3); default: break; } return factory.create(TokenType.UnorderedLessOrEqual, index, line, col, 2); } return factory.create(TokenType.Not, index, line, col); case '~': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.TildeAssign, index, line, col, 2); } return factory.create(TokenType.Tilde, index, line, col); case '=': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.Equal, index, line, col, 2); case '>': in_stream.read(); return factory.create(TokenType.GoesTo, index, line, col, 2); } return factory.create(TokenType.Assign, index, line, col); case '<': switch (next) { case '<': in_stream.read(); switch (in_stream.peek()) { case '=': in_stream.read(); return factory.create(TokenType.ShiftLeftAssign, index, line, col, 3); default: break; } return factory.create(TokenType.ShiftLeft, index, line, col, 2); case '>': in_stream.read(); switch (in_stream.peek()) { case '=': in_stream.read(); return factory.create(TokenType.LessEqualOrGreater, index, line, col, 3); default: break; } return factory.create(TokenType.LessOrGreater, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.LessEqual, index, line, col, 2); } return factory.create(TokenType.LessThan, index, line, col); case '>': switch (next) { case '>': in_stream.read(); int p = in_stream.peek(); if (p != -1) { switch ((char)p) { case '=': in_stream.read(); return factory.create(TokenType.ShiftRightAssign, index, line, col, 3); case '>': in_stream.read(); int q = in_stream.peek(); if (q != -1 && q == '=') { in_stream.read(); return factory.create(TokenType.TripleRightShiftAssign, index, line, col, 4); } return factory.create(TokenType.ShiftRightUnsigned, index, line, col, 3); } } return factory.create(TokenType.ShiftRight, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.GreaterEqual, index, line, col, 2); } return factory.create(TokenType.GreaterThan, index, line, col); case '?': return factory.create(TokenType.Question, index, line, col); case '$': return factory.create(TokenType.Dollar, index, line, col); case ';': return factory.create(TokenType.Semicolon, index, line, col); case ':': return factory.create(TokenType.Colon, index, line, col); case ',': return factory.create(TokenType.Comma, index, line, col); case '.': if (next == '.') { in_stream.read(); int p = in_stream.peek(); if (p != -1 && p == '.') { in_stream.read(); return factory.create(TokenType.TripleDot, index, line, col, 3); } return factory.create(TokenType.DoubleDot, index, line, col, 2); } return factory.create(TokenType.Dot, index, line, col); case ')': return factory.create(TokenType.CloseParenthesis, index, line, col); case '(': return factory.create(TokenType.OpenParenthesis, index, line, col); case ']': return factory.create(TokenType.CloseSquareBracket, index, line, col); case '[': return factory.create(TokenType.OpenSquareBracket, index, line, col); case '}': return factory.create(TokenType.CloseCurlyBrace, index, line, col); case '{': return factory.create(TokenType.OpenCurlyBrace, index, line, col); case '#': return factory.create(TokenType.Hash, index, line, col); default: return null; } } LexOperator(TokenFactory factory, LexerStream in_stream); Token read(); }
LexOperator { public Token read() throws IOException, LexerException { int index = in_stream.getPosition(); int line = in_stream.getLine(); int col = in_stream.getCol(); int next = in_stream.peek(); if (next == -1) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Unexpected end of input stream when parsing operator"); } if (next == '.') { int tmp = in_stream.peek(2); if (tmp > 0 && Character.isDigit(tmp)) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Literal float encountered when operator '.' expected"); } } char cur = (char)in_stream.read(); next = in_stream.peek(); switch (cur) { case '+': switch (next) { case '+': in_stream.read(); return factory.create(TokenType.Increment, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.PlusAssign, index, line, col, 2); } return factory.create(TokenType.Plus, index, line, col); case '-': switch (next) { case '-': in_stream.read(); return factory.create(TokenType.Decrement, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.MinusAssign, index, line, col, 2); } return factory.create(TokenType.Minus, index, line, col); case '*': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.TimesAssign, index, line, col, 2); default: break; } return factory.create(TokenType.Times, index, line, col); case '/': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.DivAssign, index, line, col, 2); } return factory.create(TokenType.Div, index, line, col); case '%': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.ModAssign, index, line, col, 2); } return factory.create(TokenType.Mod, index, line, col); case '&': switch (next) { case '&': in_stream.read(); return factory.create(TokenType.LogicalAnd, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.BitwiseAndAssign, index, line, col, 2); } return factory.create(TokenType.BitwiseAnd, index, line, col); case '|': switch (next) { case '|': in_stream.read(); return factory.create(TokenType.LogicalOr, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.BitwiseOrAssign, index, line, col, 2); } return factory.create(TokenType.BitwiseOr, index, line, col); case '^': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.XorAssign, index, line, col, 2); case '^': in_stream.read(); if (in_stream.peek() == '=') { in_stream.read(); return factory.create(TokenType.PowAssign, index, line, col, 3); } return factory.create(TokenType.Pow, index, line, col, 2); } return factory.create(TokenType.Xor, index, line, col); case '!': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.NotEqual, index, line, col, 2); case '<': in_stream.read(); switch (in_stream.peek()) { case '=': in_stream.read(); return factory.create(TokenType.UnorderedOrGreater, index, line, col, 3); case '>': in_stream.read(); switch (in_stream.peek()) { case '=': in_stream.read(); return factory.create(TokenType.Unordered, index, line, col, 4); } return factory.create(TokenType.UnorderedOrEqual, index, line, col, 3); } return factory.create(TokenType.UnorderedGreaterOrEqual, index, line, col, 2); case '>': in_stream.read(); switch (in_stream.peek()) { case '=': in_stream.read(); return factory.create(TokenType.UnorderedOrLess, index, line, col, 3); default: break; } return factory.create(TokenType.UnorderedLessOrEqual, index, line, col, 2); } return factory.create(TokenType.Not, index, line, col); case '~': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.TildeAssign, index, line, col, 2); } return factory.create(TokenType.Tilde, index, line, col); case '=': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.Equal, index, line, col, 2); case '>': in_stream.read(); return factory.create(TokenType.GoesTo, index, line, col, 2); } return factory.create(TokenType.Assign, index, line, col); case '<': switch (next) { case '<': in_stream.read(); switch (in_stream.peek()) { case '=': in_stream.read(); return factory.create(TokenType.ShiftLeftAssign, index, line, col, 3); default: break; } return factory.create(TokenType.ShiftLeft, index, line, col, 2); case '>': in_stream.read(); switch (in_stream.peek()) { case '=': in_stream.read(); return factory.create(TokenType.LessEqualOrGreater, index, line, col, 3); default: break; } return factory.create(TokenType.LessOrGreater, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.LessEqual, index, line, col, 2); } return factory.create(TokenType.LessThan, index, line, col); case '>': switch (next) { case '>': in_stream.read(); int p = in_stream.peek(); if (p != -1) { switch ((char)p) { case '=': in_stream.read(); return factory.create(TokenType.ShiftRightAssign, index, line, col, 3); case '>': in_stream.read(); int q = in_stream.peek(); if (q != -1 && q == '=') { in_stream.read(); return factory.create(TokenType.TripleRightShiftAssign, index, line, col, 4); } return factory.create(TokenType.ShiftRightUnsigned, index, line, col, 3); } } return factory.create(TokenType.ShiftRight, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.GreaterEqual, index, line, col, 2); } return factory.create(TokenType.GreaterThan, index, line, col); case '?': return factory.create(TokenType.Question, index, line, col); case '$': return factory.create(TokenType.Dollar, index, line, col); case ';': return factory.create(TokenType.Semicolon, index, line, col); case ':': return factory.create(TokenType.Colon, index, line, col); case ',': return factory.create(TokenType.Comma, index, line, col); case '.': if (next == '.') { in_stream.read(); int p = in_stream.peek(); if (p != -1 && p == '.') { in_stream.read(); return factory.create(TokenType.TripleDot, index, line, col, 3); } return factory.create(TokenType.DoubleDot, index, line, col, 2); } return factory.create(TokenType.Dot, index, line, col); case ')': return factory.create(TokenType.CloseParenthesis, index, line, col); case '(': return factory.create(TokenType.OpenParenthesis, index, line, col); case ']': return factory.create(TokenType.CloseSquareBracket, index, line, col); case '[': return factory.create(TokenType.OpenSquareBracket, index, line, col); case '}': return factory.create(TokenType.CloseCurlyBrace, index, line, col); case '{': return factory.create(TokenType.OpenCurlyBrace, index, line, col); case '#': return factory.create(TokenType.Hash, index, line, col); default: return null; } } LexOperator(TokenFactory factory, LexerStream in_stream); Token read(); }
@Test public void shift() throws Exception { String str = "a<<b>>c<<=d>>=e"; LexerStream ls = new LexerStream(new StringReader(str)); LexOperator lex = new LexOperator(new BaseTokenFactory(), ls); ls.read(); Token tok = lex.read(); assertEquals(TokenType.ShiftLeft, tok.getType()); assertTrue(tok.getLine() == 1 && tok.getEndLine() == 1); assertEquals(2, tok.getCol()); assertEquals(4, tok.getEndCol()); ls.read(); tok = lex.read(); assertEquals(TokenType.ShiftRight, tok.getType()); assertTrue(tok.getLine() == 1 && tok.getEndLine() == 1); assertEquals(5, tok.getCol()); assertEquals(7, tok.getEndCol()); ls.read(); tok = lex.read(); assertEquals(TokenType.ShiftLeftAssign, tok.getType()); assertTrue(tok.getLine() == 1 && tok.getEndLine() == 1); assertEquals(8, tok.getCol()); assertEquals(11, tok.getEndCol()); ls.read(); tok = lex.read(); assertEquals(TokenType.ShiftRightAssign, tok.getType()); assertTrue(tok.getLine() == 1 && tok.getEndLine() == 1); assertEquals(12, tok.getCol()); assertEquals(15, tok.getEndCol()); }
public Token read() throws IOException, LexerException { int index = in_stream.getPosition(); int line = in_stream.getLine(); int col = in_stream.getCol(); int next = in_stream.peek(); if (next == -1) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Unexpected end of input stream when parsing operator"); } if (next == '.') { int tmp = in_stream.peek(2); if (tmp > 0 && Character.isDigit(tmp)) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Literal float encountered when operator '.' expected"); } } char cur = (char)in_stream.read(); next = in_stream.peek(); switch (cur) { case '+': switch (next) { case '+': in_stream.read(); return factory.create(TokenType.Increment, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.PlusAssign, index, line, col, 2); } return factory.create(TokenType.Plus, index, line, col); case '-': switch (next) { case '-': in_stream.read(); return factory.create(TokenType.Decrement, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.MinusAssign, index, line, col, 2); } return factory.create(TokenType.Minus, index, line, col); case '*': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.TimesAssign, index, line, col, 2); default: break; } return factory.create(TokenType.Times, index, line, col); case '/': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.DivAssign, index, line, col, 2); } return factory.create(TokenType.Div, index, line, col); case '%': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.ModAssign, index, line, col, 2); } return factory.create(TokenType.Mod, index, line, col); case '&': switch (next) { case '&': in_stream.read(); return factory.create(TokenType.LogicalAnd, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.BitwiseAndAssign, index, line, col, 2); } return factory.create(TokenType.BitwiseAnd, index, line, col); case '|': switch (next) { case '|': in_stream.read(); return factory.create(TokenType.LogicalOr, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.BitwiseOrAssign, index, line, col, 2); } return factory.create(TokenType.BitwiseOr, index, line, col); case '^': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.XorAssign, index, line, col, 2); case '^': in_stream.read(); if (in_stream.peek() == '=') { in_stream.read(); return factory.create(TokenType.PowAssign, index, line, col, 3); } return factory.create(TokenType.Pow, index, line, col, 2); } return factory.create(TokenType.Xor, index, line, col); case '!': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.NotEqual, index, line, col, 2); case '<': in_stream.read(); switch (in_stream.peek()) { case '=': in_stream.read(); return factory.create(TokenType.UnorderedOrGreater, index, line, col, 3); case '>': in_stream.read(); switch (in_stream.peek()) { case '=': in_stream.read(); return factory.create(TokenType.Unordered, index, line, col, 4); } return factory.create(TokenType.UnorderedOrEqual, index, line, col, 3); } return factory.create(TokenType.UnorderedGreaterOrEqual, index, line, col, 2); case '>': in_stream.read(); switch (in_stream.peek()) { case '=': in_stream.read(); return factory.create(TokenType.UnorderedOrLess, index, line, col, 3); default: break; } return factory.create(TokenType.UnorderedLessOrEqual, index, line, col, 2); } return factory.create(TokenType.Not, index, line, col); case '~': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.TildeAssign, index, line, col, 2); } return factory.create(TokenType.Tilde, index, line, col); case '=': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.Equal, index, line, col, 2); case '>': in_stream.read(); return factory.create(TokenType.GoesTo, index, line, col, 2); } return factory.create(TokenType.Assign, index, line, col); case '<': switch (next) { case '<': in_stream.read(); switch (in_stream.peek()) { case '=': in_stream.read(); return factory.create(TokenType.ShiftLeftAssign, index, line, col, 3); default: break; } return factory.create(TokenType.ShiftLeft, index, line, col, 2); case '>': in_stream.read(); switch (in_stream.peek()) { case '=': in_stream.read(); return factory.create(TokenType.LessEqualOrGreater, index, line, col, 3); default: break; } return factory.create(TokenType.LessOrGreater, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.LessEqual, index, line, col, 2); } return factory.create(TokenType.LessThan, index, line, col); case '>': switch (next) { case '>': in_stream.read(); int p = in_stream.peek(); if (p != -1) { switch ((char)p) { case '=': in_stream.read(); return factory.create(TokenType.ShiftRightAssign, index, line, col, 3); case '>': in_stream.read(); int q = in_stream.peek(); if (q != -1 && q == '=') { in_stream.read(); return factory.create(TokenType.TripleRightShiftAssign, index, line, col, 4); } return factory.create(TokenType.ShiftRightUnsigned, index, line, col, 3); } } return factory.create(TokenType.ShiftRight, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.GreaterEqual, index, line, col, 2); } return factory.create(TokenType.GreaterThan, index, line, col); case '?': return factory.create(TokenType.Question, index, line, col); case '$': return factory.create(TokenType.Dollar, index, line, col); case ';': return factory.create(TokenType.Semicolon, index, line, col); case ':': return factory.create(TokenType.Colon, index, line, col); case ',': return factory.create(TokenType.Comma, index, line, col); case '.': if (next == '.') { in_stream.read(); int p = in_stream.peek(); if (p != -1 && p == '.') { in_stream.read(); return factory.create(TokenType.TripleDot, index, line, col, 3); } return factory.create(TokenType.DoubleDot, index, line, col, 2); } return factory.create(TokenType.Dot, index, line, col); case ')': return factory.create(TokenType.CloseParenthesis, index, line, col); case '(': return factory.create(TokenType.OpenParenthesis, index, line, col); case ']': return factory.create(TokenType.CloseSquareBracket, index, line, col); case '[': return factory.create(TokenType.OpenSquareBracket, index, line, col); case '}': return factory.create(TokenType.CloseCurlyBrace, index, line, col); case '{': return factory.create(TokenType.OpenCurlyBrace, index, line, col); case '#': return factory.create(TokenType.Hash, index, line, col); default: return null; } }
LexOperator { public Token read() throws IOException, LexerException { int index = in_stream.getPosition(); int line = in_stream.getLine(); int col = in_stream.getCol(); int next = in_stream.peek(); if (next == -1) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Unexpected end of input stream when parsing operator"); } if (next == '.') { int tmp = in_stream.peek(2); if (tmp > 0 && Character.isDigit(tmp)) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Literal float encountered when operator '.' expected"); } } char cur = (char)in_stream.read(); next = in_stream.peek(); switch (cur) { case '+': switch (next) { case '+': in_stream.read(); return factory.create(TokenType.Increment, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.PlusAssign, index, line, col, 2); } return factory.create(TokenType.Plus, index, line, col); case '-': switch (next) { case '-': in_stream.read(); return factory.create(TokenType.Decrement, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.MinusAssign, index, line, col, 2); } return factory.create(TokenType.Minus, index, line, col); case '*': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.TimesAssign, index, line, col, 2); default: break; } return factory.create(TokenType.Times, index, line, col); case '/': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.DivAssign, index, line, col, 2); } return factory.create(TokenType.Div, index, line, col); case '%': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.ModAssign, index, line, col, 2); } return factory.create(TokenType.Mod, index, line, col); case '&': switch (next) { case '&': in_stream.read(); return factory.create(TokenType.LogicalAnd, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.BitwiseAndAssign, index, line, col, 2); } return factory.create(TokenType.BitwiseAnd, index, line, col); case '|': switch (next) { case '|': in_stream.read(); return factory.create(TokenType.LogicalOr, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.BitwiseOrAssign, index, line, col, 2); } return factory.create(TokenType.BitwiseOr, index, line, col); case '^': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.XorAssign, index, line, col, 2); case '^': in_stream.read(); if (in_stream.peek() == '=') { in_stream.read(); return factory.create(TokenType.PowAssign, index, line, col, 3); } return factory.create(TokenType.Pow, index, line, col, 2); } return factory.create(TokenType.Xor, index, line, col); case '!': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.NotEqual, index, line, col, 2); case '<': in_stream.read(); switch (in_stream.peek()) { case '=': in_stream.read(); return factory.create(TokenType.UnorderedOrGreater, index, line, col, 3); case '>': in_stream.read(); switch (in_stream.peek()) { case '=': in_stream.read(); return factory.create(TokenType.Unordered, index, line, col, 4); } return factory.create(TokenType.UnorderedOrEqual, index, line, col, 3); } return factory.create(TokenType.UnorderedGreaterOrEqual, index, line, col, 2); case '>': in_stream.read(); switch (in_stream.peek()) { case '=': in_stream.read(); return factory.create(TokenType.UnorderedOrLess, index, line, col, 3); default: break; } return factory.create(TokenType.UnorderedLessOrEqual, index, line, col, 2); } return factory.create(TokenType.Not, index, line, col); case '~': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.TildeAssign, index, line, col, 2); } return factory.create(TokenType.Tilde, index, line, col); case '=': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.Equal, index, line, col, 2); case '>': in_stream.read(); return factory.create(TokenType.GoesTo, index, line, col, 2); } return factory.create(TokenType.Assign, index, line, col); case '<': switch (next) { case '<': in_stream.read(); switch (in_stream.peek()) { case '=': in_stream.read(); return factory.create(TokenType.ShiftLeftAssign, index, line, col, 3); default: break; } return factory.create(TokenType.ShiftLeft, index, line, col, 2); case '>': in_stream.read(); switch (in_stream.peek()) { case '=': in_stream.read(); return factory.create(TokenType.LessEqualOrGreater, index, line, col, 3); default: break; } return factory.create(TokenType.LessOrGreater, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.LessEqual, index, line, col, 2); } return factory.create(TokenType.LessThan, index, line, col); case '>': switch (next) { case '>': in_stream.read(); int p = in_stream.peek(); if (p != -1) { switch ((char)p) { case '=': in_stream.read(); return factory.create(TokenType.ShiftRightAssign, index, line, col, 3); case '>': in_stream.read(); int q = in_stream.peek(); if (q != -1 && q == '=') { in_stream.read(); return factory.create(TokenType.TripleRightShiftAssign, index, line, col, 4); } return factory.create(TokenType.ShiftRightUnsigned, index, line, col, 3); } } return factory.create(TokenType.ShiftRight, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.GreaterEqual, index, line, col, 2); } return factory.create(TokenType.GreaterThan, index, line, col); case '?': return factory.create(TokenType.Question, index, line, col); case '$': return factory.create(TokenType.Dollar, index, line, col); case ';': return factory.create(TokenType.Semicolon, index, line, col); case ':': return factory.create(TokenType.Colon, index, line, col); case ',': return factory.create(TokenType.Comma, index, line, col); case '.': if (next == '.') { in_stream.read(); int p = in_stream.peek(); if (p != -1 && p == '.') { in_stream.read(); return factory.create(TokenType.TripleDot, index, line, col, 3); } return factory.create(TokenType.DoubleDot, index, line, col, 2); } return factory.create(TokenType.Dot, index, line, col); case ')': return factory.create(TokenType.CloseParenthesis, index, line, col); case '(': return factory.create(TokenType.OpenParenthesis, index, line, col); case ']': return factory.create(TokenType.CloseSquareBracket, index, line, col); case '[': return factory.create(TokenType.OpenSquareBracket, index, line, col); case '}': return factory.create(TokenType.CloseCurlyBrace, index, line, col); case '{': return factory.create(TokenType.OpenCurlyBrace, index, line, col); case '#': return factory.create(TokenType.Hash, index, line, col); default: return null; } } }
LexOperator { public Token read() throws IOException, LexerException { int index = in_stream.getPosition(); int line = in_stream.getLine(); int col = in_stream.getCol(); int next = in_stream.peek(); if (next == -1) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Unexpected end of input stream when parsing operator"); } if (next == '.') { int tmp = in_stream.peek(2); if (tmp > 0 && Character.isDigit(tmp)) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Literal float encountered when operator '.' expected"); } } char cur = (char)in_stream.read(); next = in_stream.peek(); switch (cur) { case '+': switch (next) { case '+': in_stream.read(); return factory.create(TokenType.Increment, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.PlusAssign, index, line, col, 2); } return factory.create(TokenType.Plus, index, line, col); case '-': switch (next) { case '-': in_stream.read(); return factory.create(TokenType.Decrement, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.MinusAssign, index, line, col, 2); } return factory.create(TokenType.Minus, index, line, col); case '*': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.TimesAssign, index, line, col, 2); default: break; } return factory.create(TokenType.Times, index, line, col); case '/': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.DivAssign, index, line, col, 2); } return factory.create(TokenType.Div, index, line, col); case '%': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.ModAssign, index, line, col, 2); } return factory.create(TokenType.Mod, index, line, col); case '&': switch (next) { case '&': in_stream.read(); return factory.create(TokenType.LogicalAnd, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.BitwiseAndAssign, index, line, col, 2); } return factory.create(TokenType.BitwiseAnd, index, line, col); case '|': switch (next) { case '|': in_stream.read(); return factory.create(TokenType.LogicalOr, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.BitwiseOrAssign, index, line, col, 2); } return factory.create(TokenType.BitwiseOr, index, line, col); case '^': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.XorAssign, index, line, col, 2); case '^': in_stream.read(); if (in_stream.peek() == '=') { in_stream.read(); return factory.create(TokenType.PowAssign, index, line, col, 3); } return factory.create(TokenType.Pow, index, line, col, 2); } return factory.create(TokenType.Xor, index, line, col); case '!': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.NotEqual, index, line, col, 2); case '<': in_stream.read(); switch (in_stream.peek()) { case '=': in_stream.read(); return factory.create(TokenType.UnorderedOrGreater, index, line, col, 3); case '>': in_stream.read(); switch (in_stream.peek()) { case '=': in_stream.read(); return factory.create(TokenType.Unordered, index, line, col, 4); } return factory.create(TokenType.UnorderedOrEqual, index, line, col, 3); } return factory.create(TokenType.UnorderedGreaterOrEqual, index, line, col, 2); case '>': in_stream.read(); switch (in_stream.peek()) { case '=': in_stream.read(); return factory.create(TokenType.UnorderedOrLess, index, line, col, 3); default: break; } return factory.create(TokenType.UnorderedLessOrEqual, index, line, col, 2); } return factory.create(TokenType.Not, index, line, col); case '~': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.TildeAssign, index, line, col, 2); } return factory.create(TokenType.Tilde, index, line, col); case '=': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.Equal, index, line, col, 2); case '>': in_stream.read(); return factory.create(TokenType.GoesTo, index, line, col, 2); } return factory.create(TokenType.Assign, index, line, col); case '<': switch (next) { case '<': in_stream.read(); switch (in_stream.peek()) { case '=': in_stream.read(); return factory.create(TokenType.ShiftLeftAssign, index, line, col, 3); default: break; } return factory.create(TokenType.ShiftLeft, index, line, col, 2); case '>': in_stream.read(); switch (in_stream.peek()) { case '=': in_stream.read(); return factory.create(TokenType.LessEqualOrGreater, index, line, col, 3); default: break; } return factory.create(TokenType.LessOrGreater, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.LessEqual, index, line, col, 2); } return factory.create(TokenType.LessThan, index, line, col); case '>': switch (next) { case '>': in_stream.read(); int p = in_stream.peek(); if (p != -1) { switch ((char)p) { case '=': in_stream.read(); return factory.create(TokenType.ShiftRightAssign, index, line, col, 3); case '>': in_stream.read(); int q = in_stream.peek(); if (q != -1 && q == '=') { in_stream.read(); return factory.create(TokenType.TripleRightShiftAssign, index, line, col, 4); } return factory.create(TokenType.ShiftRightUnsigned, index, line, col, 3); } } return factory.create(TokenType.ShiftRight, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.GreaterEqual, index, line, col, 2); } return factory.create(TokenType.GreaterThan, index, line, col); case '?': return factory.create(TokenType.Question, index, line, col); case '$': return factory.create(TokenType.Dollar, index, line, col); case ';': return factory.create(TokenType.Semicolon, index, line, col); case ':': return factory.create(TokenType.Colon, index, line, col); case ',': return factory.create(TokenType.Comma, index, line, col); case '.': if (next == '.') { in_stream.read(); int p = in_stream.peek(); if (p != -1 && p == '.') { in_stream.read(); return factory.create(TokenType.TripleDot, index, line, col, 3); } return factory.create(TokenType.DoubleDot, index, line, col, 2); } return factory.create(TokenType.Dot, index, line, col); case ')': return factory.create(TokenType.CloseParenthesis, index, line, col); case '(': return factory.create(TokenType.OpenParenthesis, index, line, col); case ']': return factory.create(TokenType.CloseSquareBracket, index, line, col); case '[': return factory.create(TokenType.OpenSquareBracket, index, line, col); case '}': return factory.create(TokenType.CloseCurlyBrace, index, line, col); case '{': return factory.create(TokenType.OpenCurlyBrace, index, line, col); case '#': return factory.create(TokenType.Hash, index, line, col); default: return null; } } LexOperator(TokenFactory factory, LexerStream in_stream); }
LexOperator { public Token read() throws IOException, LexerException { int index = in_stream.getPosition(); int line = in_stream.getLine(); int col = in_stream.getCol(); int next = in_stream.peek(); if (next == -1) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Unexpected end of input stream when parsing operator"); } if (next == '.') { int tmp = in_stream.peek(2); if (tmp > 0 && Character.isDigit(tmp)) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Literal float encountered when operator '.' expected"); } } char cur = (char)in_stream.read(); next = in_stream.peek(); switch (cur) { case '+': switch (next) { case '+': in_stream.read(); return factory.create(TokenType.Increment, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.PlusAssign, index, line, col, 2); } return factory.create(TokenType.Plus, index, line, col); case '-': switch (next) { case '-': in_stream.read(); return factory.create(TokenType.Decrement, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.MinusAssign, index, line, col, 2); } return factory.create(TokenType.Minus, index, line, col); case '*': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.TimesAssign, index, line, col, 2); default: break; } return factory.create(TokenType.Times, index, line, col); case '/': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.DivAssign, index, line, col, 2); } return factory.create(TokenType.Div, index, line, col); case '%': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.ModAssign, index, line, col, 2); } return factory.create(TokenType.Mod, index, line, col); case '&': switch (next) { case '&': in_stream.read(); return factory.create(TokenType.LogicalAnd, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.BitwiseAndAssign, index, line, col, 2); } return factory.create(TokenType.BitwiseAnd, index, line, col); case '|': switch (next) { case '|': in_stream.read(); return factory.create(TokenType.LogicalOr, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.BitwiseOrAssign, index, line, col, 2); } return factory.create(TokenType.BitwiseOr, index, line, col); case '^': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.XorAssign, index, line, col, 2); case '^': in_stream.read(); if (in_stream.peek() == '=') { in_stream.read(); return factory.create(TokenType.PowAssign, index, line, col, 3); } return factory.create(TokenType.Pow, index, line, col, 2); } return factory.create(TokenType.Xor, index, line, col); case '!': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.NotEqual, index, line, col, 2); case '<': in_stream.read(); switch (in_stream.peek()) { case '=': in_stream.read(); return factory.create(TokenType.UnorderedOrGreater, index, line, col, 3); case '>': in_stream.read(); switch (in_stream.peek()) { case '=': in_stream.read(); return factory.create(TokenType.Unordered, index, line, col, 4); } return factory.create(TokenType.UnorderedOrEqual, index, line, col, 3); } return factory.create(TokenType.UnorderedGreaterOrEqual, index, line, col, 2); case '>': in_stream.read(); switch (in_stream.peek()) { case '=': in_stream.read(); return factory.create(TokenType.UnorderedOrLess, index, line, col, 3); default: break; } return factory.create(TokenType.UnorderedLessOrEqual, index, line, col, 2); } return factory.create(TokenType.Not, index, line, col); case '~': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.TildeAssign, index, line, col, 2); } return factory.create(TokenType.Tilde, index, line, col); case '=': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.Equal, index, line, col, 2); case '>': in_stream.read(); return factory.create(TokenType.GoesTo, index, line, col, 2); } return factory.create(TokenType.Assign, index, line, col); case '<': switch (next) { case '<': in_stream.read(); switch (in_stream.peek()) { case '=': in_stream.read(); return factory.create(TokenType.ShiftLeftAssign, index, line, col, 3); default: break; } return factory.create(TokenType.ShiftLeft, index, line, col, 2); case '>': in_stream.read(); switch (in_stream.peek()) { case '=': in_stream.read(); return factory.create(TokenType.LessEqualOrGreater, index, line, col, 3); default: break; } return factory.create(TokenType.LessOrGreater, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.LessEqual, index, line, col, 2); } return factory.create(TokenType.LessThan, index, line, col); case '>': switch (next) { case '>': in_stream.read(); int p = in_stream.peek(); if (p != -1) { switch ((char)p) { case '=': in_stream.read(); return factory.create(TokenType.ShiftRightAssign, index, line, col, 3); case '>': in_stream.read(); int q = in_stream.peek(); if (q != -1 && q == '=') { in_stream.read(); return factory.create(TokenType.TripleRightShiftAssign, index, line, col, 4); } return factory.create(TokenType.ShiftRightUnsigned, index, line, col, 3); } } return factory.create(TokenType.ShiftRight, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.GreaterEqual, index, line, col, 2); } return factory.create(TokenType.GreaterThan, index, line, col); case '?': return factory.create(TokenType.Question, index, line, col); case '$': return factory.create(TokenType.Dollar, index, line, col); case ';': return factory.create(TokenType.Semicolon, index, line, col); case ':': return factory.create(TokenType.Colon, index, line, col); case ',': return factory.create(TokenType.Comma, index, line, col); case '.': if (next == '.') { in_stream.read(); int p = in_stream.peek(); if (p != -1 && p == '.') { in_stream.read(); return factory.create(TokenType.TripleDot, index, line, col, 3); } return factory.create(TokenType.DoubleDot, index, line, col, 2); } return factory.create(TokenType.Dot, index, line, col); case ')': return factory.create(TokenType.CloseParenthesis, index, line, col); case '(': return factory.create(TokenType.OpenParenthesis, index, line, col); case ']': return factory.create(TokenType.CloseSquareBracket, index, line, col); case '[': return factory.create(TokenType.OpenSquareBracket, index, line, col); case '}': return factory.create(TokenType.CloseCurlyBrace, index, line, col); case '{': return factory.create(TokenType.OpenCurlyBrace, index, line, col); case '#': return factory.create(TokenType.Hash, index, line, col); default: return null; } } LexOperator(TokenFactory factory, LexerStream in_stream); Token read(); }
LexOperator { public Token read() throws IOException, LexerException { int index = in_stream.getPosition(); int line = in_stream.getLine(); int col = in_stream.getCol(); int next = in_stream.peek(); if (next == -1) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Unexpected end of input stream when parsing operator"); } if (next == '.') { int tmp = in_stream.peek(2); if (tmp > 0 && Character.isDigit(tmp)) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Literal float encountered when operator '.' expected"); } } char cur = (char)in_stream.read(); next = in_stream.peek(); switch (cur) { case '+': switch (next) { case '+': in_stream.read(); return factory.create(TokenType.Increment, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.PlusAssign, index, line, col, 2); } return factory.create(TokenType.Plus, index, line, col); case '-': switch (next) { case '-': in_stream.read(); return factory.create(TokenType.Decrement, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.MinusAssign, index, line, col, 2); } return factory.create(TokenType.Minus, index, line, col); case '*': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.TimesAssign, index, line, col, 2); default: break; } return factory.create(TokenType.Times, index, line, col); case '/': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.DivAssign, index, line, col, 2); } return factory.create(TokenType.Div, index, line, col); case '%': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.ModAssign, index, line, col, 2); } return factory.create(TokenType.Mod, index, line, col); case '&': switch (next) { case '&': in_stream.read(); return factory.create(TokenType.LogicalAnd, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.BitwiseAndAssign, index, line, col, 2); } return factory.create(TokenType.BitwiseAnd, index, line, col); case '|': switch (next) { case '|': in_stream.read(); return factory.create(TokenType.LogicalOr, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.BitwiseOrAssign, index, line, col, 2); } return factory.create(TokenType.BitwiseOr, index, line, col); case '^': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.XorAssign, index, line, col, 2); case '^': in_stream.read(); if (in_stream.peek() == '=') { in_stream.read(); return factory.create(TokenType.PowAssign, index, line, col, 3); } return factory.create(TokenType.Pow, index, line, col, 2); } return factory.create(TokenType.Xor, index, line, col); case '!': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.NotEqual, index, line, col, 2); case '<': in_stream.read(); switch (in_stream.peek()) { case '=': in_stream.read(); return factory.create(TokenType.UnorderedOrGreater, index, line, col, 3); case '>': in_stream.read(); switch (in_stream.peek()) { case '=': in_stream.read(); return factory.create(TokenType.Unordered, index, line, col, 4); } return factory.create(TokenType.UnorderedOrEqual, index, line, col, 3); } return factory.create(TokenType.UnorderedGreaterOrEqual, index, line, col, 2); case '>': in_stream.read(); switch (in_stream.peek()) { case '=': in_stream.read(); return factory.create(TokenType.UnorderedOrLess, index, line, col, 3); default: break; } return factory.create(TokenType.UnorderedLessOrEqual, index, line, col, 2); } return factory.create(TokenType.Not, index, line, col); case '~': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.TildeAssign, index, line, col, 2); } return factory.create(TokenType.Tilde, index, line, col); case '=': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.Equal, index, line, col, 2); case '>': in_stream.read(); return factory.create(TokenType.GoesTo, index, line, col, 2); } return factory.create(TokenType.Assign, index, line, col); case '<': switch (next) { case '<': in_stream.read(); switch (in_stream.peek()) { case '=': in_stream.read(); return factory.create(TokenType.ShiftLeftAssign, index, line, col, 3); default: break; } return factory.create(TokenType.ShiftLeft, index, line, col, 2); case '>': in_stream.read(); switch (in_stream.peek()) { case '=': in_stream.read(); return factory.create(TokenType.LessEqualOrGreater, index, line, col, 3); default: break; } return factory.create(TokenType.LessOrGreater, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.LessEqual, index, line, col, 2); } return factory.create(TokenType.LessThan, index, line, col); case '>': switch (next) { case '>': in_stream.read(); int p = in_stream.peek(); if (p != -1) { switch ((char)p) { case '=': in_stream.read(); return factory.create(TokenType.ShiftRightAssign, index, line, col, 3); case '>': in_stream.read(); int q = in_stream.peek(); if (q != -1 && q == '=') { in_stream.read(); return factory.create(TokenType.TripleRightShiftAssign, index, line, col, 4); } return factory.create(TokenType.ShiftRightUnsigned, index, line, col, 3); } } return factory.create(TokenType.ShiftRight, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.GreaterEqual, index, line, col, 2); } return factory.create(TokenType.GreaterThan, index, line, col); case '?': return factory.create(TokenType.Question, index, line, col); case '$': return factory.create(TokenType.Dollar, index, line, col); case ';': return factory.create(TokenType.Semicolon, index, line, col); case ':': return factory.create(TokenType.Colon, index, line, col); case ',': return factory.create(TokenType.Comma, index, line, col); case '.': if (next == '.') { in_stream.read(); int p = in_stream.peek(); if (p != -1 && p == '.') { in_stream.read(); return factory.create(TokenType.TripleDot, index, line, col, 3); } return factory.create(TokenType.DoubleDot, index, line, col, 2); } return factory.create(TokenType.Dot, index, line, col); case ')': return factory.create(TokenType.CloseParenthesis, index, line, col); case '(': return factory.create(TokenType.OpenParenthesis, index, line, col); case ']': return factory.create(TokenType.CloseSquareBracket, index, line, col); case '[': return factory.create(TokenType.OpenSquareBracket, index, line, col); case '}': return factory.create(TokenType.CloseCurlyBrace, index, line, col); case '{': return factory.create(TokenType.OpenCurlyBrace, index, line, col); case '#': return factory.create(TokenType.Hash, index, line, col); default: return null; } } LexOperator(TokenFactory factory, LexerStream in_stream); Token read(); }
@Test public void u_shift() throws Exception { String str = "a>>>=b>>>"; LexerStream ls = new LexerStream(new StringReader(str)); LexOperator lex = new LexOperator(new BaseTokenFactory(), ls); ls.read(); Token tok = lex.read(); assertEquals(TokenType.TripleRightShiftAssign, tok.getType()); assertTrue(tok.getLine() == 1 && tok.getEndLine() == 1); assertEquals(2, tok.getCol()); assertEquals(6, tok.getEndCol()); ls.read(); tok = lex.read(); assertEquals(TokenType.ShiftRightUnsigned, tok.getType()); assertTrue(tok.getLine() == 1 && tok.getEndLine() == 1); assertEquals(7, tok.getCol()); assertEquals(10, tok.getEndCol()); }
public Token read() throws IOException, LexerException { int index = in_stream.getPosition(); int line = in_stream.getLine(); int col = in_stream.getCol(); int next = in_stream.peek(); if (next == -1) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Unexpected end of input stream when parsing operator"); } if (next == '.') { int tmp = in_stream.peek(2); if (tmp > 0 && Character.isDigit(tmp)) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Literal float encountered when operator '.' expected"); } } char cur = (char)in_stream.read(); next = in_stream.peek(); switch (cur) { case '+': switch (next) { case '+': in_stream.read(); return factory.create(TokenType.Increment, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.PlusAssign, index, line, col, 2); } return factory.create(TokenType.Plus, index, line, col); case '-': switch (next) { case '-': in_stream.read(); return factory.create(TokenType.Decrement, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.MinusAssign, index, line, col, 2); } return factory.create(TokenType.Minus, index, line, col); case '*': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.TimesAssign, index, line, col, 2); default: break; } return factory.create(TokenType.Times, index, line, col); case '/': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.DivAssign, index, line, col, 2); } return factory.create(TokenType.Div, index, line, col); case '%': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.ModAssign, index, line, col, 2); } return factory.create(TokenType.Mod, index, line, col); case '&': switch (next) { case '&': in_stream.read(); return factory.create(TokenType.LogicalAnd, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.BitwiseAndAssign, index, line, col, 2); } return factory.create(TokenType.BitwiseAnd, index, line, col); case '|': switch (next) { case '|': in_stream.read(); return factory.create(TokenType.LogicalOr, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.BitwiseOrAssign, index, line, col, 2); } return factory.create(TokenType.BitwiseOr, index, line, col); case '^': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.XorAssign, index, line, col, 2); case '^': in_stream.read(); if (in_stream.peek() == '=') { in_stream.read(); return factory.create(TokenType.PowAssign, index, line, col, 3); } return factory.create(TokenType.Pow, index, line, col, 2); } return factory.create(TokenType.Xor, index, line, col); case '!': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.NotEqual, index, line, col, 2); case '<': in_stream.read(); switch (in_stream.peek()) { case '=': in_stream.read(); return factory.create(TokenType.UnorderedOrGreater, index, line, col, 3); case '>': in_stream.read(); switch (in_stream.peek()) { case '=': in_stream.read(); return factory.create(TokenType.Unordered, index, line, col, 4); } return factory.create(TokenType.UnorderedOrEqual, index, line, col, 3); } return factory.create(TokenType.UnorderedGreaterOrEqual, index, line, col, 2); case '>': in_stream.read(); switch (in_stream.peek()) { case '=': in_stream.read(); return factory.create(TokenType.UnorderedOrLess, index, line, col, 3); default: break; } return factory.create(TokenType.UnorderedLessOrEqual, index, line, col, 2); } return factory.create(TokenType.Not, index, line, col); case '~': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.TildeAssign, index, line, col, 2); } return factory.create(TokenType.Tilde, index, line, col); case '=': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.Equal, index, line, col, 2); case '>': in_stream.read(); return factory.create(TokenType.GoesTo, index, line, col, 2); } return factory.create(TokenType.Assign, index, line, col); case '<': switch (next) { case '<': in_stream.read(); switch (in_stream.peek()) { case '=': in_stream.read(); return factory.create(TokenType.ShiftLeftAssign, index, line, col, 3); default: break; } return factory.create(TokenType.ShiftLeft, index, line, col, 2); case '>': in_stream.read(); switch (in_stream.peek()) { case '=': in_stream.read(); return factory.create(TokenType.LessEqualOrGreater, index, line, col, 3); default: break; } return factory.create(TokenType.LessOrGreater, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.LessEqual, index, line, col, 2); } return factory.create(TokenType.LessThan, index, line, col); case '>': switch (next) { case '>': in_stream.read(); int p = in_stream.peek(); if (p != -1) { switch ((char)p) { case '=': in_stream.read(); return factory.create(TokenType.ShiftRightAssign, index, line, col, 3); case '>': in_stream.read(); int q = in_stream.peek(); if (q != -1 && q == '=') { in_stream.read(); return factory.create(TokenType.TripleRightShiftAssign, index, line, col, 4); } return factory.create(TokenType.ShiftRightUnsigned, index, line, col, 3); } } return factory.create(TokenType.ShiftRight, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.GreaterEqual, index, line, col, 2); } return factory.create(TokenType.GreaterThan, index, line, col); case '?': return factory.create(TokenType.Question, index, line, col); case '$': return factory.create(TokenType.Dollar, index, line, col); case ';': return factory.create(TokenType.Semicolon, index, line, col); case ':': return factory.create(TokenType.Colon, index, line, col); case ',': return factory.create(TokenType.Comma, index, line, col); case '.': if (next == '.') { in_stream.read(); int p = in_stream.peek(); if (p != -1 && p == '.') { in_stream.read(); return factory.create(TokenType.TripleDot, index, line, col, 3); } return factory.create(TokenType.DoubleDot, index, line, col, 2); } return factory.create(TokenType.Dot, index, line, col); case ')': return factory.create(TokenType.CloseParenthesis, index, line, col); case '(': return factory.create(TokenType.OpenParenthesis, index, line, col); case ']': return factory.create(TokenType.CloseSquareBracket, index, line, col); case '[': return factory.create(TokenType.OpenSquareBracket, index, line, col); case '}': return factory.create(TokenType.CloseCurlyBrace, index, line, col); case '{': return factory.create(TokenType.OpenCurlyBrace, index, line, col); case '#': return factory.create(TokenType.Hash, index, line, col); default: return null; } }
LexOperator { public Token read() throws IOException, LexerException { int index = in_stream.getPosition(); int line = in_stream.getLine(); int col = in_stream.getCol(); int next = in_stream.peek(); if (next == -1) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Unexpected end of input stream when parsing operator"); } if (next == '.') { int tmp = in_stream.peek(2); if (tmp > 0 && Character.isDigit(tmp)) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Literal float encountered when operator '.' expected"); } } char cur = (char)in_stream.read(); next = in_stream.peek(); switch (cur) { case '+': switch (next) { case '+': in_stream.read(); return factory.create(TokenType.Increment, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.PlusAssign, index, line, col, 2); } return factory.create(TokenType.Plus, index, line, col); case '-': switch (next) { case '-': in_stream.read(); return factory.create(TokenType.Decrement, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.MinusAssign, index, line, col, 2); } return factory.create(TokenType.Minus, index, line, col); case '*': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.TimesAssign, index, line, col, 2); default: break; } return factory.create(TokenType.Times, index, line, col); case '/': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.DivAssign, index, line, col, 2); } return factory.create(TokenType.Div, index, line, col); case '%': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.ModAssign, index, line, col, 2); } return factory.create(TokenType.Mod, index, line, col); case '&': switch (next) { case '&': in_stream.read(); return factory.create(TokenType.LogicalAnd, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.BitwiseAndAssign, index, line, col, 2); } return factory.create(TokenType.BitwiseAnd, index, line, col); case '|': switch (next) { case '|': in_stream.read(); return factory.create(TokenType.LogicalOr, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.BitwiseOrAssign, index, line, col, 2); } return factory.create(TokenType.BitwiseOr, index, line, col); case '^': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.XorAssign, index, line, col, 2); case '^': in_stream.read(); if (in_stream.peek() == '=') { in_stream.read(); return factory.create(TokenType.PowAssign, index, line, col, 3); } return factory.create(TokenType.Pow, index, line, col, 2); } return factory.create(TokenType.Xor, index, line, col); case '!': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.NotEqual, index, line, col, 2); case '<': in_stream.read(); switch (in_stream.peek()) { case '=': in_stream.read(); return factory.create(TokenType.UnorderedOrGreater, index, line, col, 3); case '>': in_stream.read(); switch (in_stream.peek()) { case '=': in_stream.read(); return factory.create(TokenType.Unordered, index, line, col, 4); } return factory.create(TokenType.UnorderedOrEqual, index, line, col, 3); } return factory.create(TokenType.UnorderedGreaterOrEqual, index, line, col, 2); case '>': in_stream.read(); switch (in_stream.peek()) { case '=': in_stream.read(); return factory.create(TokenType.UnorderedOrLess, index, line, col, 3); default: break; } return factory.create(TokenType.UnorderedLessOrEqual, index, line, col, 2); } return factory.create(TokenType.Not, index, line, col); case '~': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.TildeAssign, index, line, col, 2); } return factory.create(TokenType.Tilde, index, line, col); case '=': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.Equal, index, line, col, 2); case '>': in_stream.read(); return factory.create(TokenType.GoesTo, index, line, col, 2); } return factory.create(TokenType.Assign, index, line, col); case '<': switch (next) { case '<': in_stream.read(); switch (in_stream.peek()) { case '=': in_stream.read(); return factory.create(TokenType.ShiftLeftAssign, index, line, col, 3); default: break; } return factory.create(TokenType.ShiftLeft, index, line, col, 2); case '>': in_stream.read(); switch (in_stream.peek()) { case '=': in_stream.read(); return factory.create(TokenType.LessEqualOrGreater, index, line, col, 3); default: break; } return factory.create(TokenType.LessOrGreater, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.LessEqual, index, line, col, 2); } return factory.create(TokenType.LessThan, index, line, col); case '>': switch (next) { case '>': in_stream.read(); int p = in_stream.peek(); if (p != -1) { switch ((char)p) { case '=': in_stream.read(); return factory.create(TokenType.ShiftRightAssign, index, line, col, 3); case '>': in_stream.read(); int q = in_stream.peek(); if (q != -1 && q == '=') { in_stream.read(); return factory.create(TokenType.TripleRightShiftAssign, index, line, col, 4); } return factory.create(TokenType.ShiftRightUnsigned, index, line, col, 3); } } return factory.create(TokenType.ShiftRight, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.GreaterEqual, index, line, col, 2); } return factory.create(TokenType.GreaterThan, index, line, col); case '?': return factory.create(TokenType.Question, index, line, col); case '$': return factory.create(TokenType.Dollar, index, line, col); case ';': return factory.create(TokenType.Semicolon, index, line, col); case ':': return factory.create(TokenType.Colon, index, line, col); case ',': return factory.create(TokenType.Comma, index, line, col); case '.': if (next == '.') { in_stream.read(); int p = in_stream.peek(); if (p != -1 && p == '.') { in_stream.read(); return factory.create(TokenType.TripleDot, index, line, col, 3); } return factory.create(TokenType.DoubleDot, index, line, col, 2); } return factory.create(TokenType.Dot, index, line, col); case ')': return factory.create(TokenType.CloseParenthesis, index, line, col); case '(': return factory.create(TokenType.OpenParenthesis, index, line, col); case ']': return factory.create(TokenType.CloseSquareBracket, index, line, col); case '[': return factory.create(TokenType.OpenSquareBracket, index, line, col); case '}': return factory.create(TokenType.CloseCurlyBrace, index, line, col); case '{': return factory.create(TokenType.OpenCurlyBrace, index, line, col); case '#': return factory.create(TokenType.Hash, index, line, col); default: return null; } } }
LexOperator { public Token read() throws IOException, LexerException { int index = in_stream.getPosition(); int line = in_stream.getLine(); int col = in_stream.getCol(); int next = in_stream.peek(); if (next == -1) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Unexpected end of input stream when parsing operator"); } if (next == '.') { int tmp = in_stream.peek(2); if (tmp > 0 && Character.isDigit(tmp)) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Literal float encountered when operator '.' expected"); } } char cur = (char)in_stream.read(); next = in_stream.peek(); switch (cur) { case '+': switch (next) { case '+': in_stream.read(); return factory.create(TokenType.Increment, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.PlusAssign, index, line, col, 2); } return factory.create(TokenType.Plus, index, line, col); case '-': switch (next) { case '-': in_stream.read(); return factory.create(TokenType.Decrement, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.MinusAssign, index, line, col, 2); } return factory.create(TokenType.Minus, index, line, col); case '*': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.TimesAssign, index, line, col, 2); default: break; } return factory.create(TokenType.Times, index, line, col); case '/': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.DivAssign, index, line, col, 2); } return factory.create(TokenType.Div, index, line, col); case '%': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.ModAssign, index, line, col, 2); } return factory.create(TokenType.Mod, index, line, col); case '&': switch (next) { case '&': in_stream.read(); return factory.create(TokenType.LogicalAnd, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.BitwiseAndAssign, index, line, col, 2); } return factory.create(TokenType.BitwiseAnd, index, line, col); case '|': switch (next) { case '|': in_stream.read(); return factory.create(TokenType.LogicalOr, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.BitwiseOrAssign, index, line, col, 2); } return factory.create(TokenType.BitwiseOr, index, line, col); case '^': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.XorAssign, index, line, col, 2); case '^': in_stream.read(); if (in_stream.peek() == '=') { in_stream.read(); return factory.create(TokenType.PowAssign, index, line, col, 3); } return factory.create(TokenType.Pow, index, line, col, 2); } return factory.create(TokenType.Xor, index, line, col); case '!': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.NotEqual, index, line, col, 2); case '<': in_stream.read(); switch (in_stream.peek()) { case '=': in_stream.read(); return factory.create(TokenType.UnorderedOrGreater, index, line, col, 3); case '>': in_stream.read(); switch (in_stream.peek()) { case '=': in_stream.read(); return factory.create(TokenType.Unordered, index, line, col, 4); } return factory.create(TokenType.UnorderedOrEqual, index, line, col, 3); } return factory.create(TokenType.UnorderedGreaterOrEqual, index, line, col, 2); case '>': in_stream.read(); switch (in_stream.peek()) { case '=': in_stream.read(); return factory.create(TokenType.UnorderedOrLess, index, line, col, 3); default: break; } return factory.create(TokenType.UnorderedLessOrEqual, index, line, col, 2); } return factory.create(TokenType.Not, index, line, col); case '~': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.TildeAssign, index, line, col, 2); } return factory.create(TokenType.Tilde, index, line, col); case '=': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.Equal, index, line, col, 2); case '>': in_stream.read(); return factory.create(TokenType.GoesTo, index, line, col, 2); } return factory.create(TokenType.Assign, index, line, col); case '<': switch (next) { case '<': in_stream.read(); switch (in_stream.peek()) { case '=': in_stream.read(); return factory.create(TokenType.ShiftLeftAssign, index, line, col, 3); default: break; } return factory.create(TokenType.ShiftLeft, index, line, col, 2); case '>': in_stream.read(); switch (in_stream.peek()) { case '=': in_stream.read(); return factory.create(TokenType.LessEqualOrGreater, index, line, col, 3); default: break; } return factory.create(TokenType.LessOrGreater, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.LessEqual, index, line, col, 2); } return factory.create(TokenType.LessThan, index, line, col); case '>': switch (next) { case '>': in_stream.read(); int p = in_stream.peek(); if (p != -1) { switch ((char)p) { case '=': in_stream.read(); return factory.create(TokenType.ShiftRightAssign, index, line, col, 3); case '>': in_stream.read(); int q = in_stream.peek(); if (q != -1 && q == '=') { in_stream.read(); return factory.create(TokenType.TripleRightShiftAssign, index, line, col, 4); } return factory.create(TokenType.ShiftRightUnsigned, index, line, col, 3); } } return factory.create(TokenType.ShiftRight, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.GreaterEqual, index, line, col, 2); } return factory.create(TokenType.GreaterThan, index, line, col); case '?': return factory.create(TokenType.Question, index, line, col); case '$': return factory.create(TokenType.Dollar, index, line, col); case ';': return factory.create(TokenType.Semicolon, index, line, col); case ':': return factory.create(TokenType.Colon, index, line, col); case ',': return factory.create(TokenType.Comma, index, line, col); case '.': if (next == '.') { in_stream.read(); int p = in_stream.peek(); if (p != -1 && p == '.') { in_stream.read(); return factory.create(TokenType.TripleDot, index, line, col, 3); } return factory.create(TokenType.DoubleDot, index, line, col, 2); } return factory.create(TokenType.Dot, index, line, col); case ')': return factory.create(TokenType.CloseParenthesis, index, line, col); case '(': return factory.create(TokenType.OpenParenthesis, index, line, col); case ']': return factory.create(TokenType.CloseSquareBracket, index, line, col); case '[': return factory.create(TokenType.OpenSquareBracket, index, line, col); case '}': return factory.create(TokenType.CloseCurlyBrace, index, line, col); case '{': return factory.create(TokenType.OpenCurlyBrace, index, line, col); case '#': return factory.create(TokenType.Hash, index, line, col); default: return null; } } LexOperator(TokenFactory factory, LexerStream in_stream); }
LexOperator { public Token read() throws IOException, LexerException { int index = in_stream.getPosition(); int line = in_stream.getLine(); int col = in_stream.getCol(); int next = in_stream.peek(); if (next == -1) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Unexpected end of input stream when parsing operator"); } if (next == '.') { int tmp = in_stream.peek(2); if (tmp > 0 && Character.isDigit(tmp)) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Literal float encountered when operator '.' expected"); } } char cur = (char)in_stream.read(); next = in_stream.peek(); switch (cur) { case '+': switch (next) { case '+': in_stream.read(); return factory.create(TokenType.Increment, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.PlusAssign, index, line, col, 2); } return factory.create(TokenType.Plus, index, line, col); case '-': switch (next) { case '-': in_stream.read(); return factory.create(TokenType.Decrement, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.MinusAssign, index, line, col, 2); } return factory.create(TokenType.Minus, index, line, col); case '*': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.TimesAssign, index, line, col, 2); default: break; } return factory.create(TokenType.Times, index, line, col); case '/': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.DivAssign, index, line, col, 2); } return factory.create(TokenType.Div, index, line, col); case '%': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.ModAssign, index, line, col, 2); } return factory.create(TokenType.Mod, index, line, col); case '&': switch (next) { case '&': in_stream.read(); return factory.create(TokenType.LogicalAnd, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.BitwiseAndAssign, index, line, col, 2); } return factory.create(TokenType.BitwiseAnd, index, line, col); case '|': switch (next) { case '|': in_stream.read(); return factory.create(TokenType.LogicalOr, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.BitwiseOrAssign, index, line, col, 2); } return factory.create(TokenType.BitwiseOr, index, line, col); case '^': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.XorAssign, index, line, col, 2); case '^': in_stream.read(); if (in_stream.peek() == '=') { in_stream.read(); return factory.create(TokenType.PowAssign, index, line, col, 3); } return factory.create(TokenType.Pow, index, line, col, 2); } return factory.create(TokenType.Xor, index, line, col); case '!': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.NotEqual, index, line, col, 2); case '<': in_stream.read(); switch (in_stream.peek()) { case '=': in_stream.read(); return factory.create(TokenType.UnorderedOrGreater, index, line, col, 3); case '>': in_stream.read(); switch (in_stream.peek()) { case '=': in_stream.read(); return factory.create(TokenType.Unordered, index, line, col, 4); } return factory.create(TokenType.UnorderedOrEqual, index, line, col, 3); } return factory.create(TokenType.UnorderedGreaterOrEqual, index, line, col, 2); case '>': in_stream.read(); switch (in_stream.peek()) { case '=': in_stream.read(); return factory.create(TokenType.UnorderedOrLess, index, line, col, 3); default: break; } return factory.create(TokenType.UnorderedLessOrEqual, index, line, col, 2); } return factory.create(TokenType.Not, index, line, col); case '~': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.TildeAssign, index, line, col, 2); } return factory.create(TokenType.Tilde, index, line, col); case '=': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.Equal, index, line, col, 2); case '>': in_stream.read(); return factory.create(TokenType.GoesTo, index, line, col, 2); } return factory.create(TokenType.Assign, index, line, col); case '<': switch (next) { case '<': in_stream.read(); switch (in_stream.peek()) { case '=': in_stream.read(); return factory.create(TokenType.ShiftLeftAssign, index, line, col, 3); default: break; } return factory.create(TokenType.ShiftLeft, index, line, col, 2); case '>': in_stream.read(); switch (in_stream.peek()) { case '=': in_stream.read(); return factory.create(TokenType.LessEqualOrGreater, index, line, col, 3); default: break; } return factory.create(TokenType.LessOrGreater, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.LessEqual, index, line, col, 2); } return factory.create(TokenType.LessThan, index, line, col); case '>': switch (next) { case '>': in_stream.read(); int p = in_stream.peek(); if (p != -1) { switch ((char)p) { case '=': in_stream.read(); return factory.create(TokenType.ShiftRightAssign, index, line, col, 3); case '>': in_stream.read(); int q = in_stream.peek(); if (q != -1 && q == '=') { in_stream.read(); return factory.create(TokenType.TripleRightShiftAssign, index, line, col, 4); } return factory.create(TokenType.ShiftRightUnsigned, index, line, col, 3); } } return factory.create(TokenType.ShiftRight, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.GreaterEqual, index, line, col, 2); } return factory.create(TokenType.GreaterThan, index, line, col); case '?': return factory.create(TokenType.Question, index, line, col); case '$': return factory.create(TokenType.Dollar, index, line, col); case ';': return factory.create(TokenType.Semicolon, index, line, col); case ':': return factory.create(TokenType.Colon, index, line, col); case ',': return factory.create(TokenType.Comma, index, line, col); case '.': if (next == '.') { in_stream.read(); int p = in_stream.peek(); if (p != -1 && p == '.') { in_stream.read(); return factory.create(TokenType.TripleDot, index, line, col, 3); } return factory.create(TokenType.DoubleDot, index, line, col, 2); } return factory.create(TokenType.Dot, index, line, col); case ')': return factory.create(TokenType.CloseParenthesis, index, line, col); case '(': return factory.create(TokenType.OpenParenthesis, index, line, col); case ']': return factory.create(TokenType.CloseSquareBracket, index, line, col); case '[': return factory.create(TokenType.OpenSquareBracket, index, line, col); case '}': return factory.create(TokenType.CloseCurlyBrace, index, line, col); case '{': return factory.create(TokenType.OpenCurlyBrace, index, line, col); case '#': return factory.create(TokenType.Hash, index, line, col); default: return null; } } LexOperator(TokenFactory factory, LexerStream in_stream); Token read(); }
LexOperator { public Token read() throws IOException, LexerException { int index = in_stream.getPosition(); int line = in_stream.getLine(); int col = in_stream.getCol(); int next = in_stream.peek(); if (next == -1) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Unexpected end of input stream when parsing operator"); } if (next == '.') { int tmp = in_stream.peek(2); if (tmp > 0 && Character.isDigit(tmp)) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Literal float encountered when operator '.' expected"); } } char cur = (char)in_stream.read(); next = in_stream.peek(); switch (cur) { case '+': switch (next) { case '+': in_stream.read(); return factory.create(TokenType.Increment, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.PlusAssign, index, line, col, 2); } return factory.create(TokenType.Plus, index, line, col); case '-': switch (next) { case '-': in_stream.read(); return factory.create(TokenType.Decrement, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.MinusAssign, index, line, col, 2); } return factory.create(TokenType.Minus, index, line, col); case '*': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.TimesAssign, index, line, col, 2); default: break; } return factory.create(TokenType.Times, index, line, col); case '/': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.DivAssign, index, line, col, 2); } return factory.create(TokenType.Div, index, line, col); case '%': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.ModAssign, index, line, col, 2); } return factory.create(TokenType.Mod, index, line, col); case '&': switch (next) { case '&': in_stream.read(); return factory.create(TokenType.LogicalAnd, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.BitwiseAndAssign, index, line, col, 2); } return factory.create(TokenType.BitwiseAnd, index, line, col); case '|': switch (next) { case '|': in_stream.read(); return factory.create(TokenType.LogicalOr, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.BitwiseOrAssign, index, line, col, 2); } return factory.create(TokenType.BitwiseOr, index, line, col); case '^': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.XorAssign, index, line, col, 2); case '^': in_stream.read(); if (in_stream.peek() == '=') { in_stream.read(); return factory.create(TokenType.PowAssign, index, line, col, 3); } return factory.create(TokenType.Pow, index, line, col, 2); } return factory.create(TokenType.Xor, index, line, col); case '!': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.NotEqual, index, line, col, 2); case '<': in_stream.read(); switch (in_stream.peek()) { case '=': in_stream.read(); return factory.create(TokenType.UnorderedOrGreater, index, line, col, 3); case '>': in_stream.read(); switch (in_stream.peek()) { case '=': in_stream.read(); return factory.create(TokenType.Unordered, index, line, col, 4); } return factory.create(TokenType.UnorderedOrEqual, index, line, col, 3); } return factory.create(TokenType.UnorderedGreaterOrEqual, index, line, col, 2); case '>': in_stream.read(); switch (in_stream.peek()) { case '=': in_stream.read(); return factory.create(TokenType.UnorderedOrLess, index, line, col, 3); default: break; } return factory.create(TokenType.UnorderedLessOrEqual, index, line, col, 2); } return factory.create(TokenType.Not, index, line, col); case '~': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.TildeAssign, index, line, col, 2); } return factory.create(TokenType.Tilde, index, line, col); case '=': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.Equal, index, line, col, 2); case '>': in_stream.read(); return factory.create(TokenType.GoesTo, index, line, col, 2); } return factory.create(TokenType.Assign, index, line, col); case '<': switch (next) { case '<': in_stream.read(); switch (in_stream.peek()) { case '=': in_stream.read(); return factory.create(TokenType.ShiftLeftAssign, index, line, col, 3); default: break; } return factory.create(TokenType.ShiftLeft, index, line, col, 2); case '>': in_stream.read(); switch (in_stream.peek()) { case '=': in_stream.read(); return factory.create(TokenType.LessEqualOrGreater, index, line, col, 3); default: break; } return factory.create(TokenType.LessOrGreater, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.LessEqual, index, line, col, 2); } return factory.create(TokenType.LessThan, index, line, col); case '>': switch (next) { case '>': in_stream.read(); int p = in_stream.peek(); if (p != -1) { switch ((char)p) { case '=': in_stream.read(); return factory.create(TokenType.ShiftRightAssign, index, line, col, 3); case '>': in_stream.read(); int q = in_stream.peek(); if (q != -1 && q == '=') { in_stream.read(); return factory.create(TokenType.TripleRightShiftAssign, index, line, col, 4); } return factory.create(TokenType.ShiftRightUnsigned, index, line, col, 3); } } return factory.create(TokenType.ShiftRight, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.GreaterEqual, index, line, col, 2); } return factory.create(TokenType.GreaterThan, index, line, col); case '?': return factory.create(TokenType.Question, index, line, col); case '$': return factory.create(TokenType.Dollar, index, line, col); case ';': return factory.create(TokenType.Semicolon, index, line, col); case ':': return factory.create(TokenType.Colon, index, line, col); case ',': return factory.create(TokenType.Comma, index, line, col); case '.': if (next == '.') { in_stream.read(); int p = in_stream.peek(); if (p != -1 && p == '.') { in_stream.read(); return factory.create(TokenType.TripleDot, index, line, col, 3); } return factory.create(TokenType.DoubleDot, index, line, col, 2); } return factory.create(TokenType.Dot, index, line, col); case ')': return factory.create(TokenType.CloseParenthesis, index, line, col); case '(': return factory.create(TokenType.OpenParenthesis, index, line, col); case ']': return factory.create(TokenType.CloseSquareBracket, index, line, col); case '[': return factory.create(TokenType.OpenSquareBracket, index, line, col); case '}': return factory.create(TokenType.CloseCurlyBrace, index, line, col); case '{': return factory.create(TokenType.OpenCurlyBrace, index, line, col); case '#': return factory.create(TokenType.Hash, index, line, col); default: return null; } } LexOperator(TokenFactory factory, LexerStream in_stream); Token read(); }
@Test public void tri() throws Exception { String str = "a?b:c$d"; LexerStream ls = new LexerStream(new StringReader(str)); LexOperator lex = new LexOperator(new BaseTokenFactory(), ls); ls.read(); Token tok = lex.read(); assertEquals(TokenType.Question, tok.getType()); assertTrue(tok.getLine() == 1 && tok.getEndLine() == 1); assertEquals(2, tok.getCol()); assertEquals(3, tok.getEndCol()); ls.read(); tok = lex.read(); assertEquals(TokenType.Colon, tok.getType()); assertTrue(tok.getLine() == 1 && tok.getEndLine() == 1); assertEquals(4, tok.getCol()); assertEquals(5, tok.getEndCol()); ls.read(); tok = lex.read(); assertEquals(TokenType.Dollar, tok.getType()); assertTrue(tok.getLine() == 1 && tok.getEndLine() == 1); assertEquals(6, tok.getCol()); assertEquals(7, tok.getEndCol()); }
public Token read() throws IOException, LexerException { int index = in_stream.getPosition(); int line = in_stream.getLine(); int col = in_stream.getCol(); int next = in_stream.peek(); if (next == -1) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Unexpected end of input stream when parsing operator"); } if (next == '.') { int tmp = in_stream.peek(2); if (tmp > 0 && Character.isDigit(tmp)) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Literal float encountered when operator '.' expected"); } } char cur = (char)in_stream.read(); next = in_stream.peek(); switch (cur) { case '+': switch (next) { case '+': in_stream.read(); return factory.create(TokenType.Increment, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.PlusAssign, index, line, col, 2); } return factory.create(TokenType.Plus, index, line, col); case '-': switch (next) { case '-': in_stream.read(); return factory.create(TokenType.Decrement, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.MinusAssign, index, line, col, 2); } return factory.create(TokenType.Minus, index, line, col); case '*': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.TimesAssign, index, line, col, 2); default: break; } return factory.create(TokenType.Times, index, line, col); case '/': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.DivAssign, index, line, col, 2); } return factory.create(TokenType.Div, index, line, col); case '%': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.ModAssign, index, line, col, 2); } return factory.create(TokenType.Mod, index, line, col); case '&': switch (next) { case '&': in_stream.read(); return factory.create(TokenType.LogicalAnd, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.BitwiseAndAssign, index, line, col, 2); } return factory.create(TokenType.BitwiseAnd, index, line, col); case '|': switch (next) { case '|': in_stream.read(); return factory.create(TokenType.LogicalOr, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.BitwiseOrAssign, index, line, col, 2); } return factory.create(TokenType.BitwiseOr, index, line, col); case '^': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.XorAssign, index, line, col, 2); case '^': in_stream.read(); if (in_stream.peek() == '=') { in_stream.read(); return factory.create(TokenType.PowAssign, index, line, col, 3); } return factory.create(TokenType.Pow, index, line, col, 2); } return factory.create(TokenType.Xor, index, line, col); case '!': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.NotEqual, index, line, col, 2); case '<': in_stream.read(); switch (in_stream.peek()) { case '=': in_stream.read(); return factory.create(TokenType.UnorderedOrGreater, index, line, col, 3); case '>': in_stream.read(); switch (in_stream.peek()) { case '=': in_stream.read(); return factory.create(TokenType.Unordered, index, line, col, 4); } return factory.create(TokenType.UnorderedOrEqual, index, line, col, 3); } return factory.create(TokenType.UnorderedGreaterOrEqual, index, line, col, 2); case '>': in_stream.read(); switch (in_stream.peek()) { case '=': in_stream.read(); return factory.create(TokenType.UnorderedOrLess, index, line, col, 3); default: break; } return factory.create(TokenType.UnorderedLessOrEqual, index, line, col, 2); } return factory.create(TokenType.Not, index, line, col); case '~': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.TildeAssign, index, line, col, 2); } return factory.create(TokenType.Tilde, index, line, col); case '=': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.Equal, index, line, col, 2); case '>': in_stream.read(); return factory.create(TokenType.GoesTo, index, line, col, 2); } return factory.create(TokenType.Assign, index, line, col); case '<': switch (next) { case '<': in_stream.read(); switch (in_stream.peek()) { case '=': in_stream.read(); return factory.create(TokenType.ShiftLeftAssign, index, line, col, 3); default: break; } return factory.create(TokenType.ShiftLeft, index, line, col, 2); case '>': in_stream.read(); switch (in_stream.peek()) { case '=': in_stream.read(); return factory.create(TokenType.LessEqualOrGreater, index, line, col, 3); default: break; } return factory.create(TokenType.LessOrGreater, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.LessEqual, index, line, col, 2); } return factory.create(TokenType.LessThan, index, line, col); case '>': switch (next) { case '>': in_stream.read(); int p = in_stream.peek(); if (p != -1) { switch ((char)p) { case '=': in_stream.read(); return factory.create(TokenType.ShiftRightAssign, index, line, col, 3); case '>': in_stream.read(); int q = in_stream.peek(); if (q != -1 && q == '=') { in_stream.read(); return factory.create(TokenType.TripleRightShiftAssign, index, line, col, 4); } return factory.create(TokenType.ShiftRightUnsigned, index, line, col, 3); } } return factory.create(TokenType.ShiftRight, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.GreaterEqual, index, line, col, 2); } return factory.create(TokenType.GreaterThan, index, line, col); case '?': return factory.create(TokenType.Question, index, line, col); case '$': return factory.create(TokenType.Dollar, index, line, col); case ';': return factory.create(TokenType.Semicolon, index, line, col); case ':': return factory.create(TokenType.Colon, index, line, col); case ',': return factory.create(TokenType.Comma, index, line, col); case '.': if (next == '.') { in_stream.read(); int p = in_stream.peek(); if (p != -1 && p == '.') { in_stream.read(); return factory.create(TokenType.TripleDot, index, line, col, 3); } return factory.create(TokenType.DoubleDot, index, line, col, 2); } return factory.create(TokenType.Dot, index, line, col); case ')': return factory.create(TokenType.CloseParenthesis, index, line, col); case '(': return factory.create(TokenType.OpenParenthesis, index, line, col); case ']': return factory.create(TokenType.CloseSquareBracket, index, line, col); case '[': return factory.create(TokenType.OpenSquareBracket, index, line, col); case '}': return factory.create(TokenType.CloseCurlyBrace, index, line, col); case '{': return factory.create(TokenType.OpenCurlyBrace, index, line, col); case '#': return factory.create(TokenType.Hash, index, line, col); default: return null; } }
LexOperator { public Token read() throws IOException, LexerException { int index = in_stream.getPosition(); int line = in_stream.getLine(); int col = in_stream.getCol(); int next = in_stream.peek(); if (next == -1) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Unexpected end of input stream when parsing operator"); } if (next == '.') { int tmp = in_stream.peek(2); if (tmp > 0 && Character.isDigit(tmp)) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Literal float encountered when operator '.' expected"); } } char cur = (char)in_stream.read(); next = in_stream.peek(); switch (cur) { case '+': switch (next) { case '+': in_stream.read(); return factory.create(TokenType.Increment, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.PlusAssign, index, line, col, 2); } return factory.create(TokenType.Plus, index, line, col); case '-': switch (next) { case '-': in_stream.read(); return factory.create(TokenType.Decrement, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.MinusAssign, index, line, col, 2); } return factory.create(TokenType.Minus, index, line, col); case '*': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.TimesAssign, index, line, col, 2); default: break; } return factory.create(TokenType.Times, index, line, col); case '/': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.DivAssign, index, line, col, 2); } return factory.create(TokenType.Div, index, line, col); case '%': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.ModAssign, index, line, col, 2); } return factory.create(TokenType.Mod, index, line, col); case '&': switch (next) { case '&': in_stream.read(); return factory.create(TokenType.LogicalAnd, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.BitwiseAndAssign, index, line, col, 2); } return factory.create(TokenType.BitwiseAnd, index, line, col); case '|': switch (next) { case '|': in_stream.read(); return factory.create(TokenType.LogicalOr, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.BitwiseOrAssign, index, line, col, 2); } return factory.create(TokenType.BitwiseOr, index, line, col); case '^': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.XorAssign, index, line, col, 2); case '^': in_stream.read(); if (in_stream.peek() == '=') { in_stream.read(); return factory.create(TokenType.PowAssign, index, line, col, 3); } return factory.create(TokenType.Pow, index, line, col, 2); } return factory.create(TokenType.Xor, index, line, col); case '!': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.NotEqual, index, line, col, 2); case '<': in_stream.read(); switch (in_stream.peek()) { case '=': in_stream.read(); return factory.create(TokenType.UnorderedOrGreater, index, line, col, 3); case '>': in_stream.read(); switch (in_stream.peek()) { case '=': in_stream.read(); return factory.create(TokenType.Unordered, index, line, col, 4); } return factory.create(TokenType.UnorderedOrEqual, index, line, col, 3); } return factory.create(TokenType.UnorderedGreaterOrEqual, index, line, col, 2); case '>': in_stream.read(); switch (in_stream.peek()) { case '=': in_stream.read(); return factory.create(TokenType.UnorderedOrLess, index, line, col, 3); default: break; } return factory.create(TokenType.UnorderedLessOrEqual, index, line, col, 2); } return factory.create(TokenType.Not, index, line, col); case '~': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.TildeAssign, index, line, col, 2); } return factory.create(TokenType.Tilde, index, line, col); case '=': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.Equal, index, line, col, 2); case '>': in_stream.read(); return factory.create(TokenType.GoesTo, index, line, col, 2); } return factory.create(TokenType.Assign, index, line, col); case '<': switch (next) { case '<': in_stream.read(); switch (in_stream.peek()) { case '=': in_stream.read(); return factory.create(TokenType.ShiftLeftAssign, index, line, col, 3); default: break; } return factory.create(TokenType.ShiftLeft, index, line, col, 2); case '>': in_stream.read(); switch (in_stream.peek()) { case '=': in_stream.read(); return factory.create(TokenType.LessEqualOrGreater, index, line, col, 3); default: break; } return factory.create(TokenType.LessOrGreater, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.LessEqual, index, line, col, 2); } return factory.create(TokenType.LessThan, index, line, col); case '>': switch (next) { case '>': in_stream.read(); int p = in_stream.peek(); if (p != -1) { switch ((char)p) { case '=': in_stream.read(); return factory.create(TokenType.ShiftRightAssign, index, line, col, 3); case '>': in_stream.read(); int q = in_stream.peek(); if (q != -1 && q == '=') { in_stream.read(); return factory.create(TokenType.TripleRightShiftAssign, index, line, col, 4); } return factory.create(TokenType.ShiftRightUnsigned, index, line, col, 3); } } return factory.create(TokenType.ShiftRight, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.GreaterEqual, index, line, col, 2); } return factory.create(TokenType.GreaterThan, index, line, col); case '?': return factory.create(TokenType.Question, index, line, col); case '$': return factory.create(TokenType.Dollar, index, line, col); case ';': return factory.create(TokenType.Semicolon, index, line, col); case ':': return factory.create(TokenType.Colon, index, line, col); case ',': return factory.create(TokenType.Comma, index, line, col); case '.': if (next == '.') { in_stream.read(); int p = in_stream.peek(); if (p != -1 && p == '.') { in_stream.read(); return factory.create(TokenType.TripleDot, index, line, col, 3); } return factory.create(TokenType.DoubleDot, index, line, col, 2); } return factory.create(TokenType.Dot, index, line, col); case ')': return factory.create(TokenType.CloseParenthesis, index, line, col); case '(': return factory.create(TokenType.OpenParenthesis, index, line, col); case ']': return factory.create(TokenType.CloseSquareBracket, index, line, col); case '[': return factory.create(TokenType.OpenSquareBracket, index, line, col); case '}': return factory.create(TokenType.CloseCurlyBrace, index, line, col); case '{': return factory.create(TokenType.OpenCurlyBrace, index, line, col); case '#': return factory.create(TokenType.Hash, index, line, col); default: return null; } } }
LexOperator { public Token read() throws IOException, LexerException { int index = in_stream.getPosition(); int line = in_stream.getLine(); int col = in_stream.getCol(); int next = in_stream.peek(); if (next == -1) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Unexpected end of input stream when parsing operator"); } if (next == '.') { int tmp = in_stream.peek(2); if (tmp > 0 && Character.isDigit(tmp)) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Literal float encountered when operator '.' expected"); } } char cur = (char)in_stream.read(); next = in_stream.peek(); switch (cur) { case '+': switch (next) { case '+': in_stream.read(); return factory.create(TokenType.Increment, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.PlusAssign, index, line, col, 2); } return factory.create(TokenType.Plus, index, line, col); case '-': switch (next) { case '-': in_stream.read(); return factory.create(TokenType.Decrement, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.MinusAssign, index, line, col, 2); } return factory.create(TokenType.Minus, index, line, col); case '*': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.TimesAssign, index, line, col, 2); default: break; } return factory.create(TokenType.Times, index, line, col); case '/': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.DivAssign, index, line, col, 2); } return factory.create(TokenType.Div, index, line, col); case '%': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.ModAssign, index, line, col, 2); } return factory.create(TokenType.Mod, index, line, col); case '&': switch (next) { case '&': in_stream.read(); return factory.create(TokenType.LogicalAnd, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.BitwiseAndAssign, index, line, col, 2); } return factory.create(TokenType.BitwiseAnd, index, line, col); case '|': switch (next) { case '|': in_stream.read(); return factory.create(TokenType.LogicalOr, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.BitwiseOrAssign, index, line, col, 2); } return factory.create(TokenType.BitwiseOr, index, line, col); case '^': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.XorAssign, index, line, col, 2); case '^': in_stream.read(); if (in_stream.peek() == '=') { in_stream.read(); return factory.create(TokenType.PowAssign, index, line, col, 3); } return factory.create(TokenType.Pow, index, line, col, 2); } return factory.create(TokenType.Xor, index, line, col); case '!': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.NotEqual, index, line, col, 2); case '<': in_stream.read(); switch (in_stream.peek()) { case '=': in_stream.read(); return factory.create(TokenType.UnorderedOrGreater, index, line, col, 3); case '>': in_stream.read(); switch (in_stream.peek()) { case '=': in_stream.read(); return factory.create(TokenType.Unordered, index, line, col, 4); } return factory.create(TokenType.UnorderedOrEqual, index, line, col, 3); } return factory.create(TokenType.UnorderedGreaterOrEqual, index, line, col, 2); case '>': in_stream.read(); switch (in_stream.peek()) { case '=': in_stream.read(); return factory.create(TokenType.UnorderedOrLess, index, line, col, 3); default: break; } return factory.create(TokenType.UnorderedLessOrEqual, index, line, col, 2); } return factory.create(TokenType.Not, index, line, col); case '~': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.TildeAssign, index, line, col, 2); } return factory.create(TokenType.Tilde, index, line, col); case '=': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.Equal, index, line, col, 2); case '>': in_stream.read(); return factory.create(TokenType.GoesTo, index, line, col, 2); } return factory.create(TokenType.Assign, index, line, col); case '<': switch (next) { case '<': in_stream.read(); switch (in_stream.peek()) { case '=': in_stream.read(); return factory.create(TokenType.ShiftLeftAssign, index, line, col, 3); default: break; } return factory.create(TokenType.ShiftLeft, index, line, col, 2); case '>': in_stream.read(); switch (in_stream.peek()) { case '=': in_stream.read(); return factory.create(TokenType.LessEqualOrGreater, index, line, col, 3); default: break; } return factory.create(TokenType.LessOrGreater, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.LessEqual, index, line, col, 2); } return factory.create(TokenType.LessThan, index, line, col); case '>': switch (next) { case '>': in_stream.read(); int p = in_stream.peek(); if (p != -1) { switch ((char)p) { case '=': in_stream.read(); return factory.create(TokenType.ShiftRightAssign, index, line, col, 3); case '>': in_stream.read(); int q = in_stream.peek(); if (q != -1 && q == '=') { in_stream.read(); return factory.create(TokenType.TripleRightShiftAssign, index, line, col, 4); } return factory.create(TokenType.ShiftRightUnsigned, index, line, col, 3); } } return factory.create(TokenType.ShiftRight, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.GreaterEqual, index, line, col, 2); } return factory.create(TokenType.GreaterThan, index, line, col); case '?': return factory.create(TokenType.Question, index, line, col); case '$': return factory.create(TokenType.Dollar, index, line, col); case ';': return factory.create(TokenType.Semicolon, index, line, col); case ':': return factory.create(TokenType.Colon, index, line, col); case ',': return factory.create(TokenType.Comma, index, line, col); case '.': if (next == '.') { in_stream.read(); int p = in_stream.peek(); if (p != -1 && p == '.') { in_stream.read(); return factory.create(TokenType.TripleDot, index, line, col, 3); } return factory.create(TokenType.DoubleDot, index, line, col, 2); } return factory.create(TokenType.Dot, index, line, col); case ')': return factory.create(TokenType.CloseParenthesis, index, line, col); case '(': return factory.create(TokenType.OpenParenthesis, index, line, col); case ']': return factory.create(TokenType.CloseSquareBracket, index, line, col); case '[': return factory.create(TokenType.OpenSquareBracket, index, line, col); case '}': return factory.create(TokenType.CloseCurlyBrace, index, line, col); case '{': return factory.create(TokenType.OpenCurlyBrace, index, line, col); case '#': return factory.create(TokenType.Hash, index, line, col); default: return null; } } LexOperator(TokenFactory factory, LexerStream in_stream); }
LexOperator { public Token read() throws IOException, LexerException { int index = in_stream.getPosition(); int line = in_stream.getLine(); int col = in_stream.getCol(); int next = in_stream.peek(); if (next == -1) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Unexpected end of input stream when parsing operator"); } if (next == '.') { int tmp = in_stream.peek(2); if (tmp > 0 && Character.isDigit(tmp)) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Literal float encountered when operator '.' expected"); } } char cur = (char)in_stream.read(); next = in_stream.peek(); switch (cur) { case '+': switch (next) { case '+': in_stream.read(); return factory.create(TokenType.Increment, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.PlusAssign, index, line, col, 2); } return factory.create(TokenType.Plus, index, line, col); case '-': switch (next) { case '-': in_stream.read(); return factory.create(TokenType.Decrement, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.MinusAssign, index, line, col, 2); } return factory.create(TokenType.Minus, index, line, col); case '*': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.TimesAssign, index, line, col, 2); default: break; } return factory.create(TokenType.Times, index, line, col); case '/': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.DivAssign, index, line, col, 2); } return factory.create(TokenType.Div, index, line, col); case '%': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.ModAssign, index, line, col, 2); } return factory.create(TokenType.Mod, index, line, col); case '&': switch (next) { case '&': in_stream.read(); return factory.create(TokenType.LogicalAnd, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.BitwiseAndAssign, index, line, col, 2); } return factory.create(TokenType.BitwiseAnd, index, line, col); case '|': switch (next) { case '|': in_stream.read(); return factory.create(TokenType.LogicalOr, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.BitwiseOrAssign, index, line, col, 2); } return factory.create(TokenType.BitwiseOr, index, line, col); case '^': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.XorAssign, index, line, col, 2); case '^': in_stream.read(); if (in_stream.peek() == '=') { in_stream.read(); return factory.create(TokenType.PowAssign, index, line, col, 3); } return factory.create(TokenType.Pow, index, line, col, 2); } return factory.create(TokenType.Xor, index, line, col); case '!': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.NotEqual, index, line, col, 2); case '<': in_stream.read(); switch (in_stream.peek()) { case '=': in_stream.read(); return factory.create(TokenType.UnorderedOrGreater, index, line, col, 3); case '>': in_stream.read(); switch (in_stream.peek()) { case '=': in_stream.read(); return factory.create(TokenType.Unordered, index, line, col, 4); } return factory.create(TokenType.UnorderedOrEqual, index, line, col, 3); } return factory.create(TokenType.UnorderedGreaterOrEqual, index, line, col, 2); case '>': in_stream.read(); switch (in_stream.peek()) { case '=': in_stream.read(); return factory.create(TokenType.UnorderedOrLess, index, line, col, 3); default: break; } return factory.create(TokenType.UnorderedLessOrEqual, index, line, col, 2); } return factory.create(TokenType.Not, index, line, col); case '~': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.TildeAssign, index, line, col, 2); } return factory.create(TokenType.Tilde, index, line, col); case '=': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.Equal, index, line, col, 2); case '>': in_stream.read(); return factory.create(TokenType.GoesTo, index, line, col, 2); } return factory.create(TokenType.Assign, index, line, col); case '<': switch (next) { case '<': in_stream.read(); switch (in_stream.peek()) { case '=': in_stream.read(); return factory.create(TokenType.ShiftLeftAssign, index, line, col, 3); default: break; } return factory.create(TokenType.ShiftLeft, index, line, col, 2); case '>': in_stream.read(); switch (in_stream.peek()) { case '=': in_stream.read(); return factory.create(TokenType.LessEqualOrGreater, index, line, col, 3); default: break; } return factory.create(TokenType.LessOrGreater, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.LessEqual, index, line, col, 2); } return factory.create(TokenType.LessThan, index, line, col); case '>': switch (next) { case '>': in_stream.read(); int p = in_stream.peek(); if (p != -1) { switch ((char)p) { case '=': in_stream.read(); return factory.create(TokenType.ShiftRightAssign, index, line, col, 3); case '>': in_stream.read(); int q = in_stream.peek(); if (q != -1 && q == '=') { in_stream.read(); return factory.create(TokenType.TripleRightShiftAssign, index, line, col, 4); } return factory.create(TokenType.ShiftRightUnsigned, index, line, col, 3); } } return factory.create(TokenType.ShiftRight, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.GreaterEqual, index, line, col, 2); } return factory.create(TokenType.GreaterThan, index, line, col); case '?': return factory.create(TokenType.Question, index, line, col); case '$': return factory.create(TokenType.Dollar, index, line, col); case ';': return factory.create(TokenType.Semicolon, index, line, col); case ':': return factory.create(TokenType.Colon, index, line, col); case ',': return factory.create(TokenType.Comma, index, line, col); case '.': if (next == '.') { in_stream.read(); int p = in_stream.peek(); if (p != -1 && p == '.') { in_stream.read(); return factory.create(TokenType.TripleDot, index, line, col, 3); } return factory.create(TokenType.DoubleDot, index, line, col, 2); } return factory.create(TokenType.Dot, index, line, col); case ')': return factory.create(TokenType.CloseParenthesis, index, line, col); case '(': return factory.create(TokenType.OpenParenthesis, index, line, col); case ']': return factory.create(TokenType.CloseSquareBracket, index, line, col); case '[': return factory.create(TokenType.OpenSquareBracket, index, line, col); case '}': return factory.create(TokenType.CloseCurlyBrace, index, line, col); case '{': return factory.create(TokenType.OpenCurlyBrace, index, line, col); case '#': return factory.create(TokenType.Hash, index, line, col); default: return null; } } LexOperator(TokenFactory factory, LexerStream in_stream); Token read(); }
LexOperator { public Token read() throws IOException, LexerException { int index = in_stream.getPosition(); int line = in_stream.getLine(); int col = in_stream.getCol(); int next = in_stream.peek(); if (next == -1) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Unexpected end of input stream when parsing operator"); } if (next == '.') { int tmp = in_stream.peek(2); if (tmp > 0 && Character.isDigit(tmp)) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Literal float encountered when operator '.' expected"); } } char cur = (char)in_stream.read(); next = in_stream.peek(); switch (cur) { case '+': switch (next) { case '+': in_stream.read(); return factory.create(TokenType.Increment, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.PlusAssign, index, line, col, 2); } return factory.create(TokenType.Plus, index, line, col); case '-': switch (next) { case '-': in_stream.read(); return factory.create(TokenType.Decrement, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.MinusAssign, index, line, col, 2); } return factory.create(TokenType.Minus, index, line, col); case '*': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.TimesAssign, index, line, col, 2); default: break; } return factory.create(TokenType.Times, index, line, col); case '/': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.DivAssign, index, line, col, 2); } return factory.create(TokenType.Div, index, line, col); case '%': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.ModAssign, index, line, col, 2); } return factory.create(TokenType.Mod, index, line, col); case '&': switch (next) { case '&': in_stream.read(); return factory.create(TokenType.LogicalAnd, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.BitwiseAndAssign, index, line, col, 2); } return factory.create(TokenType.BitwiseAnd, index, line, col); case '|': switch (next) { case '|': in_stream.read(); return factory.create(TokenType.LogicalOr, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.BitwiseOrAssign, index, line, col, 2); } return factory.create(TokenType.BitwiseOr, index, line, col); case '^': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.XorAssign, index, line, col, 2); case '^': in_stream.read(); if (in_stream.peek() == '=') { in_stream.read(); return factory.create(TokenType.PowAssign, index, line, col, 3); } return factory.create(TokenType.Pow, index, line, col, 2); } return factory.create(TokenType.Xor, index, line, col); case '!': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.NotEqual, index, line, col, 2); case '<': in_stream.read(); switch (in_stream.peek()) { case '=': in_stream.read(); return factory.create(TokenType.UnorderedOrGreater, index, line, col, 3); case '>': in_stream.read(); switch (in_stream.peek()) { case '=': in_stream.read(); return factory.create(TokenType.Unordered, index, line, col, 4); } return factory.create(TokenType.UnorderedOrEqual, index, line, col, 3); } return factory.create(TokenType.UnorderedGreaterOrEqual, index, line, col, 2); case '>': in_stream.read(); switch (in_stream.peek()) { case '=': in_stream.read(); return factory.create(TokenType.UnorderedOrLess, index, line, col, 3); default: break; } return factory.create(TokenType.UnorderedLessOrEqual, index, line, col, 2); } return factory.create(TokenType.Not, index, line, col); case '~': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.TildeAssign, index, line, col, 2); } return factory.create(TokenType.Tilde, index, line, col); case '=': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.Equal, index, line, col, 2); case '>': in_stream.read(); return factory.create(TokenType.GoesTo, index, line, col, 2); } return factory.create(TokenType.Assign, index, line, col); case '<': switch (next) { case '<': in_stream.read(); switch (in_stream.peek()) { case '=': in_stream.read(); return factory.create(TokenType.ShiftLeftAssign, index, line, col, 3); default: break; } return factory.create(TokenType.ShiftLeft, index, line, col, 2); case '>': in_stream.read(); switch (in_stream.peek()) { case '=': in_stream.read(); return factory.create(TokenType.LessEqualOrGreater, index, line, col, 3); default: break; } return factory.create(TokenType.LessOrGreater, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.LessEqual, index, line, col, 2); } return factory.create(TokenType.LessThan, index, line, col); case '>': switch (next) { case '>': in_stream.read(); int p = in_stream.peek(); if (p != -1) { switch ((char)p) { case '=': in_stream.read(); return factory.create(TokenType.ShiftRightAssign, index, line, col, 3); case '>': in_stream.read(); int q = in_stream.peek(); if (q != -1 && q == '=') { in_stream.read(); return factory.create(TokenType.TripleRightShiftAssign, index, line, col, 4); } return factory.create(TokenType.ShiftRightUnsigned, index, line, col, 3); } } return factory.create(TokenType.ShiftRight, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.GreaterEqual, index, line, col, 2); } return factory.create(TokenType.GreaterThan, index, line, col); case '?': return factory.create(TokenType.Question, index, line, col); case '$': return factory.create(TokenType.Dollar, index, line, col); case ';': return factory.create(TokenType.Semicolon, index, line, col); case ':': return factory.create(TokenType.Colon, index, line, col); case ',': return factory.create(TokenType.Comma, index, line, col); case '.': if (next == '.') { in_stream.read(); int p = in_stream.peek(); if (p != -1 && p == '.') { in_stream.read(); return factory.create(TokenType.TripleDot, index, line, col, 3); } return factory.create(TokenType.DoubleDot, index, line, col, 2); } return factory.create(TokenType.Dot, index, line, col); case ')': return factory.create(TokenType.CloseParenthesis, index, line, col); case '(': return factory.create(TokenType.OpenParenthesis, index, line, col); case ']': return factory.create(TokenType.CloseSquareBracket, index, line, col); case '[': return factory.create(TokenType.OpenSquareBracket, index, line, col); case '}': return factory.create(TokenType.CloseCurlyBrace, index, line, col); case '{': return factory.create(TokenType.OpenCurlyBrace, index, line, col); case '#': return factory.create(TokenType.Hash, index, line, col); default: return null; } } LexOperator(TokenFactory factory, LexerStream in_stream); Token read(); }
@Test public void dot() throws Exception { String str = "a.b .3"; LexerStream ls = new LexerStream(new StringReader(str)); LexOperator lex = new LexOperator(new BaseTokenFactory(), ls); ls.read(); Token tok = lex.read(); assertEquals(TokenType.Dot, tok.getType()); assertTrue(tok.getLine() == 1 && tok.getEndLine() == 1); assertEquals(2, tok.getCol()); assertEquals(3, tok.getEndCol()); ls.read(); ls.read(); try { tok = lex.read(); } catch (LexerException e) { return; } fail(); }
public Token read() throws IOException, LexerException { int index = in_stream.getPosition(); int line = in_stream.getLine(); int col = in_stream.getCol(); int next = in_stream.peek(); if (next == -1) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Unexpected end of input stream when parsing operator"); } if (next == '.') { int tmp = in_stream.peek(2); if (tmp > 0 && Character.isDigit(tmp)) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Literal float encountered when operator '.' expected"); } } char cur = (char)in_stream.read(); next = in_stream.peek(); switch (cur) { case '+': switch (next) { case '+': in_stream.read(); return factory.create(TokenType.Increment, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.PlusAssign, index, line, col, 2); } return factory.create(TokenType.Plus, index, line, col); case '-': switch (next) { case '-': in_stream.read(); return factory.create(TokenType.Decrement, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.MinusAssign, index, line, col, 2); } return factory.create(TokenType.Minus, index, line, col); case '*': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.TimesAssign, index, line, col, 2); default: break; } return factory.create(TokenType.Times, index, line, col); case '/': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.DivAssign, index, line, col, 2); } return factory.create(TokenType.Div, index, line, col); case '%': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.ModAssign, index, line, col, 2); } return factory.create(TokenType.Mod, index, line, col); case '&': switch (next) { case '&': in_stream.read(); return factory.create(TokenType.LogicalAnd, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.BitwiseAndAssign, index, line, col, 2); } return factory.create(TokenType.BitwiseAnd, index, line, col); case '|': switch (next) { case '|': in_stream.read(); return factory.create(TokenType.LogicalOr, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.BitwiseOrAssign, index, line, col, 2); } return factory.create(TokenType.BitwiseOr, index, line, col); case '^': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.XorAssign, index, line, col, 2); case '^': in_stream.read(); if (in_stream.peek() == '=') { in_stream.read(); return factory.create(TokenType.PowAssign, index, line, col, 3); } return factory.create(TokenType.Pow, index, line, col, 2); } return factory.create(TokenType.Xor, index, line, col); case '!': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.NotEqual, index, line, col, 2); case '<': in_stream.read(); switch (in_stream.peek()) { case '=': in_stream.read(); return factory.create(TokenType.UnorderedOrGreater, index, line, col, 3); case '>': in_stream.read(); switch (in_stream.peek()) { case '=': in_stream.read(); return factory.create(TokenType.Unordered, index, line, col, 4); } return factory.create(TokenType.UnorderedOrEqual, index, line, col, 3); } return factory.create(TokenType.UnorderedGreaterOrEqual, index, line, col, 2); case '>': in_stream.read(); switch (in_stream.peek()) { case '=': in_stream.read(); return factory.create(TokenType.UnorderedOrLess, index, line, col, 3); default: break; } return factory.create(TokenType.UnorderedLessOrEqual, index, line, col, 2); } return factory.create(TokenType.Not, index, line, col); case '~': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.TildeAssign, index, line, col, 2); } return factory.create(TokenType.Tilde, index, line, col); case '=': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.Equal, index, line, col, 2); case '>': in_stream.read(); return factory.create(TokenType.GoesTo, index, line, col, 2); } return factory.create(TokenType.Assign, index, line, col); case '<': switch (next) { case '<': in_stream.read(); switch (in_stream.peek()) { case '=': in_stream.read(); return factory.create(TokenType.ShiftLeftAssign, index, line, col, 3); default: break; } return factory.create(TokenType.ShiftLeft, index, line, col, 2); case '>': in_stream.read(); switch (in_stream.peek()) { case '=': in_stream.read(); return factory.create(TokenType.LessEqualOrGreater, index, line, col, 3); default: break; } return factory.create(TokenType.LessOrGreater, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.LessEqual, index, line, col, 2); } return factory.create(TokenType.LessThan, index, line, col); case '>': switch (next) { case '>': in_stream.read(); int p = in_stream.peek(); if (p != -1) { switch ((char)p) { case '=': in_stream.read(); return factory.create(TokenType.ShiftRightAssign, index, line, col, 3); case '>': in_stream.read(); int q = in_stream.peek(); if (q != -1 && q == '=') { in_stream.read(); return factory.create(TokenType.TripleRightShiftAssign, index, line, col, 4); } return factory.create(TokenType.ShiftRightUnsigned, index, line, col, 3); } } return factory.create(TokenType.ShiftRight, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.GreaterEqual, index, line, col, 2); } return factory.create(TokenType.GreaterThan, index, line, col); case '?': return factory.create(TokenType.Question, index, line, col); case '$': return factory.create(TokenType.Dollar, index, line, col); case ';': return factory.create(TokenType.Semicolon, index, line, col); case ':': return factory.create(TokenType.Colon, index, line, col); case ',': return factory.create(TokenType.Comma, index, line, col); case '.': if (next == '.') { in_stream.read(); int p = in_stream.peek(); if (p != -1 && p == '.') { in_stream.read(); return factory.create(TokenType.TripleDot, index, line, col, 3); } return factory.create(TokenType.DoubleDot, index, line, col, 2); } return factory.create(TokenType.Dot, index, line, col); case ')': return factory.create(TokenType.CloseParenthesis, index, line, col); case '(': return factory.create(TokenType.OpenParenthesis, index, line, col); case ']': return factory.create(TokenType.CloseSquareBracket, index, line, col); case '[': return factory.create(TokenType.OpenSquareBracket, index, line, col); case '}': return factory.create(TokenType.CloseCurlyBrace, index, line, col); case '{': return factory.create(TokenType.OpenCurlyBrace, index, line, col); case '#': return factory.create(TokenType.Hash, index, line, col); default: return null; } }
LexOperator { public Token read() throws IOException, LexerException { int index = in_stream.getPosition(); int line = in_stream.getLine(); int col = in_stream.getCol(); int next = in_stream.peek(); if (next == -1) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Unexpected end of input stream when parsing operator"); } if (next == '.') { int tmp = in_stream.peek(2); if (tmp > 0 && Character.isDigit(tmp)) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Literal float encountered when operator '.' expected"); } } char cur = (char)in_stream.read(); next = in_stream.peek(); switch (cur) { case '+': switch (next) { case '+': in_stream.read(); return factory.create(TokenType.Increment, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.PlusAssign, index, line, col, 2); } return factory.create(TokenType.Plus, index, line, col); case '-': switch (next) { case '-': in_stream.read(); return factory.create(TokenType.Decrement, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.MinusAssign, index, line, col, 2); } return factory.create(TokenType.Minus, index, line, col); case '*': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.TimesAssign, index, line, col, 2); default: break; } return factory.create(TokenType.Times, index, line, col); case '/': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.DivAssign, index, line, col, 2); } return factory.create(TokenType.Div, index, line, col); case '%': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.ModAssign, index, line, col, 2); } return factory.create(TokenType.Mod, index, line, col); case '&': switch (next) { case '&': in_stream.read(); return factory.create(TokenType.LogicalAnd, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.BitwiseAndAssign, index, line, col, 2); } return factory.create(TokenType.BitwiseAnd, index, line, col); case '|': switch (next) { case '|': in_stream.read(); return factory.create(TokenType.LogicalOr, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.BitwiseOrAssign, index, line, col, 2); } return factory.create(TokenType.BitwiseOr, index, line, col); case '^': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.XorAssign, index, line, col, 2); case '^': in_stream.read(); if (in_stream.peek() == '=') { in_stream.read(); return factory.create(TokenType.PowAssign, index, line, col, 3); } return factory.create(TokenType.Pow, index, line, col, 2); } return factory.create(TokenType.Xor, index, line, col); case '!': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.NotEqual, index, line, col, 2); case '<': in_stream.read(); switch (in_stream.peek()) { case '=': in_stream.read(); return factory.create(TokenType.UnorderedOrGreater, index, line, col, 3); case '>': in_stream.read(); switch (in_stream.peek()) { case '=': in_stream.read(); return factory.create(TokenType.Unordered, index, line, col, 4); } return factory.create(TokenType.UnorderedOrEqual, index, line, col, 3); } return factory.create(TokenType.UnorderedGreaterOrEqual, index, line, col, 2); case '>': in_stream.read(); switch (in_stream.peek()) { case '=': in_stream.read(); return factory.create(TokenType.UnorderedOrLess, index, line, col, 3); default: break; } return factory.create(TokenType.UnorderedLessOrEqual, index, line, col, 2); } return factory.create(TokenType.Not, index, line, col); case '~': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.TildeAssign, index, line, col, 2); } return factory.create(TokenType.Tilde, index, line, col); case '=': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.Equal, index, line, col, 2); case '>': in_stream.read(); return factory.create(TokenType.GoesTo, index, line, col, 2); } return factory.create(TokenType.Assign, index, line, col); case '<': switch (next) { case '<': in_stream.read(); switch (in_stream.peek()) { case '=': in_stream.read(); return factory.create(TokenType.ShiftLeftAssign, index, line, col, 3); default: break; } return factory.create(TokenType.ShiftLeft, index, line, col, 2); case '>': in_stream.read(); switch (in_stream.peek()) { case '=': in_stream.read(); return factory.create(TokenType.LessEqualOrGreater, index, line, col, 3); default: break; } return factory.create(TokenType.LessOrGreater, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.LessEqual, index, line, col, 2); } return factory.create(TokenType.LessThan, index, line, col); case '>': switch (next) { case '>': in_stream.read(); int p = in_stream.peek(); if (p != -1) { switch ((char)p) { case '=': in_stream.read(); return factory.create(TokenType.ShiftRightAssign, index, line, col, 3); case '>': in_stream.read(); int q = in_stream.peek(); if (q != -1 && q == '=') { in_stream.read(); return factory.create(TokenType.TripleRightShiftAssign, index, line, col, 4); } return factory.create(TokenType.ShiftRightUnsigned, index, line, col, 3); } } return factory.create(TokenType.ShiftRight, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.GreaterEqual, index, line, col, 2); } return factory.create(TokenType.GreaterThan, index, line, col); case '?': return factory.create(TokenType.Question, index, line, col); case '$': return factory.create(TokenType.Dollar, index, line, col); case ';': return factory.create(TokenType.Semicolon, index, line, col); case ':': return factory.create(TokenType.Colon, index, line, col); case ',': return factory.create(TokenType.Comma, index, line, col); case '.': if (next == '.') { in_stream.read(); int p = in_stream.peek(); if (p != -1 && p == '.') { in_stream.read(); return factory.create(TokenType.TripleDot, index, line, col, 3); } return factory.create(TokenType.DoubleDot, index, line, col, 2); } return factory.create(TokenType.Dot, index, line, col); case ')': return factory.create(TokenType.CloseParenthesis, index, line, col); case '(': return factory.create(TokenType.OpenParenthesis, index, line, col); case ']': return factory.create(TokenType.CloseSquareBracket, index, line, col); case '[': return factory.create(TokenType.OpenSquareBracket, index, line, col); case '}': return factory.create(TokenType.CloseCurlyBrace, index, line, col); case '{': return factory.create(TokenType.OpenCurlyBrace, index, line, col); case '#': return factory.create(TokenType.Hash, index, line, col); default: return null; } } }
LexOperator { public Token read() throws IOException, LexerException { int index = in_stream.getPosition(); int line = in_stream.getLine(); int col = in_stream.getCol(); int next = in_stream.peek(); if (next == -1) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Unexpected end of input stream when parsing operator"); } if (next == '.') { int tmp = in_stream.peek(2); if (tmp > 0 && Character.isDigit(tmp)) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Literal float encountered when operator '.' expected"); } } char cur = (char)in_stream.read(); next = in_stream.peek(); switch (cur) { case '+': switch (next) { case '+': in_stream.read(); return factory.create(TokenType.Increment, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.PlusAssign, index, line, col, 2); } return factory.create(TokenType.Plus, index, line, col); case '-': switch (next) { case '-': in_stream.read(); return factory.create(TokenType.Decrement, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.MinusAssign, index, line, col, 2); } return factory.create(TokenType.Minus, index, line, col); case '*': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.TimesAssign, index, line, col, 2); default: break; } return factory.create(TokenType.Times, index, line, col); case '/': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.DivAssign, index, line, col, 2); } return factory.create(TokenType.Div, index, line, col); case '%': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.ModAssign, index, line, col, 2); } return factory.create(TokenType.Mod, index, line, col); case '&': switch (next) { case '&': in_stream.read(); return factory.create(TokenType.LogicalAnd, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.BitwiseAndAssign, index, line, col, 2); } return factory.create(TokenType.BitwiseAnd, index, line, col); case '|': switch (next) { case '|': in_stream.read(); return factory.create(TokenType.LogicalOr, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.BitwiseOrAssign, index, line, col, 2); } return factory.create(TokenType.BitwiseOr, index, line, col); case '^': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.XorAssign, index, line, col, 2); case '^': in_stream.read(); if (in_stream.peek() == '=') { in_stream.read(); return factory.create(TokenType.PowAssign, index, line, col, 3); } return factory.create(TokenType.Pow, index, line, col, 2); } return factory.create(TokenType.Xor, index, line, col); case '!': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.NotEqual, index, line, col, 2); case '<': in_stream.read(); switch (in_stream.peek()) { case '=': in_stream.read(); return factory.create(TokenType.UnorderedOrGreater, index, line, col, 3); case '>': in_stream.read(); switch (in_stream.peek()) { case '=': in_stream.read(); return factory.create(TokenType.Unordered, index, line, col, 4); } return factory.create(TokenType.UnorderedOrEqual, index, line, col, 3); } return factory.create(TokenType.UnorderedGreaterOrEqual, index, line, col, 2); case '>': in_stream.read(); switch (in_stream.peek()) { case '=': in_stream.read(); return factory.create(TokenType.UnorderedOrLess, index, line, col, 3); default: break; } return factory.create(TokenType.UnorderedLessOrEqual, index, line, col, 2); } return factory.create(TokenType.Not, index, line, col); case '~': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.TildeAssign, index, line, col, 2); } return factory.create(TokenType.Tilde, index, line, col); case '=': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.Equal, index, line, col, 2); case '>': in_stream.read(); return factory.create(TokenType.GoesTo, index, line, col, 2); } return factory.create(TokenType.Assign, index, line, col); case '<': switch (next) { case '<': in_stream.read(); switch (in_stream.peek()) { case '=': in_stream.read(); return factory.create(TokenType.ShiftLeftAssign, index, line, col, 3); default: break; } return factory.create(TokenType.ShiftLeft, index, line, col, 2); case '>': in_stream.read(); switch (in_stream.peek()) { case '=': in_stream.read(); return factory.create(TokenType.LessEqualOrGreater, index, line, col, 3); default: break; } return factory.create(TokenType.LessOrGreater, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.LessEqual, index, line, col, 2); } return factory.create(TokenType.LessThan, index, line, col); case '>': switch (next) { case '>': in_stream.read(); int p = in_stream.peek(); if (p != -1) { switch ((char)p) { case '=': in_stream.read(); return factory.create(TokenType.ShiftRightAssign, index, line, col, 3); case '>': in_stream.read(); int q = in_stream.peek(); if (q != -1 && q == '=') { in_stream.read(); return factory.create(TokenType.TripleRightShiftAssign, index, line, col, 4); } return factory.create(TokenType.ShiftRightUnsigned, index, line, col, 3); } } return factory.create(TokenType.ShiftRight, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.GreaterEqual, index, line, col, 2); } return factory.create(TokenType.GreaterThan, index, line, col); case '?': return factory.create(TokenType.Question, index, line, col); case '$': return factory.create(TokenType.Dollar, index, line, col); case ';': return factory.create(TokenType.Semicolon, index, line, col); case ':': return factory.create(TokenType.Colon, index, line, col); case ',': return factory.create(TokenType.Comma, index, line, col); case '.': if (next == '.') { in_stream.read(); int p = in_stream.peek(); if (p != -1 && p == '.') { in_stream.read(); return factory.create(TokenType.TripleDot, index, line, col, 3); } return factory.create(TokenType.DoubleDot, index, line, col, 2); } return factory.create(TokenType.Dot, index, line, col); case ')': return factory.create(TokenType.CloseParenthesis, index, line, col); case '(': return factory.create(TokenType.OpenParenthesis, index, line, col); case ']': return factory.create(TokenType.CloseSquareBracket, index, line, col); case '[': return factory.create(TokenType.OpenSquareBracket, index, line, col); case '}': return factory.create(TokenType.CloseCurlyBrace, index, line, col); case '{': return factory.create(TokenType.OpenCurlyBrace, index, line, col); case '#': return factory.create(TokenType.Hash, index, line, col); default: return null; } } LexOperator(TokenFactory factory, LexerStream in_stream); }
LexOperator { public Token read() throws IOException, LexerException { int index = in_stream.getPosition(); int line = in_stream.getLine(); int col = in_stream.getCol(); int next = in_stream.peek(); if (next == -1) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Unexpected end of input stream when parsing operator"); } if (next == '.') { int tmp = in_stream.peek(2); if (tmp > 0 && Character.isDigit(tmp)) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Literal float encountered when operator '.' expected"); } } char cur = (char)in_stream.read(); next = in_stream.peek(); switch (cur) { case '+': switch (next) { case '+': in_stream.read(); return factory.create(TokenType.Increment, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.PlusAssign, index, line, col, 2); } return factory.create(TokenType.Plus, index, line, col); case '-': switch (next) { case '-': in_stream.read(); return factory.create(TokenType.Decrement, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.MinusAssign, index, line, col, 2); } return factory.create(TokenType.Minus, index, line, col); case '*': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.TimesAssign, index, line, col, 2); default: break; } return factory.create(TokenType.Times, index, line, col); case '/': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.DivAssign, index, line, col, 2); } return factory.create(TokenType.Div, index, line, col); case '%': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.ModAssign, index, line, col, 2); } return factory.create(TokenType.Mod, index, line, col); case '&': switch (next) { case '&': in_stream.read(); return factory.create(TokenType.LogicalAnd, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.BitwiseAndAssign, index, line, col, 2); } return factory.create(TokenType.BitwiseAnd, index, line, col); case '|': switch (next) { case '|': in_stream.read(); return factory.create(TokenType.LogicalOr, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.BitwiseOrAssign, index, line, col, 2); } return factory.create(TokenType.BitwiseOr, index, line, col); case '^': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.XorAssign, index, line, col, 2); case '^': in_stream.read(); if (in_stream.peek() == '=') { in_stream.read(); return factory.create(TokenType.PowAssign, index, line, col, 3); } return factory.create(TokenType.Pow, index, line, col, 2); } return factory.create(TokenType.Xor, index, line, col); case '!': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.NotEqual, index, line, col, 2); case '<': in_stream.read(); switch (in_stream.peek()) { case '=': in_stream.read(); return factory.create(TokenType.UnorderedOrGreater, index, line, col, 3); case '>': in_stream.read(); switch (in_stream.peek()) { case '=': in_stream.read(); return factory.create(TokenType.Unordered, index, line, col, 4); } return factory.create(TokenType.UnorderedOrEqual, index, line, col, 3); } return factory.create(TokenType.UnorderedGreaterOrEqual, index, line, col, 2); case '>': in_stream.read(); switch (in_stream.peek()) { case '=': in_stream.read(); return factory.create(TokenType.UnorderedOrLess, index, line, col, 3); default: break; } return factory.create(TokenType.UnorderedLessOrEqual, index, line, col, 2); } return factory.create(TokenType.Not, index, line, col); case '~': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.TildeAssign, index, line, col, 2); } return factory.create(TokenType.Tilde, index, line, col); case '=': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.Equal, index, line, col, 2); case '>': in_stream.read(); return factory.create(TokenType.GoesTo, index, line, col, 2); } return factory.create(TokenType.Assign, index, line, col); case '<': switch (next) { case '<': in_stream.read(); switch (in_stream.peek()) { case '=': in_stream.read(); return factory.create(TokenType.ShiftLeftAssign, index, line, col, 3); default: break; } return factory.create(TokenType.ShiftLeft, index, line, col, 2); case '>': in_stream.read(); switch (in_stream.peek()) { case '=': in_stream.read(); return factory.create(TokenType.LessEqualOrGreater, index, line, col, 3); default: break; } return factory.create(TokenType.LessOrGreater, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.LessEqual, index, line, col, 2); } return factory.create(TokenType.LessThan, index, line, col); case '>': switch (next) { case '>': in_stream.read(); int p = in_stream.peek(); if (p != -1) { switch ((char)p) { case '=': in_stream.read(); return factory.create(TokenType.ShiftRightAssign, index, line, col, 3); case '>': in_stream.read(); int q = in_stream.peek(); if (q != -1 && q == '=') { in_stream.read(); return factory.create(TokenType.TripleRightShiftAssign, index, line, col, 4); } return factory.create(TokenType.ShiftRightUnsigned, index, line, col, 3); } } return factory.create(TokenType.ShiftRight, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.GreaterEqual, index, line, col, 2); } return factory.create(TokenType.GreaterThan, index, line, col); case '?': return factory.create(TokenType.Question, index, line, col); case '$': return factory.create(TokenType.Dollar, index, line, col); case ';': return factory.create(TokenType.Semicolon, index, line, col); case ':': return factory.create(TokenType.Colon, index, line, col); case ',': return factory.create(TokenType.Comma, index, line, col); case '.': if (next == '.') { in_stream.read(); int p = in_stream.peek(); if (p != -1 && p == '.') { in_stream.read(); return factory.create(TokenType.TripleDot, index, line, col, 3); } return factory.create(TokenType.DoubleDot, index, line, col, 2); } return factory.create(TokenType.Dot, index, line, col); case ')': return factory.create(TokenType.CloseParenthesis, index, line, col); case '(': return factory.create(TokenType.OpenParenthesis, index, line, col); case ']': return factory.create(TokenType.CloseSquareBracket, index, line, col); case '[': return factory.create(TokenType.OpenSquareBracket, index, line, col); case '}': return factory.create(TokenType.CloseCurlyBrace, index, line, col); case '{': return factory.create(TokenType.OpenCurlyBrace, index, line, col); case '#': return factory.create(TokenType.Hash, index, line, col); default: return null; } } LexOperator(TokenFactory factory, LexerStream in_stream); Token read(); }
LexOperator { public Token read() throws IOException, LexerException { int index = in_stream.getPosition(); int line = in_stream.getLine(); int col = in_stream.getCol(); int next = in_stream.peek(); if (next == -1) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Unexpected end of input stream when parsing operator"); } if (next == '.') { int tmp = in_stream.peek(2); if (tmp > 0 && Character.isDigit(tmp)) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Literal float encountered when operator '.' expected"); } } char cur = (char)in_stream.read(); next = in_stream.peek(); switch (cur) { case '+': switch (next) { case '+': in_stream.read(); return factory.create(TokenType.Increment, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.PlusAssign, index, line, col, 2); } return factory.create(TokenType.Plus, index, line, col); case '-': switch (next) { case '-': in_stream.read(); return factory.create(TokenType.Decrement, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.MinusAssign, index, line, col, 2); } return factory.create(TokenType.Minus, index, line, col); case '*': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.TimesAssign, index, line, col, 2); default: break; } return factory.create(TokenType.Times, index, line, col); case '/': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.DivAssign, index, line, col, 2); } return factory.create(TokenType.Div, index, line, col); case '%': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.ModAssign, index, line, col, 2); } return factory.create(TokenType.Mod, index, line, col); case '&': switch (next) { case '&': in_stream.read(); return factory.create(TokenType.LogicalAnd, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.BitwiseAndAssign, index, line, col, 2); } return factory.create(TokenType.BitwiseAnd, index, line, col); case '|': switch (next) { case '|': in_stream.read(); return factory.create(TokenType.LogicalOr, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.BitwiseOrAssign, index, line, col, 2); } return factory.create(TokenType.BitwiseOr, index, line, col); case '^': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.XorAssign, index, line, col, 2); case '^': in_stream.read(); if (in_stream.peek() == '=') { in_stream.read(); return factory.create(TokenType.PowAssign, index, line, col, 3); } return factory.create(TokenType.Pow, index, line, col, 2); } return factory.create(TokenType.Xor, index, line, col); case '!': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.NotEqual, index, line, col, 2); case '<': in_stream.read(); switch (in_stream.peek()) { case '=': in_stream.read(); return factory.create(TokenType.UnorderedOrGreater, index, line, col, 3); case '>': in_stream.read(); switch (in_stream.peek()) { case '=': in_stream.read(); return factory.create(TokenType.Unordered, index, line, col, 4); } return factory.create(TokenType.UnorderedOrEqual, index, line, col, 3); } return factory.create(TokenType.UnorderedGreaterOrEqual, index, line, col, 2); case '>': in_stream.read(); switch (in_stream.peek()) { case '=': in_stream.read(); return factory.create(TokenType.UnorderedOrLess, index, line, col, 3); default: break; } return factory.create(TokenType.UnorderedLessOrEqual, index, line, col, 2); } return factory.create(TokenType.Not, index, line, col); case '~': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.TildeAssign, index, line, col, 2); } return factory.create(TokenType.Tilde, index, line, col); case '=': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.Equal, index, line, col, 2); case '>': in_stream.read(); return factory.create(TokenType.GoesTo, index, line, col, 2); } return factory.create(TokenType.Assign, index, line, col); case '<': switch (next) { case '<': in_stream.read(); switch (in_stream.peek()) { case '=': in_stream.read(); return factory.create(TokenType.ShiftLeftAssign, index, line, col, 3); default: break; } return factory.create(TokenType.ShiftLeft, index, line, col, 2); case '>': in_stream.read(); switch (in_stream.peek()) { case '=': in_stream.read(); return factory.create(TokenType.LessEqualOrGreater, index, line, col, 3); default: break; } return factory.create(TokenType.LessOrGreater, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.LessEqual, index, line, col, 2); } return factory.create(TokenType.LessThan, index, line, col); case '>': switch (next) { case '>': in_stream.read(); int p = in_stream.peek(); if (p != -1) { switch ((char)p) { case '=': in_stream.read(); return factory.create(TokenType.ShiftRightAssign, index, line, col, 3); case '>': in_stream.read(); int q = in_stream.peek(); if (q != -1 && q == '=') { in_stream.read(); return factory.create(TokenType.TripleRightShiftAssign, index, line, col, 4); } return factory.create(TokenType.ShiftRightUnsigned, index, line, col, 3); } } return factory.create(TokenType.ShiftRight, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.GreaterEqual, index, line, col, 2); } return factory.create(TokenType.GreaterThan, index, line, col); case '?': return factory.create(TokenType.Question, index, line, col); case '$': return factory.create(TokenType.Dollar, index, line, col); case ';': return factory.create(TokenType.Semicolon, index, line, col); case ':': return factory.create(TokenType.Colon, index, line, col); case ',': return factory.create(TokenType.Comma, index, line, col); case '.': if (next == '.') { in_stream.read(); int p = in_stream.peek(); if (p != -1 && p == '.') { in_stream.read(); return factory.create(TokenType.TripleDot, index, line, col, 3); } return factory.create(TokenType.DoubleDot, index, line, col, 2); } return factory.create(TokenType.Dot, index, line, col); case ')': return factory.create(TokenType.CloseParenthesis, index, line, col); case '(': return factory.create(TokenType.OpenParenthesis, index, line, col); case ']': return factory.create(TokenType.CloseSquareBracket, index, line, col); case '[': return factory.create(TokenType.OpenSquareBracket, index, line, col); case '}': return factory.create(TokenType.CloseCurlyBrace, index, line, col); case '{': return factory.create(TokenType.OpenCurlyBrace, index, line, col); case '#': return factory.create(TokenType.Hash, index, line, col); default: return null; } } LexOperator(TokenFactory factory, LexerStream in_stream); Token read(); }
@Test public void range() throws Exception { String str = "1..2...3,4"; LexerStream ls = new LexerStream(new StringReader(str)); LexOperator lex = new LexOperator(new BaseTokenFactory(), ls); ls.read(); Token tok = lex.read(); assertEquals(TokenType.DoubleDot, tok.getType()); assertTrue(tok.getLine() == 1 && tok.getEndLine() == 1); assertEquals(2, tok.getCol()); assertEquals(4, tok.getEndCol()); ls.read(); tok = lex.read(); assertEquals(TokenType.TripleDot, tok.getType()); assertTrue(tok.getLine() == 1 && tok.getEndLine() == 1); assertEquals(5, tok.getCol()); assertEquals(8, tok.getEndCol()); ls.read(); tok = lex.read(); assertEquals(TokenType.Comma, tok.getType()); assertTrue(tok.getLine() == 1 && tok.getEndLine() == 1); assertEquals(9, tok.getCol()); assertEquals(10, tok.getEndCol()); }
public Token read() throws IOException, LexerException { int index = in_stream.getPosition(); int line = in_stream.getLine(); int col = in_stream.getCol(); int next = in_stream.peek(); if (next == -1) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Unexpected end of input stream when parsing operator"); } if (next == '.') { int tmp = in_stream.peek(2); if (tmp > 0 && Character.isDigit(tmp)) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Literal float encountered when operator '.' expected"); } } char cur = (char)in_stream.read(); next = in_stream.peek(); switch (cur) { case '+': switch (next) { case '+': in_stream.read(); return factory.create(TokenType.Increment, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.PlusAssign, index, line, col, 2); } return factory.create(TokenType.Plus, index, line, col); case '-': switch (next) { case '-': in_stream.read(); return factory.create(TokenType.Decrement, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.MinusAssign, index, line, col, 2); } return factory.create(TokenType.Minus, index, line, col); case '*': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.TimesAssign, index, line, col, 2); default: break; } return factory.create(TokenType.Times, index, line, col); case '/': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.DivAssign, index, line, col, 2); } return factory.create(TokenType.Div, index, line, col); case '%': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.ModAssign, index, line, col, 2); } return factory.create(TokenType.Mod, index, line, col); case '&': switch (next) { case '&': in_stream.read(); return factory.create(TokenType.LogicalAnd, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.BitwiseAndAssign, index, line, col, 2); } return factory.create(TokenType.BitwiseAnd, index, line, col); case '|': switch (next) { case '|': in_stream.read(); return factory.create(TokenType.LogicalOr, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.BitwiseOrAssign, index, line, col, 2); } return factory.create(TokenType.BitwiseOr, index, line, col); case '^': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.XorAssign, index, line, col, 2); case '^': in_stream.read(); if (in_stream.peek() == '=') { in_stream.read(); return factory.create(TokenType.PowAssign, index, line, col, 3); } return factory.create(TokenType.Pow, index, line, col, 2); } return factory.create(TokenType.Xor, index, line, col); case '!': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.NotEqual, index, line, col, 2); case '<': in_stream.read(); switch (in_stream.peek()) { case '=': in_stream.read(); return factory.create(TokenType.UnorderedOrGreater, index, line, col, 3); case '>': in_stream.read(); switch (in_stream.peek()) { case '=': in_stream.read(); return factory.create(TokenType.Unordered, index, line, col, 4); } return factory.create(TokenType.UnorderedOrEqual, index, line, col, 3); } return factory.create(TokenType.UnorderedGreaterOrEqual, index, line, col, 2); case '>': in_stream.read(); switch (in_stream.peek()) { case '=': in_stream.read(); return factory.create(TokenType.UnorderedOrLess, index, line, col, 3); default: break; } return factory.create(TokenType.UnorderedLessOrEqual, index, line, col, 2); } return factory.create(TokenType.Not, index, line, col); case '~': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.TildeAssign, index, line, col, 2); } return factory.create(TokenType.Tilde, index, line, col); case '=': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.Equal, index, line, col, 2); case '>': in_stream.read(); return factory.create(TokenType.GoesTo, index, line, col, 2); } return factory.create(TokenType.Assign, index, line, col); case '<': switch (next) { case '<': in_stream.read(); switch (in_stream.peek()) { case '=': in_stream.read(); return factory.create(TokenType.ShiftLeftAssign, index, line, col, 3); default: break; } return factory.create(TokenType.ShiftLeft, index, line, col, 2); case '>': in_stream.read(); switch (in_stream.peek()) { case '=': in_stream.read(); return factory.create(TokenType.LessEqualOrGreater, index, line, col, 3); default: break; } return factory.create(TokenType.LessOrGreater, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.LessEqual, index, line, col, 2); } return factory.create(TokenType.LessThan, index, line, col); case '>': switch (next) { case '>': in_stream.read(); int p = in_stream.peek(); if (p != -1) { switch ((char)p) { case '=': in_stream.read(); return factory.create(TokenType.ShiftRightAssign, index, line, col, 3); case '>': in_stream.read(); int q = in_stream.peek(); if (q != -1 && q == '=') { in_stream.read(); return factory.create(TokenType.TripleRightShiftAssign, index, line, col, 4); } return factory.create(TokenType.ShiftRightUnsigned, index, line, col, 3); } } return factory.create(TokenType.ShiftRight, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.GreaterEqual, index, line, col, 2); } return factory.create(TokenType.GreaterThan, index, line, col); case '?': return factory.create(TokenType.Question, index, line, col); case '$': return factory.create(TokenType.Dollar, index, line, col); case ';': return factory.create(TokenType.Semicolon, index, line, col); case ':': return factory.create(TokenType.Colon, index, line, col); case ',': return factory.create(TokenType.Comma, index, line, col); case '.': if (next == '.') { in_stream.read(); int p = in_stream.peek(); if (p != -1 && p == '.') { in_stream.read(); return factory.create(TokenType.TripleDot, index, line, col, 3); } return factory.create(TokenType.DoubleDot, index, line, col, 2); } return factory.create(TokenType.Dot, index, line, col); case ')': return factory.create(TokenType.CloseParenthesis, index, line, col); case '(': return factory.create(TokenType.OpenParenthesis, index, line, col); case ']': return factory.create(TokenType.CloseSquareBracket, index, line, col); case '[': return factory.create(TokenType.OpenSquareBracket, index, line, col); case '}': return factory.create(TokenType.CloseCurlyBrace, index, line, col); case '{': return factory.create(TokenType.OpenCurlyBrace, index, line, col); case '#': return factory.create(TokenType.Hash, index, line, col); default: return null; } }
LexOperator { public Token read() throws IOException, LexerException { int index = in_stream.getPosition(); int line = in_stream.getLine(); int col = in_stream.getCol(); int next = in_stream.peek(); if (next == -1) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Unexpected end of input stream when parsing operator"); } if (next == '.') { int tmp = in_stream.peek(2); if (tmp > 0 && Character.isDigit(tmp)) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Literal float encountered when operator '.' expected"); } } char cur = (char)in_stream.read(); next = in_stream.peek(); switch (cur) { case '+': switch (next) { case '+': in_stream.read(); return factory.create(TokenType.Increment, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.PlusAssign, index, line, col, 2); } return factory.create(TokenType.Plus, index, line, col); case '-': switch (next) { case '-': in_stream.read(); return factory.create(TokenType.Decrement, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.MinusAssign, index, line, col, 2); } return factory.create(TokenType.Minus, index, line, col); case '*': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.TimesAssign, index, line, col, 2); default: break; } return factory.create(TokenType.Times, index, line, col); case '/': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.DivAssign, index, line, col, 2); } return factory.create(TokenType.Div, index, line, col); case '%': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.ModAssign, index, line, col, 2); } return factory.create(TokenType.Mod, index, line, col); case '&': switch (next) { case '&': in_stream.read(); return factory.create(TokenType.LogicalAnd, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.BitwiseAndAssign, index, line, col, 2); } return factory.create(TokenType.BitwiseAnd, index, line, col); case '|': switch (next) { case '|': in_stream.read(); return factory.create(TokenType.LogicalOr, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.BitwiseOrAssign, index, line, col, 2); } return factory.create(TokenType.BitwiseOr, index, line, col); case '^': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.XorAssign, index, line, col, 2); case '^': in_stream.read(); if (in_stream.peek() == '=') { in_stream.read(); return factory.create(TokenType.PowAssign, index, line, col, 3); } return factory.create(TokenType.Pow, index, line, col, 2); } return factory.create(TokenType.Xor, index, line, col); case '!': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.NotEqual, index, line, col, 2); case '<': in_stream.read(); switch (in_stream.peek()) { case '=': in_stream.read(); return factory.create(TokenType.UnorderedOrGreater, index, line, col, 3); case '>': in_stream.read(); switch (in_stream.peek()) { case '=': in_stream.read(); return factory.create(TokenType.Unordered, index, line, col, 4); } return factory.create(TokenType.UnorderedOrEqual, index, line, col, 3); } return factory.create(TokenType.UnorderedGreaterOrEqual, index, line, col, 2); case '>': in_stream.read(); switch (in_stream.peek()) { case '=': in_stream.read(); return factory.create(TokenType.UnorderedOrLess, index, line, col, 3); default: break; } return factory.create(TokenType.UnorderedLessOrEqual, index, line, col, 2); } return factory.create(TokenType.Not, index, line, col); case '~': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.TildeAssign, index, line, col, 2); } return factory.create(TokenType.Tilde, index, line, col); case '=': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.Equal, index, line, col, 2); case '>': in_stream.read(); return factory.create(TokenType.GoesTo, index, line, col, 2); } return factory.create(TokenType.Assign, index, line, col); case '<': switch (next) { case '<': in_stream.read(); switch (in_stream.peek()) { case '=': in_stream.read(); return factory.create(TokenType.ShiftLeftAssign, index, line, col, 3); default: break; } return factory.create(TokenType.ShiftLeft, index, line, col, 2); case '>': in_stream.read(); switch (in_stream.peek()) { case '=': in_stream.read(); return factory.create(TokenType.LessEqualOrGreater, index, line, col, 3); default: break; } return factory.create(TokenType.LessOrGreater, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.LessEqual, index, line, col, 2); } return factory.create(TokenType.LessThan, index, line, col); case '>': switch (next) { case '>': in_stream.read(); int p = in_stream.peek(); if (p != -1) { switch ((char)p) { case '=': in_stream.read(); return factory.create(TokenType.ShiftRightAssign, index, line, col, 3); case '>': in_stream.read(); int q = in_stream.peek(); if (q != -1 && q == '=') { in_stream.read(); return factory.create(TokenType.TripleRightShiftAssign, index, line, col, 4); } return factory.create(TokenType.ShiftRightUnsigned, index, line, col, 3); } } return factory.create(TokenType.ShiftRight, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.GreaterEqual, index, line, col, 2); } return factory.create(TokenType.GreaterThan, index, line, col); case '?': return factory.create(TokenType.Question, index, line, col); case '$': return factory.create(TokenType.Dollar, index, line, col); case ';': return factory.create(TokenType.Semicolon, index, line, col); case ':': return factory.create(TokenType.Colon, index, line, col); case ',': return factory.create(TokenType.Comma, index, line, col); case '.': if (next == '.') { in_stream.read(); int p = in_stream.peek(); if (p != -1 && p == '.') { in_stream.read(); return factory.create(TokenType.TripleDot, index, line, col, 3); } return factory.create(TokenType.DoubleDot, index, line, col, 2); } return factory.create(TokenType.Dot, index, line, col); case ')': return factory.create(TokenType.CloseParenthesis, index, line, col); case '(': return factory.create(TokenType.OpenParenthesis, index, line, col); case ']': return factory.create(TokenType.CloseSquareBracket, index, line, col); case '[': return factory.create(TokenType.OpenSquareBracket, index, line, col); case '}': return factory.create(TokenType.CloseCurlyBrace, index, line, col); case '{': return factory.create(TokenType.OpenCurlyBrace, index, line, col); case '#': return factory.create(TokenType.Hash, index, line, col); default: return null; } } }
LexOperator { public Token read() throws IOException, LexerException { int index = in_stream.getPosition(); int line = in_stream.getLine(); int col = in_stream.getCol(); int next = in_stream.peek(); if (next == -1) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Unexpected end of input stream when parsing operator"); } if (next == '.') { int tmp = in_stream.peek(2); if (tmp > 0 && Character.isDigit(tmp)) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Literal float encountered when operator '.' expected"); } } char cur = (char)in_stream.read(); next = in_stream.peek(); switch (cur) { case '+': switch (next) { case '+': in_stream.read(); return factory.create(TokenType.Increment, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.PlusAssign, index, line, col, 2); } return factory.create(TokenType.Plus, index, line, col); case '-': switch (next) { case '-': in_stream.read(); return factory.create(TokenType.Decrement, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.MinusAssign, index, line, col, 2); } return factory.create(TokenType.Minus, index, line, col); case '*': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.TimesAssign, index, line, col, 2); default: break; } return factory.create(TokenType.Times, index, line, col); case '/': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.DivAssign, index, line, col, 2); } return factory.create(TokenType.Div, index, line, col); case '%': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.ModAssign, index, line, col, 2); } return factory.create(TokenType.Mod, index, line, col); case '&': switch (next) { case '&': in_stream.read(); return factory.create(TokenType.LogicalAnd, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.BitwiseAndAssign, index, line, col, 2); } return factory.create(TokenType.BitwiseAnd, index, line, col); case '|': switch (next) { case '|': in_stream.read(); return factory.create(TokenType.LogicalOr, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.BitwiseOrAssign, index, line, col, 2); } return factory.create(TokenType.BitwiseOr, index, line, col); case '^': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.XorAssign, index, line, col, 2); case '^': in_stream.read(); if (in_stream.peek() == '=') { in_stream.read(); return factory.create(TokenType.PowAssign, index, line, col, 3); } return factory.create(TokenType.Pow, index, line, col, 2); } return factory.create(TokenType.Xor, index, line, col); case '!': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.NotEqual, index, line, col, 2); case '<': in_stream.read(); switch (in_stream.peek()) { case '=': in_stream.read(); return factory.create(TokenType.UnorderedOrGreater, index, line, col, 3); case '>': in_stream.read(); switch (in_stream.peek()) { case '=': in_stream.read(); return factory.create(TokenType.Unordered, index, line, col, 4); } return factory.create(TokenType.UnorderedOrEqual, index, line, col, 3); } return factory.create(TokenType.UnorderedGreaterOrEqual, index, line, col, 2); case '>': in_stream.read(); switch (in_stream.peek()) { case '=': in_stream.read(); return factory.create(TokenType.UnorderedOrLess, index, line, col, 3); default: break; } return factory.create(TokenType.UnorderedLessOrEqual, index, line, col, 2); } return factory.create(TokenType.Not, index, line, col); case '~': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.TildeAssign, index, line, col, 2); } return factory.create(TokenType.Tilde, index, line, col); case '=': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.Equal, index, line, col, 2); case '>': in_stream.read(); return factory.create(TokenType.GoesTo, index, line, col, 2); } return factory.create(TokenType.Assign, index, line, col); case '<': switch (next) { case '<': in_stream.read(); switch (in_stream.peek()) { case '=': in_stream.read(); return factory.create(TokenType.ShiftLeftAssign, index, line, col, 3); default: break; } return factory.create(TokenType.ShiftLeft, index, line, col, 2); case '>': in_stream.read(); switch (in_stream.peek()) { case '=': in_stream.read(); return factory.create(TokenType.LessEqualOrGreater, index, line, col, 3); default: break; } return factory.create(TokenType.LessOrGreater, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.LessEqual, index, line, col, 2); } return factory.create(TokenType.LessThan, index, line, col); case '>': switch (next) { case '>': in_stream.read(); int p = in_stream.peek(); if (p != -1) { switch ((char)p) { case '=': in_stream.read(); return factory.create(TokenType.ShiftRightAssign, index, line, col, 3); case '>': in_stream.read(); int q = in_stream.peek(); if (q != -1 && q == '=') { in_stream.read(); return factory.create(TokenType.TripleRightShiftAssign, index, line, col, 4); } return factory.create(TokenType.ShiftRightUnsigned, index, line, col, 3); } } return factory.create(TokenType.ShiftRight, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.GreaterEqual, index, line, col, 2); } return factory.create(TokenType.GreaterThan, index, line, col); case '?': return factory.create(TokenType.Question, index, line, col); case '$': return factory.create(TokenType.Dollar, index, line, col); case ';': return factory.create(TokenType.Semicolon, index, line, col); case ':': return factory.create(TokenType.Colon, index, line, col); case ',': return factory.create(TokenType.Comma, index, line, col); case '.': if (next == '.') { in_stream.read(); int p = in_stream.peek(); if (p != -1 && p == '.') { in_stream.read(); return factory.create(TokenType.TripleDot, index, line, col, 3); } return factory.create(TokenType.DoubleDot, index, line, col, 2); } return factory.create(TokenType.Dot, index, line, col); case ')': return factory.create(TokenType.CloseParenthesis, index, line, col); case '(': return factory.create(TokenType.OpenParenthesis, index, line, col); case ']': return factory.create(TokenType.CloseSquareBracket, index, line, col); case '[': return factory.create(TokenType.OpenSquareBracket, index, line, col); case '}': return factory.create(TokenType.CloseCurlyBrace, index, line, col); case '{': return factory.create(TokenType.OpenCurlyBrace, index, line, col); case '#': return factory.create(TokenType.Hash, index, line, col); default: return null; } } LexOperator(TokenFactory factory, LexerStream in_stream); }
LexOperator { public Token read() throws IOException, LexerException { int index = in_stream.getPosition(); int line = in_stream.getLine(); int col = in_stream.getCol(); int next = in_stream.peek(); if (next == -1) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Unexpected end of input stream when parsing operator"); } if (next == '.') { int tmp = in_stream.peek(2); if (tmp > 0 && Character.isDigit(tmp)) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Literal float encountered when operator '.' expected"); } } char cur = (char)in_stream.read(); next = in_stream.peek(); switch (cur) { case '+': switch (next) { case '+': in_stream.read(); return factory.create(TokenType.Increment, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.PlusAssign, index, line, col, 2); } return factory.create(TokenType.Plus, index, line, col); case '-': switch (next) { case '-': in_stream.read(); return factory.create(TokenType.Decrement, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.MinusAssign, index, line, col, 2); } return factory.create(TokenType.Minus, index, line, col); case '*': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.TimesAssign, index, line, col, 2); default: break; } return factory.create(TokenType.Times, index, line, col); case '/': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.DivAssign, index, line, col, 2); } return factory.create(TokenType.Div, index, line, col); case '%': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.ModAssign, index, line, col, 2); } return factory.create(TokenType.Mod, index, line, col); case '&': switch (next) { case '&': in_stream.read(); return factory.create(TokenType.LogicalAnd, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.BitwiseAndAssign, index, line, col, 2); } return factory.create(TokenType.BitwiseAnd, index, line, col); case '|': switch (next) { case '|': in_stream.read(); return factory.create(TokenType.LogicalOr, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.BitwiseOrAssign, index, line, col, 2); } return factory.create(TokenType.BitwiseOr, index, line, col); case '^': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.XorAssign, index, line, col, 2); case '^': in_stream.read(); if (in_stream.peek() == '=') { in_stream.read(); return factory.create(TokenType.PowAssign, index, line, col, 3); } return factory.create(TokenType.Pow, index, line, col, 2); } return factory.create(TokenType.Xor, index, line, col); case '!': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.NotEqual, index, line, col, 2); case '<': in_stream.read(); switch (in_stream.peek()) { case '=': in_stream.read(); return factory.create(TokenType.UnorderedOrGreater, index, line, col, 3); case '>': in_stream.read(); switch (in_stream.peek()) { case '=': in_stream.read(); return factory.create(TokenType.Unordered, index, line, col, 4); } return factory.create(TokenType.UnorderedOrEqual, index, line, col, 3); } return factory.create(TokenType.UnorderedGreaterOrEqual, index, line, col, 2); case '>': in_stream.read(); switch (in_stream.peek()) { case '=': in_stream.read(); return factory.create(TokenType.UnorderedOrLess, index, line, col, 3); default: break; } return factory.create(TokenType.UnorderedLessOrEqual, index, line, col, 2); } return factory.create(TokenType.Not, index, line, col); case '~': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.TildeAssign, index, line, col, 2); } return factory.create(TokenType.Tilde, index, line, col); case '=': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.Equal, index, line, col, 2); case '>': in_stream.read(); return factory.create(TokenType.GoesTo, index, line, col, 2); } return factory.create(TokenType.Assign, index, line, col); case '<': switch (next) { case '<': in_stream.read(); switch (in_stream.peek()) { case '=': in_stream.read(); return factory.create(TokenType.ShiftLeftAssign, index, line, col, 3); default: break; } return factory.create(TokenType.ShiftLeft, index, line, col, 2); case '>': in_stream.read(); switch (in_stream.peek()) { case '=': in_stream.read(); return factory.create(TokenType.LessEqualOrGreater, index, line, col, 3); default: break; } return factory.create(TokenType.LessOrGreater, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.LessEqual, index, line, col, 2); } return factory.create(TokenType.LessThan, index, line, col); case '>': switch (next) { case '>': in_stream.read(); int p = in_stream.peek(); if (p != -1) { switch ((char)p) { case '=': in_stream.read(); return factory.create(TokenType.ShiftRightAssign, index, line, col, 3); case '>': in_stream.read(); int q = in_stream.peek(); if (q != -1 && q == '=') { in_stream.read(); return factory.create(TokenType.TripleRightShiftAssign, index, line, col, 4); } return factory.create(TokenType.ShiftRightUnsigned, index, line, col, 3); } } return factory.create(TokenType.ShiftRight, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.GreaterEqual, index, line, col, 2); } return factory.create(TokenType.GreaterThan, index, line, col); case '?': return factory.create(TokenType.Question, index, line, col); case '$': return factory.create(TokenType.Dollar, index, line, col); case ';': return factory.create(TokenType.Semicolon, index, line, col); case ':': return factory.create(TokenType.Colon, index, line, col); case ',': return factory.create(TokenType.Comma, index, line, col); case '.': if (next == '.') { in_stream.read(); int p = in_stream.peek(); if (p != -1 && p == '.') { in_stream.read(); return factory.create(TokenType.TripleDot, index, line, col, 3); } return factory.create(TokenType.DoubleDot, index, line, col, 2); } return factory.create(TokenType.Dot, index, line, col); case ')': return factory.create(TokenType.CloseParenthesis, index, line, col); case '(': return factory.create(TokenType.OpenParenthesis, index, line, col); case ']': return factory.create(TokenType.CloseSquareBracket, index, line, col); case '[': return factory.create(TokenType.OpenSquareBracket, index, line, col); case '}': return factory.create(TokenType.CloseCurlyBrace, index, line, col); case '{': return factory.create(TokenType.OpenCurlyBrace, index, line, col); case '#': return factory.create(TokenType.Hash, index, line, col); default: return null; } } LexOperator(TokenFactory factory, LexerStream in_stream); Token read(); }
LexOperator { public Token read() throws IOException, LexerException { int index = in_stream.getPosition(); int line = in_stream.getLine(); int col = in_stream.getCol(); int next = in_stream.peek(); if (next == -1) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Unexpected end of input stream when parsing operator"); } if (next == '.') { int tmp = in_stream.peek(2); if (tmp > 0 && Character.isDigit(tmp)) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Literal float encountered when operator '.' expected"); } } char cur = (char)in_stream.read(); next = in_stream.peek(); switch (cur) { case '+': switch (next) { case '+': in_stream.read(); return factory.create(TokenType.Increment, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.PlusAssign, index, line, col, 2); } return factory.create(TokenType.Plus, index, line, col); case '-': switch (next) { case '-': in_stream.read(); return factory.create(TokenType.Decrement, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.MinusAssign, index, line, col, 2); } return factory.create(TokenType.Minus, index, line, col); case '*': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.TimesAssign, index, line, col, 2); default: break; } return factory.create(TokenType.Times, index, line, col); case '/': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.DivAssign, index, line, col, 2); } return factory.create(TokenType.Div, index, line, col); case '%': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.ModAssign, index, line, col, 2); } return factory.create(TokenType.Mod, index, line, col); case '&': switch (next) { case '&': in_stream.read(); return factory.create(TokenType.LogicalAnd, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.BitwiseAndAssign, index, line, col, 2); } return factory.create(TokenType.BitwiseAnd, index, line, col); case '|': switch (next) { case '|': in_stream.read(); return factory.create(TokenType.LogicalOr, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.BitwiseOrAssign, index, line, col, 2); } return factory.create(TokenType.BitwiseOr, index, line, col); case '^': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.XorAssign, index, line, col, 2); case '^': in_stream.read(); if (in_stream.peek() == '=') { in_stream.read(); return factory.create(TokenType.PowAssign, index, line, col, 3); } return factory.create(TokenType.Pow, index, line, col, 2); } return factory.create(TokenType.Xor, index, line, col); case '!': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.NotEqual, index, line, col, 2); case '<': in_stream.read(); switch (in_stream.peek()) { case '=': in_stream.read(); return factory.create(TokenType.UnorderedOrGreater, index, line, col, 3); case '>': in_stream.read(); switch (in_stream.peek()) { case '=': in_stream.read(); return factory.create(TokenType.Unordered, index, line, col, 4); } return factory.create(TokenType.UnorderedOrEqual, index, line, col, 3); } return factory.create(TokenType.UnorderedGreaterOrEqual, index, line, col, 2); case '>': in_stream.read(); switch (in_stream.peek()) { case '=': in_stream.read(); return factory.create(TokenType.UnorderedOrLess, index, line, col, 3); default: break; } return factory.create(TokenType.UnorderedLessOrEqual, index, line, col, 2); } return factory.create(TokenType.Not, index, line, col); case '~': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.TildeAssign, index, line, col, 2); } return factory.create(TokenType.Tilde, index, line, col); case '=': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.Equal, index, line, col, 2); case '>': in_stream.read(); return factory.create(TokenType.GoesTo, index, line, col, 2); } return factory.create(TokenType.Assign, index, line, col); case '<': switch (next) { case '<': in_stream.read(); switch (in_stream.peek()) { case '=': in_stream.read(); return factory.create(TokenType.ShiftLeftAssign, index, line, col, 3); default: break; } return factory.create(TokenType.ShiftLeft, index, line, col, 2); case '>': in_stream.read(); switch (in_stream.peek()) { case '=': in_stream.read(); return factory.create(TokenType.LessEqualOrGreater, index, line, col, 3); default: break; } return factory.create(TokenType.LessOrGreater, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.LessEqual, index, line, col, 2); } return factory.create(TokenType.LessThan, index, line, col); case '>': switch (next) { case '>': in_stream.read(); int p = in_stream.peek(); if (p != -1) { switch ((char)p) { case '=': in_stream.read(); return factory.create(TokenType.ShiftRightAssign, index, line, col, 3); case '>': in_stream.read(); int q = in_stream.peek(); if (q != -1 && q == '=') { in_stream.read(); return factory.create(TokenType.TripleRightShiftAssign, index, line, col, 4); } return factory.create(TokenType.ShiftRightUnsigned, index, line, col, 3); } } return factory.create(TokenType.ShiftRight, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.GreaterEqual, index, line, col, 2); } return factory.create(TokenType.GreaterThan, index, line, col); case '?': return factory.create(TokenType.Question, index, line, col); case '$': return factory.create(TokenType.Dollar, index, line, col); case ';': return factory.create(TokenType.Semicolon, index, line, col); case ':': return factory.create(TokenType.Colon, index, line, col); case ',': return factory.create(TokenType.Comma, index, line, col); case '.': if (next == '.') { in_stream.read(); int p = in_stream.peek(); if (p != -1 && p == '.') { in_stream.read(); return factory.create(TokenType.TripleDot, index, line, col, 3); } return factory.create(TokenType.DoubleDot, index, line, col, 2); } return factory.create(TokenType.Dot, index, line, col); case ')': return factory.create(TokenType.CloseParenthesis, index, line, col); case '(': return factory.create(TokenType.OpenParenthesis, index, line, col); case ']': return factory.create(TokenType.CloseSquareBracket, index, line, col); case '[': return factory.create(TokenType.OpenSquareBracket, index, line, col); case '}': return factory.create(TokenType.CloseCurlyBrace, index, line, col); case '{': return factory.create(TokenType.OpenCurlyBrace, index, line, col); case '#': return factory.create(TokenType.Hash, index, line, col); default: return null; } } LexOperator(TokenFactory factory, LexerStream in_stream); Token read(); }
@Test public void paren() throws Exception { String str = "(1)[2]{3}"; LexerStream ls = new LexerStream(new StringReader(str)); LexOperator lex = new LexOperator(new BaseTokenFactory(), ls); Token tok = lex.read(); assertEquals(TokenType.OpenParenthesis, tok.getType()); assertTrue(tok.getLine() == 1 && tok.getEndLine() == 1); assertEquals(1, tok.getCol()); assertEquals(2, tok.getEndCol()); ls.read(); tok = lex.read(); assertEquals(TokenType.CloseParenthesis, tok.getType()); assertTrue(tok.getLine() == 1 && tok.getEndLine() == 1); assertEquals(3, tok.getCol()); assertEquals(4, tok.getEndCol()); tok = lex.read(); assertEquals(TokenType.OpenSquareBracket, tok.getType()); assertTrue(tok.getLine() == 1 && tok.getEndLine() == 1); assertEquals(4, tok.getCol()); assertEquals(5, tok.getEndCol()); ls.read(); tok = lex.read(); assertEquals(TokenType.CloseSquareBracket, tok.getType()); assertTrue(tok.getLine() == 1 && tok.getEndLine() == 1); assertEquals(6, tok.getCol()); assertEquals(7, tok.getEndCol()); tok = lex.read(); assertEquals(TokenType.OpenCurlyBrace, tok.getType()); assertTrue(tok.getLine() == 1 && tok.getEndLine() == 1); assertEquals(7, tok.getCol()); assertEquals(8, tok.getEndCol()); ls.read(); tok = lex.read(); assertEquals(TokenType.CloseCurlyBrace, tok.getType()); assertTrue(tok.getLine() == 1 && tok.getEndLine() == 1); assertEquals(9, tok.getCol()); assertEquals(10, tok.getEndCol()); }
public Token read() throws IOException, LexerException { int index = in_stream.getPosition(); int line = in_stream.getLine(); int col = in_stream.getCol(); int next = in_stream.peek(); if (next == -1) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Unexpected end of input stream when parsing operator"); } if (next == '.') { int tmp = in_stream.peek(2); if (tmp > 0 && Character.isDigit(tmp)) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Literal float encountered when operator '.' expected"); } } char cur = (char)in_stream.read(); next = in_stream.peek(); switch (cur) { case '+': switch (next) { case '+': in_stream.read(); return factory.create(TokenType.Increment, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.PlusAssign, index, line, col, 2); } return factory.create(TokenType.Plus, index, line, col); case '-': switch (next) { case '-': in_stream.read(); return factory.create(TokenType.Decrement, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.MinusAssign, index, line, col, 2); } return factory.create(TokenType.Minus, index, line, col); case '*': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.TimesAssign, index, line, col, 2); default: break; } return factory.create(TokenType.Times, index, line, col); case '/': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.DivAssign, index, line, col, 2); } return factory.create(TokenType.Div, index, line, col); case '%': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.ModAssign, index, line, col, 2); } return factory.create(TokenType.Mod, index, line, col); case '&': switch (next) { case '&': in_stream.read(); return factory.create(TokenType.LogicalAnd, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.BitwiseAndAssign, index, line, col, 2); } return factory.create(TokenType.BitwiseAnd, index, line, col); case '|': switch (next) { case '|': in_stream.read(); return factory.create(TokenType.LogicalOr, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.BitwiseOrAssign, index, line, col, 2); } return factory.create(TokenType.BitwiseOr, index, line, col); case '^': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.XorAssign, index, line, col, 2); case '^': in_stream.read(); if (in_stream.peek() == '=') { in_stream.read(); return factory.create(TokenType.PowAssign, index, line, col, 3); } return factory.create(TokenType.Pow, index, line, col, 2); } return factory.create(TokenType.Xor, index, line, col); case '!': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.NotEqual, index, line, col, 2); case '<': in_stream.read(); switch (in_stream.peek()) { case '=': in_stream.read(); return factory.create(TokenType.UnorderedOrGreater, index, line, col, 3); case '>': in_stream.read(); switch (in_stream.peek()) { case '=': in_stream.read(); return factory.create(TokenType.Unordered, index, line, col, 4); } return factory.create(TokenType.UnorderedOrEqual, index, line, col, 3); } return factory.create(TokenType.UnorderedGreaterOrEqual, index, line, col, 2); case '>': in_stream.read(); switch (in_stream.peek()) { case '=': in_stream.read(); return factory.create(TokenType.UnorderedOrLess, index, line, col, 3); default: break; } return factory.create(TokenType.UnorderedLessOrEqual, index, line, col, 2); } return factory.create(TokenType.Not, index, line, col); case '~': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.TildeAssign, index, line, col, 2); } return factory.create(TokenType.Tilde, index, line, col); case '=': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.Equal, index, line, col, 2); case '>': in_stream.read(); return factory.create(TokenType.GoesTo, index, line, col, 2); } return factory.create(TokenType.Assign, index, line, col); case '<': switch (next) { case '<': in_stream.read(); switch (in_stream.peek()) { case '=': in_stream.read(); return factory.create(TokenType.ShiftLeftAssign, index, line, col, 3); default: break; } return factory.create(TokenType.ShiftLeft, index, line, col, 2); case '>': in_stream.read(); switch (in_stream.peek()) { case '=': in_stream.read(); return factory.create(TokenType.LessEqualOrGreater, index, line, col, 3); default: break; } return factory.create(TokenType.LessOrGreater, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.LessEqual, index, line, col, 2); } return factory.create(TokenType.LessThan, index, line, col); case '>': switch (next) { case '>': in_stream.read(); int p = in_stream.peek(); if (p != -1) { switch ((char)p) { case '=': in_stream.read(); return factory.create(TokenType.ShiftRightAssign, index, line, col, 3); case '>': in_stream.read(); int q = in_stream.peek(); if (q != -1 && q == '=') { in_stream.read(); return factory.create(TokenType.TripleRightShiftAssign, index, line, col, 4); } return factory.create(TokenType.ShiftRightUnsigned, index, line, col, 3); } } return factory.create(TokenType.ShiftRight, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.GreaterEqual, index, line, col, 2); } return factory.create(TokenType.GreaterThan, index, line, col); case '?': return factory.create(TokenType.Question, index, line, col); case '$': return factory.create(TokenType.Dollar, index, line, col); case ';': return factory.create(TokenType.Semicolon, index, line, col); case ':': return factory.create(TokenType.Colon, index, line, col); case ',': return factory.create(TokenType.Comma, index, line, col); case '.': if (next == '.') { in_stream.read(); int p = in_stream.peek(); if (p != -1 && p == '.') { in_stream.read(); return factory.create(TokenType.TripleDot, index, line, col, 3); } return factory.create(TokenType.DoubleDot, index, line, col, 2); } return factory.create(TokenType.Dot, index, line, col); case ')': return factory.create(TokenType.CloseParenthesis, index, line, col); case '(': return factory.create(TokenType.OpenParenthesis, index, line, col); case ']': return factory.create(TokenType.CloseSquareBracket, index, line, col); case '[': return factory.create(TokenType.OpenSquareBracket, index, line, col); case '}': return factory.create(TokenType.CloseCurlyBrace, index, line, col); case '{': return factory.create(TokenType.OpenCurlyBrace, index, line, col); case '#': return factory.create(TokenType.Hash, index, line, col); default: return null; } }
LexOperator { public Token read() throws IOException, LexerException { int index = in_stream.getPosition(); int line = in_stream.getLine(); int col = in_stream.getCol(); int next = in_stream.peek(); if (next == -1) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Unexpected end of input stream when parsing operator"); } if (next == '.') { int tmp = in_stream.peek(2); if (tmp > 0 && Character.isDigit(tmp)) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Literal float encountered when operator '.' expected"); } } char cur = (char)in_stream.read(); next = in_stream.peek(); switch (cur) { case '+': switch (next) { case '+': in_stream.read(); return factory.create(TokenType.Increment, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.PlusAssign, index, line, col, 2); } return factory.create(TokenType.Plus, index, line, col); case '-': switch (next) { case '-': in_stream.read(); return factory.create(TokenType.Decrement, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.MinusAssign, index, line, col, 2); } return factory.create(TokenType.Minus, index, line, col); case '*': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.TimesAssign, index, line, col, 2); default: break; } return factory.create(TokenType.Times, index, line, col); case '/': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.DivAssign, index, line, col, 2); } return factory.create(TokenType.Div, index, line, col); case '%': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.ModAssign, index, line, col, 2); } return factory.create(TokenType.Mod, index, line, col); case '&': switch (next) { case '&': in_stream.read(); return factory.create(TokenType.LogicalAnd, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.BitwiseAndAssign, index, line, col, 2); } return factory.create(TokenType.BitwiseAnd, index, line, col); case '|': switch (next) { case '|': in_stream.read(); return factory.create(TokenType.LogicalOr, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.BitwiseOrAssign, index, line, col, 2); } return factory.create(TokenType.BitwiseOr, index, line, col); case '^': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.XorAssign, index, line, col, 2); case '^': in_stream.read(); if (in_stream.peek() == '=') { in_stream.read(); return factory.create(TokenType.PowAssign, index, line, col, 3); } return factory.create(TokenType.Pow, index, line, col, 2); } return factory.create(TokenType.Xor, index, line, col); case '!': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.NotEqual, index, line, col, 2); case '<': in_stream.read(); switch (in_stream.peek()) { case '=': in_stream.read(); return factory.create(TokenType.UnorderedOrGreater, index, line, col, 3); case '>': in_stream.read(); switch (in_stream.peek()) { case '=': in_stream.read(); return factory.create(TokenType.Unordered, index, line, col, 4); } return factory.create(TokenType.UnorderedOrEqual, index, line, col, 3); } return factory.create(TokenType.UnorderedGreaterOrEqual, index, line, col, 2); case '>': in_stream.read(); switch (in_stream.peek()) { case '=': in_stream.read(); return factory.create(TokenType.UnorderedOrLess, index, line, col, 3); default: break; } return factory.create(TokenType.UnorderedLessOrEqual, index, line, col, 2); } return factory.create(TokenType.Not, index, line, col); case '~': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.TildeAssign, index, line, col, 2); } return factory.create(TokenType.Tilde, index, line, col); case '=': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.Equal, index, line, col, 2); case '>': in_stream.read(); return factory.create(TokenType.GoesTo, index, line, col, 2); } return factory.create(TokenType.Assign, index, line, col); case '<': switch (next) { case '<': in_stream.read(); switch (in_stream.peek()) { case '=': in_stream.read(); return factory.create(TokenType.ShiftLeftAssign, index, line, col, 3); default: break; } return factory.create(TokenType.ShiftLeft, index, line, col, 2); case '>': in_stream.read(); switch (in_stream.peek()) { case '=': in_stream.read(); return factory.create(TokenType.LessEqualOrGreater, index, line, col, 3); default: break; } return factory.create(TokenType.LessOrGreater, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.LessEqual, index, line, col, 2); } return factory.create(TokenType.LessThan, index, line, col); case '>': switch (next) { case '>': in_stream.read(); int p = in_stream.peek(); if (p != -1) { switch ((char)p) { case '=': in_stream.read(); return factory.create(TokenType.ShiftRightAssign, index, line, col, 3); case '>': in_stream.read(); int q = in_stream.peek(); if (q != -1 && q == '=') { in_stream.read(); return factory.create(TokenType.TripleRightShiftAssign, index, line, col, 4); } return factory.create(TokenType.ShiftRightUnsigned, index, line, col, 3); } } return factory.create(TokenType.ShiftRight, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.GreaterEqual, index, line, col, 2); } return factory.create(TokenType.GreaterThan, index, line, col); case '?': return factory.create(TokenType.Question, index, line, col); case '$': return factory.create(TokenType.Dollar, index, line, col); case ';': return factory.create(TokenType.Semicolon, index, line, col); case ':': return factory.create(TokenType.Colon, index, line, col); case ',': return factory.create(TokenType.Comma, index, line, col); case '.': if (next == '.') { in_stream.read(); int p = in_stream.peek(); if (p != -1 && p == '.') { in_stream.read(); return factory.create(TokenType.TripleDot, index, line, col, 3); } return factory.create(TokenType.DoubleDot, index, line, col, 2); } return factory.create(TokenType.Dot, index, line, col); case ')': return factory.create(TokenType.CloseParenthesis, index, line, col); case '(': return factory.create(TokenType.OpenParenthesis, index, line, col); case ']': return factory.create(TokenType.CloseSquareBracket, index, line, col); case '[': return factory.create(TokenType.OpenSquareBracket, index, line, col); case '}': return factory.create(TokenType.CloseCurlyBrace, index, line, col); case '{': return factory.create(TokenType.OpenCurlyBrace, index, line, col); case '#': return factory.create(TokenType.Hash, index, line, col); default: return null; } } }
LexOperator { public Token read() throws IOException, LexerException { int index = in_stream.getPosition(); int line = in_stream.getLine(); int col = in_stream.getCol(); int next = in_stream.peek(); if (next == -1) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Unexpected end of input stream when parsing operator"); } if (next == '.') { int tmp = in_stream.peek(2); if (tmp > 0 && Character.isDigit(tmp)) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Literal float encountered when operator '.' expected"); } } char cur = (char)in_stream.read(); next = in_stream.peek(); switch (cur) { case '+': switch (next) { case '+': in_stream.read(); return factory.create(TokenType.Increment, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.PlusAssign, index, line, col, 2); } return factory.create(TokenType.Plus, index, line, col); case '-': switch (next) { case '-': in_stream.read(); return factory.create(TokenType.Decrement, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.MinusAssign, index, line, col, 2); } return factory.create(TokenType.Minus, index, line, col); case '*': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.TimesAssign, index, line, col, 2); default: break; } return factory.create(TokenType.Times, index, line, col); case '/': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.DivAssign, index, line, col, 2); } return factory.create(TokenType.Div, index, line, col); case '%': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.ModAssign, index, line, col, 2); } return factory.create(TokenType.Mod, index, line, col); case '&': switch (next) { case '&': in_stream.read(); return factory.create(TokenType.LogicalAnd, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.BitwiseAndAssign, index, line, col, 2); } return factory.create(TokenType.BitwiseAnd, index, line, col); case '|': switch (next) { case '|': in_stream.read(); return factory.create(TokenType.LogicalOr, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.BitwiseOrAssign, index, line, col, 2); } return factory.create(TokenType.BitwiseOr, index, line, col); case '^': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.XorAssign, index, line, col, 2); case '^': in_stream.read(); if (in_stream.peek() == '=') { in_stream.read(); return factory.create(TokenType.PowAssign, index, line, col, 3); } return factory.create(TokenType.Pow, index, line, col, 2); } return factory.create(TokenType.Xor, index, line, col); case '!': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.NotEqual, index, line, col, 2); case '<': in_stream.read(); switch (in_stream.peek()) { case '=': in_stream.read(); return factory.create(TokenType.UnorderedOrGreater, index, line, col, 3); case '>': in_stream.read(); switch (in_stream.peek()) { case '=': in_stream.read(); return factory.create(TokenType.Unordered, index, line, col, 4); } return factory.create(TokenType.UnorderedOrEqual, index, line, col, 3); } return factory.create(TokenType.UnorderedGreaterOrEqual, index, line, col, 2); case '>': in_stream.read(); switch (in_stream.peek()) { case '=': in_stream.read(); return factory.create(TokenType.UnorderedOrLess, index, line, col, 3); default: break; } return factory.create(TokenType.UnorderedLessOrEqual, index, line, col, 2); } return factory.create(TokenType.Not, index, line, col); case '~': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.TildeAssign, index, line, col, 2); } return factory.create(TokenType.Tilde, index, line, col); case '=': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.Equal, index, line, col, 2); case '>': in_stream.read(); return factory.create(TokenType.GoesTo, index, line, col, 2); } return factory.create(TokenType.Assign, index, line, col); case '<': switch (next) { case '<': in_stream.read(); switch (in_stream.peek()) { case '=': in_stream.read(); return factory.create(TokenType.ShiftLeftAssign, index, line, col, 3); default: break; } return factory.create(TokenType.ShiftLeft, index, line, col, 2); case '>': in_stream.read(); switch (in_stream.peek()) { case '=': in_stream.read(); return factory.create(TokenType.LessEqualOrGreater, index, line, col, 3); default: break; } return factory.create(TokenType.LessOrGreater, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.LessEqual, index, line, col, 2); } return factory.create(TokenType.LessThan, index, line, col); case '>': switch (next) { case '>': in_stream.read(); int p = in_stream.peek(); if (p != -1) { switch ((char)p) { case '=': in_stream.read(); return factory.create(TokenType.ShiftRightAssign, index, line, col, 3); case '>': in_stream.read(); int q = in_stream.peek(); if (q != -1 && q == '=') { in_stream.read(); return factory.create(TokenType.TripleRightShiftAssign, index, line, col, 4); } return factory.create(TokenType.ShiftRightUnsigned, index, line, col, 3); } } return factory.create(TokenType.ShiftRight, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.GreaterEqual, index, line, col, 2); } return factory.create(TokenType.GreaterThan, index, line, col); case '?': return factory.create(TokenType.Question, index, line, col); case '$': return factory.create(TokenType.Dollar, index, line, col); case ';': return factory.create(TokenType.Semicolon, index, line, col); case ':': return factory.create(TokenType.Colon, index, line, col); case ',': return factory.create(TokenType.Comma, index, line, col); case '.': if (next == '.') { in_stream.read(); int p = in_stream.peek(); if (p != -1 && p == '.') { in_stream.read(); return factory.create(TokenType.TripleDot, index, line, col, 3); } return factory.create(TokenType.DoubleDot, index, line, col, 2); } return factory.create(TokenType.Dot, index, line, col); case ')': return factory.create(TokenType.CloseParenthesis, index, line, col); case '(': return factory.create(TokenType.OpenParenthesis, index, line, col); case ']': return factory.create(TokenType.CloseSquareBracket, index, line, col); case '[': return factory.create(TokenType.OpenSquareBracket, index, line, col); case '}': return factory.create(TokenType.CloseCurlyBrace, index, line, col); case '{': return factory.create(TokenType.OpenCurlyBrace, index, line, col); case '#': return factory.create(TokenType.Hash, index, line, col); default: return null; } } LexOperator(TokenFactory factory, LexerStream in_stream); }
LexOperator { public Token read() throws IOException, LexerException { int index = in_stream.getPosition(); int line = in_stream.getLine(); int col = in_stream.getCol(); int next = in_stream.peek(); if (next == -1) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Unexpected end of input stream when parsing operator"); } if (next == '.') { int tmp = in_stream.peek(2); if (tmp > 0 && Character.isDigit(tmp)) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Literal float encountered when operator '.' expected"); } } char cur = (char)in_stream.read(); next = in_stream.peek(); switch (cur) { case '+': switch (next) { case '+': in_stream.read(); return factory.create(TokenType.Increment, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.PlusAssign, index, line, col, 2); } return factory.create(TokenType.Plus, index, line, col); case '-': switch (next) { case '-': in_stream.read(); return factory.create(TokenType.Decrement, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.MinusAssign, index, line, col, 2); } return factory.create(TokenType.Minus, index, line, col); case '*': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.TimesAssign, index, line, col, 2); default: break; } return factory.create(TokenType.Times, index, line, col); case '/': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.DivAssign, index, line, col, 2); } return factory.create(TokenType.Div, index, line, col); case '%': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.ModAssign, index, line, col, 2); } return factory.create(TokenType.Mod, index, line, col); case '&': switch (next) { case '&': in_stream.read(); return factory.create(TokenType.LogicalAnd, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.BitwiseAndAssign, index, line, col, 2); } return factory.create(TokenType.BitwiseAnd, index, line, col); case '|': switch (next) { case '|': in_stream.read(); return factory.create(TokenType.LogicalOr, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.BitwiseOrAssign, index, line, col, 2); } return factory.create(TokenType.BitwiseOr, index, line, col); case '^': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.XorAssign, index, line, col, 2); case '^': in_stream.read(); if (in_stream.peek() == '=') { in_stream.read(); return factory.create(TokenType.PowAssign, index, line, col, 3); } return factory.create(TokenType.Pow, index, line, col, 2); } return factory.create(TokenType.Xor, index, line, col); case '!': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.NotEqual, index, line, col, 2); case '<': in_stream.read(); switch (in_stream.peek()) { case '=': in_stream.read(); return factory.create(TokenType.UnorderedOrGreater, index, line, col, 3); case '>': in_stream.read(); switch (in_stream.peek()) { case '=': in_stream.read(); return factory.create(TokenType.Unordered, index, line, col, 4); } return factory.create(TokenType.UnorderedOrEqual, index, line, col, 3); } return factory.create(TokenType.UnorderedGreaterOrEqual, index, line, col, 2); case '>': in_stream.read(); switch (in_stream.peek()) { case '=': in_stream.read(); return factory.create(TokenType.UnorderedOrLess, index, line, col, 3); default: break; } return factory.create(TokenType.UnorderedLessOrEqual, index, line, col, 2); } return factory.create(TokenType.Not, index, line, col); case '~': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.TildeAssign, index, line, col, 2); } return factory.create(TokenType.Tilde, index, line, col); case '=': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.Equal, index, line, col, 2); case '>': in_stream.read(); return factory.create(TokenType.GoesTo, index, line, col, 2); } return factory.create(TokenType.Assign, index, line, col); case '<': switch (next) { case '<': in_stream.read(); switch (in_stream.peek()) { case '=': in_stream.read(); return factory.create(TokenType.ShiftLeftAssign, index, line, col, 3); default: break; } return factory.create(TokenType.ShiftLeft, index, line, col, 2); case '>': in_stream.read(); switch (in_stream.peek()) { case '=': in_stream.read(); return factory.create(TokenType.LessEqualOrGreater, index, line, col, 3); default: break; } return factory.create(TokenType.LessOrGreater, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.LessEqual, index, line, col, 2); } return factory.create(TokenType.LessThan, index, line, col); case '>': switch (next) { case '>': in_stream.read(); int p = in_stream.peek(); if (p != -1) { switch ((char)p) { case '=': in_stream.read(); return factory.create(TokenType.ShiftRightAssign, index, line, col, 3); case '>': in_stream.read(); int q = in_stream.peek(); if (q != -1 && q == '=') { in_stream.read(); return factory.create(TokenType.TripleRightShiftAssign, index, line, col, 4); } return factory.create(TokenType.ShiftRightUnsigned, index, line, col, 3); } } return factory.create(TokenType.ShiftRight, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.GreaterEqual, index, line, col, 2); } return factory.create(TokenType.GreaterThan, index, line, col); case '?': return factory.create(TokenType.Question, index, line, col); case '$': return factory.create(TokenType.Dollar, index, line, col); case ';': return factory.create(TokenType.Semicolon, index, line, col); case ':': return factory.create(TokenType.Colon, index, line, col); case ',': return factory.create(TokenType.Comma, index, line, col); case '.': if (next == '.') { in_stream.read(); int p = in_stream.peek(); if (p != -1 && p == '.') { in_stream.read(); return factory.create(TokenType.TripleDot, index, line, col, 3); } return factory.create(TokenType.DoubleDot, index, line, col, 2); } return factory.create(TokenType.Dot, index, line, col); case ')': return factory.create(TokenType.CloseParenthesis, index, line, col); case '(': return factory.create(TokenType.OpenParenthesis, index, line, col); case ']': return factory.create(TokenType.CloseSquareBracket, index, line, col); case '[': return factory.create(TokenType.OpenSquareBracket, index, line, col); case '}': return factory.create(TokenType.CloseCurlyBrace, index, line, col); case '{': return factory.create(TokenType.OpenCurlyBrace, index, line, col); case '#': return factory.create(TokenType.Hash, index, line, col); default: return null; } } LexOperator(TokenFactory factory, LexerStream in_stream); Token read(); }
LexOperator { public Token read() throws IOException, LexerException { int index = in_stream.getPosition(); int line = in_stream.getLine(); int col = in_stream.getCol(); int next = in_stream.peek(); if (next == -1) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Unexpected end of input stream when parsing operator"); } if (next == '.') { int tmp = in_stream.peek(2); if (tmp > 0 && Character.isDigit(tmp)) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Literal float encountered when operator '.' expected"); } } char cur = (char)in_stream.read(); next = in_stream.peek(); switch (cur) { case '+': switch (next) { case '+': in_stream.read(); return factory.create(TokenType.Increment, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.PlusAssign, index, line, col, 2); } return factory.create(TokenType.Plus, index, line, col); case '-': switch (next) { case '-': in_stream.read(); return factory.create(TokenType.Decrement, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.MinusAssign, index, line, col, 2); } return factory.create(TokenType.Minus, index, line, col); case '*': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.TimesAssign, index, line, col, 2); default: break; } return factory.create(TokenType.Times, index, line, col); case '/': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.DivAssign, index, line, col, 2); } return factory.create(TokenType.Div, index, line, col); case '%': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.ModAssign, index, line, col, 2); } return factory.create(TokenType.Mod, index, line, col); case '&': switch (next) { case '&': in_stream.read(); return factory.create(TokenType.LogicalAnd, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.BitwiseAndAssign, index, line, col, 2); } return factory.create(TokenType.BitwiseAnd, index, line, col); case '|': switch (next) { case '|': in_stream.read(); return factory.create(TokenType.LogicalOr, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.BitwiseOrAssign, index, line, col, 2); } return factory.create(TokenType.BitwiseOr, index, line, col); case '^': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.XorAssign, index, line, col, 2); case '^': in_stream.read(); if (in_stream.peek() == '=') { in_stream.read(); return factory.create(TokenType.PowAssign, index, line, col, 3); } return factory.create(TokenType.Pow, index, line, col, 2); } return factory.create(TokenType.Xor, index, line, col); case '!': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.NotEqual, index, line, col, 2); case '<': in_stream.read(); switch (in_stream.peek()) { case '=': in_stream.read(); return factory.create(TokenType.UnorderedOrGreater, index, line, col, 3); case '>': in_stream.read(); switch (in_stream.peek()) { case '=': in_stream.read(); return factory.create(TokenType.Unordered, index, line, col, 4); } return factory.create(TokenType.UnorderedOrEqual, index, line, col, 3); } return factory.create(TokenType.UnorderedGreaterOrEqual, index, line, col, 2); case '>': in_stream.read(); switch (in_stream.peek()) { case '=': in_stream.read(); return factory.create(TokenType.UnorderedOrLess, index, line, col, 3); default: break; } return factory.create(TokenType.UnorderedLessOrEqual, index, line, col, 2); } return factory.create(TokenType.Not, index, line, col); case '~': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.TildeAssign, index, line, col, 2); } return factory.create(TokenType.Tilde, index, line, col); case '=': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.Equal, index, line, col, 2); case '>': in_stream.read(); return factory.create(TokenType.GoesTo, index, line, col, 2); } return factory.create(TokenType.Assign, index, line, col); case '<': switch (next) { case '<': in_stream.read(); switch (in_stream.peek()) { case '=': in_stream.read(); return factory.create(TokenType.ShiftLeftAssign, index, line, col, 3); default: break; } return factory.create(TokenType.ShiftLeft, index, line, col, 2); case '>': in_stream.read(); switch (in_stream.peek()) { case '=': in_stream.read(); return factory.create(TokenType.LessEqualOrGreater, index, line, col, 3); default: break; } return factory.create(TokenType.LessOrGreater, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.LessEqual, index, line, col, 2); } return factory.create(TokenType.LessThan, index, line, col); case '>': switch (next) { case '>': in_stream.read(); int p = in_stream.peek(); if (p != -1) { switch ((char)p) { case '=': in_stream.read(); return factory.create(TokenType.ShiftRightAssign, index, line, col, 3); case '>': in_stream.read(); int q = in_stream.peek(); if (q != -1 && q == '=') { in_stream.read(); return factory.create(TokenType.TripleRightShiftAssign, index, line, col, 4); } return factory.create(TokenType.ShiftRightUnsigned, index, line, col, 3); } } return factory.create(TokenType.ShiftRight, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.GreaterEqual, index, line, col, 2); } return factory.create(TokenType.GreaterThan, index, line, col); case '?': return factory.create(TokenType.Question, index, line, col); case '$': return factory.create(TokenType.Dollar, index, line, col); case ';': return factory.create(TokenType.Semicolon, index, line, col); case ':': return factory.create(TokenType.Colon, index, line, col); case ',': return factory.create(TokenType.Comma, index, line, col); case '.': if (next == '.') { in_stream.read(); int p = in_stream.peek(); if (p != -1 && p == '.') { in_stream.read(); return factory.create(TokenType.TripleDot, index, line, col, 3); } return factory.create(TokenType.DoubleDot, index, line, col, 2); } return factory.create(TokenType.Dot, index, line, col); case ')': return factory.create(TokenType.CloseParenthesis, index, line, col); case '(': return factory.create(TokenType.OpenParenthesis, index, line, col); case ']': return factory.create(TokenType.CloseSquareBracket, index, line, col); case '[': return factory.create(TokenType.OpenSquareBracket, index, line, col); case '}': return factory.create(TokenType.CloseCurlyBrace, index, line, col); case '{': return factory.create(TokenType.OpenCurlyBrace, index, line, col); case '#': return factory.create(TokenType.Hash, index, line, col); default: return null; } } LexOperator(TokenFactory factory, LexerStream in_stream); Token read(); }
@Test public void hash() throws Exception { String str = "##"; LexerStream ls = new LexerStream(new StringReader(str)); LexOperator lex = new LexOperator(new BaseTokenFactory(), ls); Token tok = lex.read(); assertEquals(TokenType.Hash, tok.getType()); assertTrue(tok.getLine() == 1 && tok.getEndLine() == 1); assertEquals(1, tok.getCol()); assertEquals(2, tok.getEndCol()); tok = lex.read(); assertEquals(TokenType.Hash, tok.getType()); assertTrue(tok.getLine() == 1 && tok.getEndLine() == 1); assertEquals(2, tok.getCol()); assertEquals(3, tok.getEndCol()); }
public Token read() throws IOException, LexerException { int index = in_stream.getPosition(); int line = in_stream.getLine(); int col = in_stream.getCol(); int next = in_stream.peek(); if (next == -1) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Unexpected end of input stream when parsing operator"); } if (next == '.') { int tmp = in_stream.peek(2); if (tmp > 0 && Character.isDigit(tmp)) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Literal float encountered when operator '.' expected"); } } char cur = (char)in_stream.read(); next = in_stream.peek(); switch (cur) { case '+': switch (next) { case '+': in_stream.read(); return factory.create(TokenType.Increment, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.PlusAssign, index, line, col, 2); } return factory.create(TokenType.Plus, index, line, col); case '-': switch (next) { case '-': in_stream.read(); return factory.create(TokenType.Decrement, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.MinusAssign, index, line, col, 2); } return factory.create(TokenType.Minus, index, line, col); case '*': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.TimesAssign, index, line, col, 2); default: break; } return factory.create(TokenType.Times, index, line, col); case '/': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.DivAssign, index, line, col, 2); } return factory.create(TokenType.Div, index, line, col); case '%': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.ModAssign, index, line, col, 2); } return factory.create(TokenType.Mod, index, line, col); case '&': switch (next) { case '&': in_stream.read(); return factory.create(TokenType.LogicalAnd, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.BitwiseAndAssign, index, line, col, 2); } return factory.create(TokenType.BitwiseAnd, index, line, col); case '|': switch (next) { case '|': in_stream.read(); return factory.create(TokenType.LogicalOr, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.BitwiseOrAssign, index, line, col, 2); } return factory.create(TokenType.BitwiseOr, index, line, col); case '^': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.XorAssign, index, line, col, 2); case '^': in_stream.read(); if (in_stream.peek() == '=') { in_stream.read(); return factory.create(TokenType.PowAssign, index, line, col, 3); } return factory.create(TokenType.Pow, index, line, col, 2); } return factory.create(TokenType.Xor, index, line, col); case '!': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.NotEqual, index, line, col, 2); case '<': in_stream.read(); switch (in_stream.peek()) { case '=': in_stream.read(); return factory.create(TokenType.UnorderedOrGreater, index, line, col, 3); case '>': in_stream.read(); switch (in_stream.peek()) { case '=': in_stream.read(); return factory.create(TokenType.Unordered, index, line, col, 4); } return factory.create(TokenType.UnorderedOrEqual, index, line, col, 3); } return factory.create(TokenType.UnorderedGreaterOrEqual, index, line, col, 2); case '>': in_stream.read(); switch (in_stream.peek()) { case '=': in_stream.read(); return factory.create(TokenType.UnorderedOrLess, index, line, col, 3); default: break; } return factory.create(TokenType.UnorderedLessOrEqual, index, line, col, 2); } return factory.create(TokenType.Not, index, line, col); case '~': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.TildeAssign, index, line, col, 2); } return factory.create(TokenType.Tilde, index, line, col); case '=': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.Equal, index, line, col, 2); case '>': in_stream.read(); return factory.create(TokenType.GoesTo, index, line, col, 2); } return factory.create(TokenType.Assign, index, line, col); case '<': switch (next) { case '<': in_stream.read(); switch (in_stream.peek()) { case '=': in_stream.read(); return factory.create(TokenType.ShiftLeftAssign, index, line, col, 3); default: break; } return factory.create(TokenType.ShiftLeft, index, line, col, 2); case '>': in_stream.read(); switch (in_stream.peek()) { case '=': in_stream.read(); return factory.create(TokenType.LessEqualOrGreater, index, line, col, 3); default: break; } return factory.create(TokenType.LessOrGreater, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.LessEqual, index, line, col, 2); } return factory.create(TokenType.LessThan, index, line, col); case '>': switch (next) { case '>': in_stream.read(); int p = in_stream.peek(); if (p != -1) { switch ((char)p) { case '=': in_stream.read(); return factory.create(TokenType.ShiftRightAssign, index, line, col, 3); case '>': in_stream.read(); int q = in_stream.peek(); if (q != -1 && q == '=') { in_stream.read(); return factory.create(TokenType.TripleRightShiftAssign, index, line, col, 4); } return factory.create(TokenType.ShiftRightUnsigned, index, line, col, 3); } } return factory.create(TokenType.ShiftRight, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.GreaterEqual, index, line, col, 2); } return factory.create(TokenType.GreaterThan, index, line, col); case '?': return factory.create(TokenType.Question, index, line, col); case '$': return factory.create(TokenType.Dollar, index, line, col); case ';': return factory.create(TokenType.Semicolon, index, line, col); case ':': return factory.create(TokenType.Colon, index, line, col); case ',': return factory.create(TokenType.Comma, index, line, col); case '.': if (next == '.') { in_stream.read(); int p = in_stream.peek(); if (p != -1 && p == '.') { in_stream.read(); return factory.create(TokenType.TripleDot, index, line, col, 3); } return factory.create(TokenType.DoubleDot, index, line, col, 2); } return factory.create(TokenType.Dot, index, line, col); case ')': return factory.create(TokenType.CloseParenthesis, index, line, col); case '(': return factory.create(TokenType.OpenParenthesis, index, line, col); case ']': return factory.create(TokenType.CloseSquareBracket, index, line, col); case '[': return factory.create(TokenType.OpenSquareBracket, index, line, col); case '}': return factory.create(TokenType.CloseCurlyBrace, index, line, col); case '{': return factory.create(TokenType.OpenCurlyBrace, index, line, col); case '#': return factory.create(TokenType.Hash, index, line, col); default: return null; } }
LexOperator { public Token read() throws IOException, LexerException { int index = in_stream.getPosition(); int line = in_stream.getLine(); int col = in_stream.getCol(); int next = in_stream.peek(); if (next == -1) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Unexpected end of input stream when parsing operator"); } if (next == '.') { int tmp = in_stream.peek(2); if (tmp > 0 && Character.isDigit(tmp)) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Literal float encountered when operator '.' expected"); } } char cur = (char)in_stream.read(); next = in_stream.peek(); switch (cur) { case '+': switch (next) { case '+': in_stream.read(); return factory.create(TokenType.Increment, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.PlusAssign, index, line, col, 2); } return factory.create(TokenType.Plus, index, line, col); case '-': switch (next) { case '-': in_stream.read(); return factory.create(TokenType.Decrement, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.MinusAssign, index, line, col, 2); } return factory.create(TokenType.Minus, index, line, col); case '*': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.TimesAssign, index, line, col, 2); default: break; } return factory.create(TokenType.Times, index, line, col); case '/': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.DivAssign, index, line, col, 2); } return factory.create(TokenType.Div, index, line, col); case '%': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.ModAssign, index, line, col, 2); } return factory.create(TokenType.Mod, index, line, col); case '&': switch (next) { case '&': in_stream.read(); return factory.create(TokenType.LogicalAnd, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.BitwiseAndAssign, index, line, col, 2); } return factory.create(TokenType.BitwiseAnd, index, line, col); case '|': switch (next) { case '|': in_stream.read(); return factory.create(TokenType.LogicalOr, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.BitwiseOrAssign, index, line, col, 2); } return factory.create(TokenType.BitwiseOr, index, line, col); case '^': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.XorAssign, index, line, col, 2); case '^': in_stream.read(); if (in_stream.peek() == '=') { in_stream.read(); return factory.create(TokenType.PowAssign, index, line, col, 3); } return factory.create(TokenType.Pow, index, line, col, 2); } return factory.create(TokenType.Xor, index, line, col); case '!': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.NotEqual, index, line, col, 2); case '<': in_stream.read(); switch (in_stream.peek()) { case '=': in_stream.read(); return factory.create(TokenType.UnorderedOrGreater, index, line, col, 3); case '>': in_stream.read(); switch (in_stream.peek()) { case '=': in_stream.read(); return factory.create(TokenType.Unordered, index, line, col, 4); } return factory.create(TokenType.UnorderedOrEqual, index, line, col, 3); } return factory.create(TokenType.UnorderedGreaterOrEqual, index, line, col, 2); case '>': in_stream.read(); switch (in_stream.peek()) { case '=': in_stream.read(); return factory.create(TokenType.UnorderedOrLess, index, line, col, 3); default: break; } return factory.create(TokenType.UnorderedLessOrEqual, index, line, col, 2); } return factory.create(TokenType.Not, index, line, col); case '~': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.TildeAssign, index, line, col, 2); } return factory.create(TokenType.Tilde, index, line, col); case '=': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.Equal, index, line, col, 2); case '>': in_stream.read(); return factory.create(TokenType.GoesTo, index, line, col, 2); } return factory.create(TokenType.Assign, index, line, col); case '<': switch (next) { case '<': in_stream.read(); switch (in_stream.peek()) { case '=': in_stream.read(); return factory.create(TokenType.ShiftLeftAssign, index, line, col, 3); default: break; } return factory.create(TokenType.ShiftLeft, index, line, col, 2); case '>': in_stream.read(); switch (in_stream.peek()) { case '=': in_stream.read(); return factory.create(TokenType.LessEqualOrGreater, index, line, col, 3); default: break; } return factory.create(TokenType.LessOrGreater, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.LessEqual, index, line, col, 2); } return factory.create(TokenType.LessThan, index, line, col); case '>': switch (next) { case '>': in_stream.read(); int p = in_stream.peek(); if (p != -1) { switch ((char)p) { case '=': in_stream.read(); return factory.create(TokenType.ShiftRightAssign, index, line, col, 3); case '>': in_stream.read(); int q = in_stream.peek(); if (q != -1 && q == '=') { in_stream.read(); return factory.create(TokenType.TripleRightShiftAssign, index, line, col, 4); } return factory.create(TokenType.ShiftRightUnsigned, index, line, col, 3); } } return factory.create(TokenType.ShiftRight, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.GreaterEqual, index, line, col, 2); } return factory.create(TokenType.GreaterThan, index, line, col); case '?': return factory.create(TokenType.Question, index, line, col); case '$': return factory.create(TokenType.Dollar, index, line, col); case ';': return factory.create(TokenType.Semicolon, index, line, col); case ':': return factory.create(TokenType.Colon, index, line, col); case ',': return factory.create(TokenType.Comma, index, line, col); case '.': if (next == '.') { in_stream.read(); int p = in_stream.peek(); if (p != -1 && p == '.') { in_stream.read(); return factory.create(TokenType.TripleDot, index, line, col, 3); } return factory.create(TokenType.DoubleDot, index, line, col, 2); } return factory.create(TokenType.Dot, index, line, col); case ')': return factory.create(TokenType.CloseParenthesis, index, line, col); case '(': return factory.create(TokenType.OpenParenthesis, index, line, col); case ']': return factory.create(TokenType.CloseSquareBracket, index, line, col); case '[': return factory.create(TokenType.OpenSquareBracket, index, line, col); case '}': return factory.create(TokenType.CloseCurlyBrace, index, line, col); case '{': return factory.create(TokenType.OpenCurlyBrace, index, line, col); case '#': return factory.create(TokenType.Hash, index, line, col); default: return null; } } }
LexOperator { public Token read() throws IOException, LexerException { int index = in_stream.getPosition(); int line = in_stream.getLine(); int col = in_stream.getCol(); int next = in_stream.peek(); if (next == -1) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Unexpected end of input stream when parsing operator"); } if (next == '.') { int tmp = in_stream.peek(2); if (tmp > 0 && Character.isDigit(tmp)) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Literal float encountered when operator '.' expected"); } } char cur = (char)in_stream.read(); next = in_stream.peek(); switch (cur) { case '+': switch (next) { case '+': in_stream.read(); return factory.create(TokenType.Increment, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.PlusAssign, index, line, col, 2); } return factory.create(TokenType.Plus, index, line, col); case '-': switch (next) { case '-': in_stream.read(); return factory.create(TokenType.Decrement, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.MinusAssign, index, line, col, 2); } return factory.create(TokenType.Minus, index, line, col); case '*': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.TimesAssign, index, line, col, 2); default: break; } return factory.create(TokenType.Times, index, line, col); case '/': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.DivAssign, index, line, col, 2); } return factory.create(TokenType.Div, index, line, col); case '%': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.ModAssign, index, line, col, 2); } return factory.create(TokenType.Mod, index, line, col); case '&': switch (next) { case '&': in_stream.read(); return factory.create(TokenType.LogicalAnd, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.BitwiseAndAssign, index, line, col, 2); } return factory.create(TokenType.BitwiseAnd, index, line, col); case '|': switch (next) { case '|': in_stream.read(); return factory.create(TokenType.LogicalOr, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.BitwiseOrAssign, index, line, col, 2); } return factory.create(TokenType.BitwiseOr, index, line, col); case '^': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.XorAssign, index, line, col, 2); case '^': in_stream.read(); if (in_stream.peek() == '=') { in_stream.read(); return factory.create(TokenType.PowAssign, index, line, col, 3); } return factory.create(TokenType.Pow, index, line, col, 2); } return factory.create(TokenType.Xor, index, line, col); case '!': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.NotEqual, index, line, col, 2); case '<': in_stream.read(); switch (in_stream.peek()) { case '=': in_stream.read(); return factory.create(TokenType.UnorderedOrGreater, index, line, col, 3); case '>': in_stream.read(); switch (in_stream.peek()) { case '=': in_stream.read(); return factory.create(TokenType.Unordered, index, line, col, 4); } return factory.create(TokenType.UnorderedOrEqual, index, line, col, 3); } return factory.create(TokenType.UnorderedGreaterOrEqual, index, line, col, 2); case '>': in_stream.read(); switch (in_stream.peek()) { case '=': in_stream.read(); return factory.create(TokenType.UnorderedOrLess, index, line, col, 3); default: break; } return factory.create(TokenType.UnorderedLessOrEqual, index, line, col, 2); } return factory.create(TokenType.Not, index, line, col); case '~': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.TildeAssign, index, line, col, 2); } return factory.create(TokenType.Tilde, index, line, col); case '=': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.Equal, index, line, col, 2); case '>': in_stream.read(); return factory.create(TokenType.GoesTo, index, line, col, 2); } return factory.create(TokenType.Assign, index, line, col); case '<': switch (next) { case '<': in_stream.read(); switch (in_stream.peek()) { case '=': in_stream.read(); return factory.create(TokenType.ShiftLeftAssign, index, line, col, 3); default: break; } return factory.create(TokenType.ShiftLeft, index, line, col, 2); case '>': in_stream.read(); switch (in_stream.peek()) { case '=': in_stream.read(); return factory.create(TokenType.LessEqualOrGreater, index, line, col, 3); default: break; } return factory.create(TokenType.LessOrGreater, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.LessEqual, index, line, col, 2); } return factory.create(TokenType.LessThan, index, line, col); case '>': switch (next) { case '>': in_stream.read(); int p = in_stream.peek(); if (p != -1) { switch ((char)p) { case '=': in_stream.read(); return factory.create(TokenType.ShiftRightAssign, index, line, col, 3); case '>': in_stream.read(); int q = in_stream.peek(); if (q != -1 && q == '=') { in_stream.read(); return factory.create(TokenType.TripleRightShiftAssign, index, line, col, 4); } return factory.create(TokenType.ShiftRightUnsigned, index, line, col, 3); } } return factory.create(TokenType.ShiftRight, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.GreaterEqual, index, line, col, 2); } return factory.create(TokenType.GreaterThan, index, line, col); case '?': return factory.create(TokenType.Question, index, line, col); case '$': return factory.create(TokenType.Dollar, index, line, col); case ';': return factory.create(TokenType.Semicolon, index, line, col); case ':': return factory.create(TokenType.Colon, index, line, col); case ',': return factory.create(TokenType.Comma, index, line, col); case '.': if (next == '.') { in_stream.read(); int p = in_stream.peek(); if (p != -1 && p == '.') { in_stream.read(); return factory.create(TokenType.TripleDot, index, line, col, 3); } return factory.create(TokenType.DoubleDot, index, line, col, 2); } return factory.create(TokenType.Dot, index, line, col); case ')': return factory.create(TokenType.CloseParenthesis, index, line, col); case '(': return factory.create(TokenType.OpenParenthesis, index, line, col); case ']': return factory.create(TokenType.CloseSquareBracket, index, line, col); case '[': return factory.create(TokenType.OpenSquareBracket, index, line, col); case '}': return factory.create(TokenType.CloseCurlyBrace, index, line, col); case '{': return factory.create(TokenType.OpenCurlyBrace, index, line, col); case '#': return factory.create(TokenType.Hash, index, line, col); default: return null; } } LexOperator(TokenFactory factory, LexerStream in_stream); }
LexOperator { public Token read() throws IOException, LexerException { int index = in_stream.getPosition(); int line = in_stream.getLine(); int col = in_stream.getCol(); int next = in_stream.peek(); if (next == -1) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Unexpected end of input stream when parsing operator"); } if (next == '.') { int tmp = in_stream.peek(2); if (tmp > 0 && Character.isDigit(tmp)) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Literal float encountered when operator '.' expected"); } } char cur = (char)in_stream.read(); next = in_stream.peek(); switch (cur) { case '+': switch (next) { case '+': in_stream.read(); return factory.create(TokenType.Increment, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.PlusAssign, index, line, col, 2); } return factory.create(TokenType.Plus, index, line, col); case '-': switch (next) { case '-': in_stream.read(); return factory.create(TokenType.Decrement, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.MinusAssign, index, line, col, 2); } return factory.create(TokenType.Minus, index, line, col); case '*': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.TimesAssign, index, line, col, 2); default: break; } return factory.create(TokenType.Times, index, line, col); case '/': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.DivAssign, index, line, col, 2); } return factory.create(TokenType.Div, index, line, col); case '%': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.ModAssign, index, line, col, 2); } return factory.create(TokenType.Mod, index, line, col); case '&': switch (next) { case '&': in_stream.read(); return factory.create(TokenType.LogicalAnd, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.BitwiseAndAssign, index, line, col, 2); } return factory.create(TokenType.BitwiseAnd, index, line, col); case '|': switch (next) { case '|': in_stream.read(); return factory.create(TokenType.LogicalOr, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.BitwiseOrAssign, index, line, col, 2); } return factory.create(TokenType.BitwiseOr, index, line, col); case '^': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.XorAssign, index, line, col, 2); case '^': in_stream.read(); if (in_stream.peek() == '=') { in_stream.read(); return factory.create(TokenType.PowAssign, index, line, col, 3); } return factory.create(TokenType.Pow, index, line, col, 2); } return factory.create(TokenType.Xor, index, line, col); case '!': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.NotEqual, index, line, col, 2); case '<': in_stream.read(); switch (in_stream.peek()) { case '=': in_stream.read(); return factory.create(TokenType.UnorderedOrGreater, index, line, col, 3); case '>': in_stream.read(); switch (in_stream.peek()) { case '=': in_stream.read(); return factory.create(TokenType.Unordered, index, line, col, 4); } return factory.create(TokenType.UnorderedOrEqual, index, line, col, 3); } return factory.create(TokenType.UnorderedGreaterOrEqual, index, line, col, 2); case '>': in_stream.read(); switch (in_stream.peek()) { case '=': in_stream.read(); return factory.create(TokenType.UnorderedOrLess, index, line, col, 3); default: break; } return factory.create(TokenType.UnorderedLessOrEqual, index, line, col, 2); } return factory.create(TokenType.Not, index, line, col); case '~': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.TildeAssign, index, line, col, 2); } return factory.create(TokenType.Tilde, index, line, col); case '=': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.Equal, index, line, col, 2); case '>': in_stream.read(); return factory.create(TokenType.GoesTo, index, line, col, 2); } return factory.create(TokenType.Assign, index, line, col); case '<': switch (next) { case '<': in_stream.read(); switch (in_stream.peek()) { case '=': in_stream.read(); return factory.create(TokenType.ShiftLeftAssign, index, line, col, 3); default: break; } return factory.create(TokenType.ShiftLeft, index, line, col, 2); case '>': in_stream.read(); switch (in_stream.peek()) { case '=': in_stream.read(); return factory.create(TokenType.LessEqualOrGreater, index, line, col, 3); default: break; } return factory.create(TokenType.LessOrGreater, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.LessEqual, index, line, col, 2); } return factory.create(TokenType.LessThan, index, line, col); case '>': switch (next) { case '>': in_stream.read(); int p = in_stream.peek(); if (p != -1) { switch ((char)p) { case '=': in_stream.read(); return factory.create(TokenType.ShiftRightAssign, index, line, col, 3); case '>': in_stream.read(); int q = in_stream.peek(); if (q != -1 && q == '=') { in_stream.read(); return factory.create(TokenType.TripleRightShiftAssign, index, line, col, 4); } return factory.create(TokenType.ShiftRightUnsigned, index, line, col, 3); } } return factory.create(TokenType.ShiftRight, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.GreaterEqual, index, line, col, 2); } return factory.create(TokenType.GreaterThan, index, line, col); case '?': return factory.create(TokenType.Question, index, line, col); case '$': return factory.create(TokenType.Dollar, index, line, col); case ';': return factory.create(TokenType.Semicolon, index, line, col); case ':': return factory.create(TokenType.Colon, index, line, col); case ',': return factory.create(TokenType.Comma, index, line, col); case '.': if (next == '.') { in_stream.read(); int p = in_stream.peek(); if (p != -1 && p == '.') { in_stream.read(); return factory.create(TokenType.TripleDot, index, line, col, 3); } return factory.create(TokenType.DoubleDot, index, line, col, 2); } return factory.create(TokenType.Dot, index, line, col); case ')': return factory.create(TokenType.CloseParenthesis, index, line, col); case '(': return factory.create(TokenType.OpenParenthesis, index, line, col); case ']': return factory.create(TokenType.CloseSquareBracket, index, line, col); case '[': return factory.create(TokenType.OpenSquareBracket, index, line, col); case '}': return factory.create(TokenType.CloseCurlyBrace, index, line, col); case '{': return factory.create(TokenType.OpenCurlyBrace, index, line, col); case '#': return factory.create(TokenType.Hash, index, line, col); default: return null; } } LexOperator(TokenFactory factory, LexerStream in_stream); Token read(); }
LexOperator { public Token read() throws IOException, LexerException { int index = in_stream.getPosition(); int line = in_stream.getLine(); int col = in_stream.getCol(); int next = in_stream.peek(); if (next == -1) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Unexpected end of input stream when parsing operator"); } if (next == '.') { int tmp = in_stream.peek(2); if (tmp > 0 && Character.isDigit(tmp)) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Literal float encountered when operator '.' expected"); } } char cur = (char)in_stream.read(); next = in_stream.peek(); switch (cur) { case '+': switch (next) { case '+': in_stream.read(); return factory.create(TokenType.Increment, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.PlusAssign, index, line, col, 2); } return factory.create(TokenType.Plus, index, line, col); case '-': switch (next) { case '-': in_stream.read(); return factory.create(TokenType.Decrement, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.MinusAssign, index, line, col, 2); } return factory.create(TokenType.Minus, index, line, col); case '*': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.TimesAssign, index, line, col, 2); default: break; } return factory.create(TokenType.Times, index, line, col); case '/': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.DivAssign, index, line, col, 2); } return factory.create(TokenType.Div, index, line, col); case '%': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.ModAssign, index, line, col, 2); } return factory.create(TokenType.Mod, index, line, col); case '&': switch (next) { case '&': in_stream.read(); return factory.create(TokenType.LogicalAnd, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.BitwiseAndAssign, index, line, col, 2); } return factory.create(TokenType.BitwiseAnd, index, line, col); case '|': switch (next) { case '|': in_stream.read(); return factory.create(TokenType.LogicalOr, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.BitwiseOrAssign, index, line, col, 2); } return factory.create(TokenType.BitwiseOr, index, line, col); case '^': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.XorAssign, index, line, col, 2); case '^': in_stream.read(); if (in_stream.peek() == '=') { in_stream.read(); return factory.create(TokenType.PowAssign, index, line, col, 3); } return factory.create(TokenType.Pow, index, line, col, 2); } return factory.create(TokenType.Xor, index, line, col); case '!': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.NotEqual, index, line, col, 2); case '<': in_stream.read(); switch (in_stream.peek()) { case '=': in_stream.read(); return factory.create(TokenType.UnorderedOrGreater, index, line, col, 3); case '>': in_stream.read(); switch (in_stream.peek()) { case '=': in_stream.read(); return factory.create(TokenType.Unordered, index, line, col, 4); } return factory.create(TokenType.UnorderedOrEqual, index, line, col, 3); } return factory.create(TokenType.UnorderedGreaterOrEqual, index, line, col, 2); case '>': in_stream.read(); switch (in_stream.peek()) { case '=': in_stream.read(); return factory.create(TokenType.UnorderedOrLess, index, line, col, 3); default: break; } return factory.create(TokenType.UnorderedLessOrEqual, index, line, col, 2); } return factory.create(TokenType.Not, index, line, col); case '~': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.TildeAssign, index, line, col, 2); } return factory.create(TokenType.Tilde, index, line, col); case '=': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.Equal, index, line, col, 2); case '>': in_stream.read(); return factory.create(TokenType.GoesTo, index, line, col, 2); } return factory.create(TokenType.Assign, index, line, col); case '<': switch (next) { case '<': in_stream.read(); switch (in_stream.peek()) { case '=': in_stream.read(); return factory.create(TokenType.ShiftLeftAssign, index, line, col, 3); default: break; } return factory.create(TokenType.ShiftLeft, index, line, col, 2); case '>': in_stream.read(); switch (in_stream.peek()) { case '=': in_stream.read(); return factory.create(TokenType.LessEqualOrGreater, index, line, col, 3); default: break; } return factory.create(TokenType.LessOrGreater, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.LessEqual, index, line, col, 2); } return factory.create(TokenType.LessThan, index, line, col); case '>': switch (next) { case '>': in_stream.read(); int p = in_stream.peek(); if (p != -1) { switch ((char)p) { case '=': in_stream.read(); return factory.create(TokenType.ShiftRightAssign, index, line, col, 3); case '>': in_stream.read(); int q = in_stream.peek(); if (q != -1 && q == '=') { in_stream.read(); return factory.create(TokenType.TripleRightShiftAssign, index, line, col, 4); } return factory.create(TokenType.ShiftRightUnsigned, index, line, col, 3); } } return factory.create(TokenType.ShiftRight, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.GreaterEqual, index, line, col, 2); } return factory.create(TokenType.GreaterThan, index, line, col); case '?': return factory.create(TokenType.Question, index, line, col); case '$': return factory.create(TokenType.Dollar, index, line, col); case ';': return factory.create(TokenType.Semicolon, index, line, col); case ':': return factory.create(TokenType.Colon, index, line, col); case ',': return factory.create(TokenType.Comma, index, line, col); case '.': if (next == '.') { in_stream.read(); int p = in_stream.peek(); if (p != -1 && p == '.') { in_stream.read(); return factory.create(TokenType.TripleDot, index, line, col, 3); } return factory.create(TokenType.DoubleDot, index, line, col, 2); } return factory.create(TokenType.Dot, index, line, col); case ')': return factory.create(TokenType.CloseParenthesis, index, line, col); case '(': return factory.create(TokenType.OpenParenthesis, index, line, col); case ']': return factory.create(TokenType.CloseSquareBracket, index, line, col); case '[': return factory.create(TokenType.OpenSquareBracket, index, line, col); case '}': return factory.create(TokenType.CloseCurlyBrace, index, line, col); case '{': return factory.create(TokenType.OpenCurlyBrace, index, line, col); case '#': return factory.create(TokenType.Hash, index, line, col); default: return null; } } LexOperator(TokenFactory factory, LexerStream in_stream); Token read(); }
@Test(expected = LexerException.class) public void empty() throws Exception { String str = ""; LexerStream ls = new LexerStream(new StringReader(str)); LexOperator lex = new LexOperator(new BaseTokenFactory(), ls); lex.read(); }
public Token read() throws IOException, LexerException { int index = in_stream.getPosition(); int line = in_stream.getLine(); int col = in_stream.getCol(); int next = in_stream.peek(); if (next == -1) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Unexpected end of input stream when parsing operator"); } if (next == '.') { int tmp = in_stream.peek(2); if (tmp > 0 && Character.isDigit(tmp)) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Literal float encountered when operator '.' expected"); } } char cur = (char)in_stream.read(); next = in_stream.peek(); switch (cur) { case '+': switch (next) { case '+': in_stream.read(); return factory.create(TokenType.Increment, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.PlusAssign, index, line, col, 2); } return factory.create(TokenType.Plus, index, line, col); case '-': switch (next) { case '-': in_stream.read(); return factory.create(TokenType.Decrement, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.MinusAssign, index, line, col, 2); } return factory.create(TokenType.Minus, index, line, col); case '*': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.TimesAssign, index, line, col, 2); default: break; } return factory.create(TokenType.Times, index, line, col); case '/': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.DivAssign, index, line, col, 2); } return factory.create(TokenType.Div, index, line, col); case '%': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.ModAssign, index, line, col, 2); } return factory.create(TokenType.Mod, index, line, col); case '&': switch (next) { case '&': in_stream.read(); return factory.create(TokenType.LogicalAnd, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.BitwiseAndAssign, index, line, col, 2); } return factory.create(TokenType.BitwiseAnd, index, line, col); case '|': switch (next) { case '|': in_stream.read(); return factory.create(TokenType.LogicalOr, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.BitwiseOrAssign, index, line, col, 2); } return factory.create(TokenType.BitwiseOr, index, line, col); case '^': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.XorAssign, index, line, col, 2); case '^': in_stream.read(); if (in_stream.peek() == '=') { in_stream.read(); return factory.create(TokenType.PowAssign, index, line, col, 3); } return factory.create(TokenType.Pow, index, line, col, 2); } return factory.create(TokenType.Xor, index, line, col); case '!': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.NotEqual, index, line, col, 2); case '<': in_stream.read(); switch (in_stream.peek()) { case '=': in_stream.read(); return factory.create(TokenType.UnorderedOrGreater, index, line, col, 3); case '>': in_stream.read(); switch (in_stream.peek()) { case '=': in_stream.read(); return factory.create(TokenType.Unordered, index, line, col, 4); } return factory.create(TokenType.UnorderedOrEqual, index, line, col, 3); } return factory.create(TokenType.UnorderedGreaterOrEqual, index, line, col, 2); case '>': in_stream.read(); switch (in_stream.peek()) { case '=': in_stream.read(); return factory.create(TokenType.UnorderedOrLess, index, line, col, 3); default: break; } return factory.create(TokenType.UnorderedLessOrEqual, index, line, col, 2); } return factory.create(TokenType.Not, index, line, col); case '~': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.TildeAssign, index, line, col, 2); } return factory.create(TokenType.Tilde, index, line, col); case '=': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.Equal, index, line, col, 2); case '>': in_stream.read(); return factory.create(TokenType.GoesTo, index, line, col, 2); } return factory.create(TokenType.Assign, index, line, col); case '<': switch (next) { case '<': in_stream.read(); switch (in_stream.peek()) { case '=': in_stream.read(); return factory.create(TokenType.ShiftLeftAssign, index, line, col, 3); default: break; } return factory.create(TokenType.ShiftLeft, index, line, col, 2); case '>': in_stream.read(); switch (in_stream.peek()) { case '=': in_stream.read(); return factory.create(TokenType.LessEqualOrGreater, index, line, col, 3); default: break; } return factory.create(TokenType.LessOrGreater, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.LessEqual, index, line, col, 2); } return factory.create(TokenType.LessThan, index, line, col); case '>': switch (next) { case '>': in_stream.read(); int p = in_stream.peek(); if (p != -1) { switch ((char)p) { case '=': in_stream.read(); return factory.create(TokenType.ShiftRightAssign, index, line, col, 3); case '>': in_stream.read(); int q = in_stream.peek(); if (q != -1 && q == '=') { in_stream.read(); return factory.create(TokenType.TripleRightShiftAssign, index, line, col, 4); } return factory.create(TokenType.ShiftRightUnsigned, index, line, col, 3); } } return factory.create(TokenType.ShiftRight, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.GreaterEqual, index, line, col, 2); } return factory.create(TokenType.GreaterThan, index, line, col); case '?': return factory.create(TokenType.Question, index, line, col); case '$': return factory.create(TokenType.Dollar, index, line, col); case ';': return factory.create(TokenType.Semicolon, index, line, col); case ':': return factory.create(TokenType.Colon, index, line, col); case ',': return factory.create(TokenType.Comma, index, line, col); case '.': if (next == '.') { in_stream.read(); int p = in_stream.peek(); if (p != -1 && p == '.') { in_stream.read(); return factory.create(TokenType.TripleDot, index, line, col, 3); } return factory.create(TokenType.DoubleDot, index, line, col, 2); } return factory.create(TokenType.Dot, index, line, col); case ')': return factory.create(TokenType.CloseParenthesis, index, line, col); case '(': return factory.create(TokenType.OpenParenthesis, index, line, col); case ']': return factory.create(TokenType.CloseSquareBracket, index, line, col); case '[': return factory.create(TokenType.OpenSquareBracket, index, line, col); case '}': return factory.create(TokenType.CloseCurlyBrace, index, line, col); case '{': return factory.create(TokenType.OpenCurlyBrace, index, line, col); case '#': return factory.create(TokenType.Hash, index, line, col); default: return null; } }
LexOperator { public Token read() throws IOException, LexerException { int index = in_stream.getPosition(); int line = in_stream.getLine(); int col = in_stream.getCol(); int next = in_stream.peek(); if (next == -1) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Unexpected end of input stream when parsing operator"); } if (next == '.') { int tmp = in_stream.peek(2); if (tmp > 0 && Character.isDigit(tmp)) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Literal float encountered when operator '.' expected"); } } char cur = (char)in_stream.read(); next = in_stream.peek(); switch (cur) { case '+': switch (next) { case '+': in_stream.read(); return factory.create(TokenType.Increment, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.PlusAssign, index, line, col, 2); } return factory.create(TokenType.Plus, index, line, col); case '-': switch (next) { case '-': in_stream.read(); return factory.create(TokenType.Decrement, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.MinusAssign, index, line, col, 2); } return factory.create(TokenType.Minus, index, line, col); case '*': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.TimesAssign, index, line, col, 2); default: break; } return factory.create(TokenType.Times, index, line, col); case '/': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.DivAssign, index, line, col, 2); } return factory.create(TokenType.Div, index, line, col); case '%': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.ModAssign, index, line, col, 2); } return factory.create(TokenType.Mod, index, line, col); case '&': switch (next) { case '&': in_stream.read(); return factory.create(TokenType.LogicalAnd, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.BitwiseAndAssign, index, line, col, 2); } return factory.create(TokenType.BitwiseAnd, index, line, col); case '|': switch (next) { case '|': in_stream.read(); return factory.create(TokenType.LogicalOr, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.BitwiseOrAssign, index, line, col, 2); } return factory.create(TokenType.BitwiseOr, index, line, col); case '^': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.XorAssign, index, line, col, 2); case '^': in_stream.read(); if (in_stream.peek() == '=') { in_stream.read(); return factory.create(TokenType.PowAssign, index, line, col, 3); } return factory.create(TokenType.Pow, index, line, col, 2); } return factory.create(TokenType.Xor, index, line, col); case '!': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.NotEqual, index, line, col, 2); case '<': in_stream.read(); switch (in_stream.peek()) { case '=': in_stream.read(); return factory.create(TokenType.UnorderedOrGreater, index, line, col, 3); case '>': in_stream.read(); switch (in_stream.peek()) { case '=': in_stream.read(); return factory.create(TokenType.Unordered, index, line, col, 4); } return factory.create(TokenType.UnorderedOrEqual, index, line, col, 3); } return factory.create(TokenType.UnorderedGreaterOrEqual, index, line, col, 2); case '>': in_stream.read(); switch (in_stream.peek()) { case '=': in_stream.read(); return factory.create(TokenType.UnorderedOrLess, index, line, col, 3); default: break; } return factory.create(TokenType.UnorderedLessOrEqual, index, line, col, 2); } return factory.create(TokenType.Not, index, line, col); case '~': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.TildeAssign, index, line, col, 2); } return factory.create(TokenType.Tilde, index, line, col); case '=': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.Equal, index, line, col, 2); case '>': in_stream.read(); return factory.create(TokenType.GoesTo, index, line, col, 2); } return factory.create(TokenType.Assign, index, line, col); case '<': switch (next) { case '<': in_stream.read(); switch (in_stream.peek()) { case '=': in_stream.read(); return factory.create(TokenType.ShiftLeftAssign, index, line, col, 3); default: break; } return factory.create(TokenType.ShiftLeft, index, line, col, 2); case '>': in_stream.read(); switch (in_stream.peek()) { case '=': in_stream.read(); return factory.create(TokenType.LessEqualOrGreater, index, line, col, 3); default: break; } return factory.create(TokenType.LessOrGreater, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.LessEqual, index, line, col, 2); } return factory.create(TokenType.LessThan, index, line, col); case '>': switch (next) { case '>': in_stream.read(); int p = in_stream.peek(); if (p != -1) { switch ((char)p) { case '=': in_stream.read(); return factory.create(TokenType.ShiftRightAssign, index, line, col, 3); case '>': in_stream.read(); int q = in_stream.peek(); if (q != -1 && q == '=') { in_stream.read(); return factory.create(TokenType.TripleRightShiftAssign, index, line, col, 4); } return factory.create(TokenType.ShiftRightUnsigned, index, line, col, 3); } } return factory.create(TokenType.ShiftRight, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.GreaterEqual, index, line, col, 2); } return factory.create(TokenType.GreaterThan, index, line, col); case '?': return factory.create(TokenType.Question, index, line, col); case '$': return factory.create(TokenType.Dollar, index, line, col); case ';': return factory.create(TokenType.Semicolon, index, line, col); case ':': return factory.create(TokenType.Colon, index, line, col); case ',': return factory.create(TokenType.Comma, index, line, col); case '.': if (next == '.') { in_stream.read(); int p = in_stream.peek(); if (p != -1 && p == '.') { in_stream.read(); return factory.create(TokenType.TripleDot, index, line, col, 3); } return factory.create(TokenType.DoubleDot, index, line, col, 2); } return factory.create(TokenType.Dot, index, line, col); case ')': return factory.create(TokenType.CloseParenthesis, index, line, col); case '(': return factory.create(TokenType.OpenParenthesis, index, line, col); case ']': return factory.create(TokenType.CloseSquareBracket, index, line, col); case '[': return factory.create(TokenType.OpenSquareBracket, index, line, col); case '}': return factory.create(TokenType.CloseCurlyBrace, index, line, col); case '{': return factory.create(TokenType.OpenCurlyBrace, index, line, col); case '#': return factory.create(TokenType.Hash, index, line, col); default: return null; } } }
LexOperator { public Token read() throws IOException, LexerException { int index = in_stream.getPosition(); int line = in_stream.getLine(); int col = in_stream.getCol(); int next = in_stream.peek(); if (next == -1) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Unexpected end of input stream when parsing operator"); } if (next == '.') { int tmp = in_stream.peek(2); if (tmp > 0 && Character.isDigit(tmp)) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Literal float encountered when operator '.' expected"); } } char cur = (char)in_stream.read(); next = in_stream.peek(); switch (cur) { case '+': switch (next) { case '+': in_stream.read(); return factory.create(TokenType.Increment, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.PlusAssign, index, line, col, 2); } return factory.create(TokenType.Plus, index, line, col); case '-': switch (next) { case '-': in_stream.read(); return factory.create(TokenType.Decrement, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.MinusAssign, index, line, col, 2); } return factory.create(TokenType.Minus, index, line, col); case '*': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.TimesAssign, index, line, col, 2); default: break; } return factory.create(TokenType.Times, index, line, col); case '/': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.DivAssign, index, line, col, 2); } return factory.create(TokenType.Div, index, line, col); case '%': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.ModAssign, index, line, col, 2); } return factory.create(TokenType.Mod, index, line, col); case '&': switch (next) { case '&': in_stream.read(); return factory.create(TokenType.LogicalAnd, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.BitwiseAndAssign, index, line, col, 2); } return factory.create(TokenType.BitwiseAnd, index, line, col); case '|': switch (next) { case '|': in_stream.read(); return factory.create(TokenType.LogicalOr, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.BitwiseOrAssign, index, line, col, 2); } return factory.create(TokenType.BitwiseOr, index, line, col); case '^': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.XorAssign, index, line, col, 2); case '^': in_stream.read(); if (in_stream.peek() == '=') { in_stream.read(); return factory.create(TokenType.PowAssign, index, line, col, 3); } return factory.create(TokenType.Pow, index, line, col, 2); } return factory.create(TokenType.Xor, index, line, col); case '!': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.NotEqual, index, line, col, 2); case '<': in_stream.read(); switch (in_stream.peek()) { case '=': in_stream.read(); return factory.create(TokenType.UnorderedOrGreater, index, line, col, 3); case '>': in_stream.read(); switch (in_stream.peek()) { case '=': in_stream.read(); return factory.create(TokenType.Unordered, index, line, col, 4); } return factory.create(TokenType.UnorderedOrEqual, index, line, col, 3); } return factory.create(TokenType.UnorderedGreaterOrEqual, index, line, col, 2); case '>': in_stream.read(); switch (in_stream.peek()) { case '=': in_stream.read(); return factory.create(TokenType.UnorderedOrLess, index, line, col, 3); default: break; } return factory.create(TokenType.UnorderedLessOrEqual, index, line, col, 2); } return factory.create(TokenType.Not, index, line, col); case '~': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.TildeAssign, index, line, col, 2); } return factory.create(TokenType.Tilde, index, line, col); case '=': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.Equal, index, line, col, 2); case '>': in_stream.read(); return factory.create(TokenType.GoesTo, index, line, col, 2); } return factory.create(TokenType.Assign, index, line, col); case '<': switch (next) { case '<': in_stream.read(); switch (in_stream.peek()) { case '=': in_stream.read(); return factory.create(TokenType.ShiftLeftAssign, index, line, col, 3); default: break; } return factory.create(TokenType.ShiftLeft, index, line, col, 2); case '>': in_stream.read(); switch (in_stream.peek()) { case '=': in_stream.read(); return factory.create(TokenType.LessEqualOrGreater, index, line, col, 3); default: break; } return factory.create(TokenType.LessOrGreater, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.LessEqual, index, line, col, 2); } return factory.create(TokenType.LessThan, index, line, col); case '>': switch (next) { case '>': in_stream.read(); int p = in_stream.peek(); if (p != -1) { switch ((char)p) { case '=': in_stream.read(); return factory.create(TokenType.ShiftRightAssign, index, line, col, 3); case '>': in_stream.read(); int q = in_stream.peek(); if (q != -1 && q == '=') { in_stream.read(); return factory.create(TokenType.TripleRightShiftAssign, index, line, col, 4); } return factory.create(TokenType.ShiftRightUnsigned, index, line, col, 3); } } return factory.create(TokenType.ShiftRight, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.GreaterEqual, index, line, col, 2); } return factory.create(TokenType.GreaterThan, index, line, col); case '?': return factory.create(TokenType.Question, index, line, col); case '$': return factory.create(TokenType.Dollar, index, line, col); case ';': return factory.create(TokenType.Semicolon, index, line, col); case ':': return factory.create(TokenType.Colon, index, line, col); case ',': return factory.create(TokenType.Comma, index, line, col); case '.': if (next == '.') { in_stream.read(); int p = in_stream.peek(); if (p != -1 && p == '.') { in_stream.read(); return factory.create(TokenType.TripleDot, index, line, col, 3); } return factory.create(TokenType.DoubleDot, index, line, col, 2); } return factory.create(TokenType.Dot, index, line, col); case ')': return factory.create(TokenType.CloseParenthesis, index, line, col); case '(': return factory.create(TokenType.OpenParenthesis, index, line, col); case ']': return factory.create(TokenType.CloseSquareBracket, index, line, col); case '[': return factory.create(TokenType.OpenSquareBracket, index, line, col); case '}': return factory.create(TokenType.CloseCurlyBrace, index, line, col); case '{': return factory.create(TokenType.OpenCurlyBrace, index, line, col); case '#': return factory.create(TokenType.Hash, index, line, col); default: return null; } } LexOperator(TokenFactory factory, LexerStream in_stream); }
LexOperator { public Token read() throws IOException, LexerException { int index = in_stream.getPosition(); int line = in_stream.getLine(); int col = in_stream.getCol(); int next = in_stream.peek(); if (next == -1) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Unexpected end of input stream when parsing operator"); } if (next == '.') { int tmp = in_stream.peek(2); if (tmp > 0 && Character.isDigit(tmp)) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Literal float encountered when operator '.' expected"); } } char cur = (char)in_stream.read(); next = in_stream.peek(); switch (cur) { case '+': switch (next) { case '+': in_stream.read(); return factory.create(TokenType.Increment, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.PlusAssign, index, line, col, 2); } return factory.create(TokenType.Plus, index, line, col); case '-': switch (next) { case '-': in_stream.read(); return factory.create(TokenType.Decrement, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.MinusAssign, index, line, col, 2); } return factory.create(TokenType.Minus, index, line, col); case '*': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.TimesAssign, index, line, col, 2); default: break; } return factory.create(TokenType.Times, index, line, col); case '/': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.DivAssign, index, line, col, 2); } return factory.create(TokenType.Div, index, line, col); case '%': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.ModAssign, index, line, col, 2); } return factory.create(TokenType.Mod, index, line, col); case '&': switch (next) { case '&': in_stream.read(); return factory.create(TokenType.LogicalAnd, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.BitwiseAndAssign, index, line, col, 2); } return factory.create(TokenType.BitwiseAnd, index, line, col); case '|': switch (next) { case '|': in_stream.read(); return factory.create(TokenType.LogicalOr, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.BitwiseOrAssign, index, line, col, 2); } return factory.create(TokenType.BitwiseOr, index, line, col); case '^': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.XorAssign, index, line, col, 2); case '^': in_stream.read(); if (in_stream.peek() == '=') { in_stream.read(); return factory.create(TokenType.PowAssign, index, line, col, 3); } return factory.create(TokenType.Pow, index, line, col, 2); } return factory.create(TokenType.Xor, index, line, col); case '!': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.NotEqual, index, line, col, 2); case '<': in_stream.read(); switch (in_stream.peek()) { case '=': in_stream.read(); return factory.create(TokenType.UnorderedOrGreater, index, line, col, 3); case '>': in_stream.read(); switch (in_stream.peek()) { case '=': in_stream.read(); return factory.create(TokenType.Unordered, index, line, col, 4); } return factory.create(TokenType.UnorderedOrEqual, index, line, col, 3); } return factory.create(TokenType.UnorderedGreaterOrEqual, index, line, col, 2); case '>': in_stream.read(); switch (in_stream.peek()) { case '=': in_stream.read(); return factory.create(TokenType.UnorderedOrLess, index, line, col, 3); default: break; } return factory.create(TokenType.UnorderedLessOrEqual, index, line, col, 2); } return factory.create(TokenType.Not, index, line, col); case '~': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.TildeAssign, index, line, col, 2); } return factory.create(TokenType.Tilde, index, line, col); case '=': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.Equal, index, line, col, 2); case '>': in_stream.read(); return factory.create(TokenType.GoesTo, index, line, col, 2); } return factory.create(TokenType.Assign, index, line, col); case '<': switch (next) { case '<': in_stream.read(); switch (in_stream.peek()) { case '=': in_stream.read(); return factory.create(TokenType.ShiftLeftAssign, index, line, col, 3); default: break; } return factory.create(TokenType.ShiftLeft, index, line, col, 2); case '>': in_stream.read(); switch (in_stream.peek()) { case '=': in_stream.read(); return factory.create(TokenType.LessEqualOrGreater, index, line, col, 3); default: break; } return factory.create(TokenType.LessOrGreater, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.LessEqual, index, line, col, 2); } return factory.create(TokenType.LessThan, index, line, col); case '>': switch (next) { case '>': in_stream.read(); int p = in_stream.peek(); if (p != -1) { switch ((char)p) { case '=': in_stream.read(); return factory.create(TokenType.ShiftRightAssign, index, line, col, 3); case '>': in_stream.read(); int q = in_stream.peek(); if (q != -1 && q == '=') { in_stream.read(); return factory.create(TokenType.TripleRightShiftAssign, index, line, col, 4); } return factory.create(TokenType.ShiftRightUnsigned, index, line, col, 3); } } return factory.create(TokenType.ShiftRight, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.GreaterEqual, index, line, col, 2); } return factory.create(TokenType.GreaterThan, index, line, col); case '?': return factory.create(TokenType.Question, index, line, col); case '$': return factory.create(TokenType.Dollar, index, line, col); case ';': return factory.create(TokenType.Semicolon, index, line, col); case ':': return factory.create(TokenType.Colon, index, line, col); case ',': return factory.create(TokenType.Comma, index, line, col); case '.': if (next == '.') { in_stream.read(); int p = in_stream.peek(); if (p != -1 && p == '.') { in_stream.read(); return factory.create(TokenType.TripleDot, index, line, col, 3); } return factory.create(TokenType.DoubleDot, index, line, col, 2); } return factory.create(TokenType.Dot, index, line, col); case ')': return factory.create(TokenType.CloseParenthesis, index, line, col); case '(': return factory.create(TokenType.OpenParenthesis, index, line, col); case ']': return factory.create(TokenType.CloseSquareBracket, index, line, col); case '[': return factory.create(TokenType.OpenSquareBracket, index, line, col); case '}': return factory.create(TokenType.CloseCurlyBrace, index, line, col); case '{': return factory.create(TokenType.OpenCurlyBrace, index, line, col); case '#': return factory.create(TokenType.Hash, index, line, col); default: return null; } } LexOperator(TokenFactory factory, LexerStream in_stream); Token read(); }
LexOperator { public Token read() throws IOException, LexerException { int index = in_stream.getPosition(); int line = in_stream.getLine(); int col = in_stream.getCol(); int next = in_stream.peek(); if (next == -1) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Unexpected end of input stream when parsing operator"); } if (next == '.') { int tmp = in_stream.peek(2); if (tmp > 0 && Character.isDigit(tmp)) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Literal float encountered when operator '.' expected"); } } char cur = (char)in_stream.read(); next = in_stream.peek(); switch (cur) { case '+': switch (next) { case '+': in_stream.read(); return factory.create(TokenType.Increment, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.PlusAssign, index, line, col, 2); } return factory.create(TokenType.Plus, index, line, col); case '-': switch (next) { case '-': in_stream.read(); return factory.create(TokenType.Decrement, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.MinusAssign, index, line, col, 2); } return factory.create(TokenType.Minus, index, line, col); case '*': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.TimesAssign, index, line, col, 2); default: break; } return factory.create(TokenType.Times, index, line, col); case '/': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.DivAssign, index, line, col, 2); } return factory.create(TokenType.Div, index, line, col); case '%': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.ModAssign, index, line, col, 2); } return factory.create(TokenType.Mod, index, line, col); case '&': switch (next) { case '&': in_stream.read(); return factory.create(TokenType.LogicalAnd, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.BitwiseAndAssign, index, line, col, 2); } return factory.create(TokenType.BitwiseAnd, index, line, col); case '|': switch (next) { case '|': in_stream.read(); return factory.create(TokenType.LogicalOr, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.BitwiseOrAssign, index, line, col, 2); } return factory.create(TokenType.BitwiseOr, index, line, col); case '^': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.XorAssign, index, line, col, 2); case '^': in_stream.read(); if (in_stream.peek() == '=') { in_stream.read(); return factory.create(TokenType.PowAssign, index, line, col, 3); } return factory.create(TokenType.Pow, index, line, col, 2); } return factory.create(TokenType.Xor, index, line, col); case '!': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.NotEqual, index, line, col, 2); case '<': in_stream.read(); switch (in_stream.peek()) { case '=': in_stream.read(); return factory.create(TokenType.UnorderedOrGreater, index, line, col, 3); case '>': in_stream.read(); switch (in_stream.peek()) { case '=': in_stream.read(); return factory.create(TokenType.Unordered, index, line, col, 4); } return factory.create(TokenType.UnorderedOrEqual, index, line, col, 3); } return factory.create(TokenType.UnorderedGreaterOrEqual, index, line, col, 2); case '>': in_stream.read(); switch (in_stream.peek()) { case '=': in_stream.read(); return factory.create(TokenType.UnorderedOrLess, index, line, col, 3); default: break; } return factory.create(TokenType.UnorderedLessOrEqual, index, line, col, 2); } return factory.create(TokenType.Not, index, line, col); case '~': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.TildeAssign, index, line, col, 2); } return factory.create(TokenType.Tilde, index, line, col); case '=': switch (next) { case '=': in_stream.read(); return factory.create(TokenType.Equal, index, line, col, 2); case '>': in_stream.read(); return factory.create(TokenType.GoesTo, index, line, col, 2); } return factory.create(TokenType.Assign, index, line, col); case '<': switch (next) { case '<': in_stream.read(); switch (in_stream.peek()) { case '=': in_stream.read(); return factory.create(TokenType.ShiftLeftAssign, index, line, col, 3); default: break; } return factory.create(TokenType.ShiftLeft, index, line, col, 2); case '>': in_stream.read(); switch (in_stream.peek()) { case '=': in_stream.read(); return factory.create(TokenType.LessEqualOrGreater, index, line, col, 3); default: break; } return factory.create(TokenType.LessOrGreater, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.LessEqual, index, line, col, 2); } return factory.create(TokenType.LessThan, index, line, col); case '>': switch (next) { case '>': in_stream.read(); int p = in_stream.peek(); if (p != -1) { switch ((char)p) { case '=': in_stream.read(); return factory.create(TokenType.ShiftRightAssign, index, line, col, 3); case '>': in_stream.read(); int q = in_stream.peek(); if (q != -1 && q == '=') { in_stream.read(); return factory.create(TokenType.TripleRightShiftAssign, index, line, col, 4); } return factory.create(TokenType.ShiftRightUnsigned, index, line, col, 3); } } return factory.create(TokenType.ShiftRight, index, line, col, 2); case '=': in_stream.read(); return factory.create(TokenType.GreaterEqual, index, line, col, 2); } return factory.create(TokenType.GreaterThan, index, line, col); case '?': return factory.create(TokenType.Question, index, line, col); case '$': return factory.create(TokenType.Dollar, index, line, col); case ';': return factory.create(TokenType.Semicolon, index, line, col); case ':': return factory.create(TokenType.Colon, index, line, col); case ',': return factory.create(TokenType.Comma, index, line, col); case '.': if (next == '.') { in_stream.read(); int p = in_stream.peek(); if (p != -1 && p == '.') { in_stream.read(); return factory.create(TokenType.TripleDot, index, line, col, 3); } return factory.create(TokenType.DoubleDot, index, line, col, 2); } return factory.create(TokenType.Dot, index, line, col); case ')': return factory.create(TokenType.CloseParenthesis, index, line, col); case '(': return factory.create(TokenType.OpenParenthesis, index, line, col); case ']': return factory.create(TokenType.CloseSquareBracket, index, line, col); case '[': return factory.create(TokenType.OpenSquareBracket, index, line, col); case '}': return factory.create(TokenType.CloseCurlyBrace, index, line, col); case '{': return factory.create(TokenType.OpenCurlyBrace, index, line, col); case '#': return factory.create(TokenType.Hash, index, line, col); default: return null; } } LexOperator(TokenFactory factory, LexerStream in_stream); Token read(); }
@Test public void dotted() throws Exception { String str = "foo.bar"; LexerStream ls = new LexerStream(new StringReader(str)); LexIdentifier lex = new LexIdentifier(new BaseTokenFactory(), ls); Token tok = lex.read(); assertEquals(TokenType.Identifier, tok.getType()); assertEquals("foo", tok.getValue()); assertEquals(1, tok.getLine()); assertEquals(1, tok.getCol()); assertEquals(1, tok.getEndLine()); assertEquals(4, tok.getEndCol()); ls.read(); tok = lex.read(); assertEquals(TokenType.Identifier, tok.getType()); assertEquals("bar", tok.getValue()); assertEquals(1, tok.getLine()); assertEquals(5, tok.getCol()); assertEquals(1, tok.getEndLine()); assertEquals(8, tok.getEndCol()); }
public Token read() throws IOException, LexerException { int start_index = in_stream.getPosition(); int start_line = in_stream.getLine(); int start_col = in_stream.getCol(); int next = in_stream.peek(); if (next == -1) { throw new LexerException(start_line, start_col, "Unexpected end of input stream when parsing identifier"); } if (!Character.isLetter(next) && next != '_') { throw new LexerException(start_line, start_col, "Invalid character for start of identifier: "+(char)next); } StringBuilder sb = new StringBuilder(); while (valid_ident_char(next)) { sb.append(Character.toChars(in_stream.read())); next = in_stream.peek(); } String result = sb.toString(); TokenType type = TokenType.forValue(result); if (type == null) { type = TokenType.Identifier; } if (type == TokenType.Identifier && result.startsWith("__")) { throw new LexerException(start_line, start_col, "Unknown reserved identifier"); } return factory.create(type, start_index, start_line, start_col, in_stream.getPosition(), in_stream.getLine(), in_stream.getCol(), result); }
LexIdentifier { public Token read() throws IOException, LexerException { int start_index = in_stream.getPosition(); int start_line = in_stream.getLine(); int start_col = in_stream.getCol(); int next = in_stream.peek(); if (next == -1) { throw new LexerException(start_line, start_col, "Unexpected end of input stream when parsing identifier"); } if (!Character.isLetter(next) && next != '_') { throw new LexerException(start_line, start_col, "Invalid character for start of identifier: "+(char)next); } StringBuilder sb = new StringBuilder(); while (valid_ident_char(next)) { sb.append(Character.toChars(in_stream.read())); next = in_stream.peek(); } String result = sb.toString(); TokenType type = TokenType.forValue(result); if (type == null) { type = TokenType.Identifier; } if (type == TokenType.Identifier && result.startsWith("__")) { throw new LexerException(start_line, start_col, "Unknown reserved identifier"); } return factory.create(type, start_index, start_line, start_col, in_stream.getPosition(), in_stream.getLine(), in_stream.getCol(), result); } }
LexIdentifier { public Token read() throws IOException, LexerException { int start_index = in_stream.getPosition(); int start_line = in_stream.getLine(); int start_col = in_stream.getCol(); int next = in_stream.peek(); if (next == -1) { throw new LexerException(start_line, start_col, "Unexpected end of input stream when parsing identifier"); } if (!Character.isLetter(next) && next != '_') { throw new LexerException(start_line, start_col, "Invalid character for start of identifier: "+(char)next); } StringBuilder sb = new StringBuilder(); while (valid_ident_char(next)) { sb.append(Character.toChars(in_stream.read())); next = in_stream.peek(); } String result = sb.toString(); TokenType type = TokenType.forValue(result); if (type == null) { type = TokenType.Identifier; } if (type == TokenType.Identifier && result.startsWith("__")) { throw new LexerException(start_line, start_col, "Unknown reserved identifier"); } return factory.create(type, start_index, start_line, start_col, in_stream.getPosition(), in_stream.getLine(), in_stream.getCol(), result); } LexIdentifier(TokenFactory factory, LexerStream in_stream); }
LexIdentifier { public Token read() throws IOException, LexerException { int start_index = in_stream.getPosition(); int start_line = in_stream.getLine(); int start_col = in_stream.getCol(); int next = in_stream.peek(); if (next == -1) { throw new LexerException(start_line, start_col, "Unexpected end of input stream when parsing identifier"); } if (!Character.isLetter(next) && next != '_') { throw new LexerException(start_line, start_col, "Invalid character for start of identifier: "+(char)next); } StringBuilder sb = new StringBuilder(); while (valid_ident_char(next)) { sb.append(Character.toChars(in_stream.read())); next = in_stream.peek(); } String result = sb.toString(); TokenType type = TokenType.forValue(result); if (type == null) { type = TokenType.Identifier; } if (type == TokenType.Identifier && result.startsWith("__")) { throw new LexerException(start_line, start_col, "Unknown reserved identifier"); } return factory.create(type, start_index, start_line, start_col, in_stream.getPosition(), in_stream.getLine(), in_stream.getCol(), result); } LexIdentifier(TokenFactory factory, LexerStream in_stream); Token read(); }
LexIdentifier { public Token read() throws IOException, LexerException { int start_index = in_stream.getPosition(); int start_line = in_stream.getLine(); int start_col = in_stream.getCol(); int next = in_stream.peek(); if (next == -1) { throw new LexerException(start_line, start_col, "Unexpected end of input stream when parsing identifier"); } if (!Character.isLetter(next) && next != '_') { throw new LexerException(start_line, start_col, "Invalid character for start of identifier: "+(char)next); } StringBuilder sb = new StringBuilder(); while (valid_ident_char(next)) { sb.append(Character.toChars(in_stream.read())); next = in_stream.peek(); } String result = sb.toString(); TokenType type = TokenType.forValue(result); if (type == null) { type = TokenType.Identifier; } if (type == TokenType.Identifier && result.startsWith("__")) { throw new LexerException(start_line, start_col, "Unknown reserved identifier"); } return factory.create(type, start_index, start_line, start_col, in_stream.getPosition(), in_stream.getLine(), in_stream.getCol(), result); } LexIdentifier(TokenFactory factory, LexerStream in_stream); Token read(); }
@Test public void simple() throws Exception { String str = "\"Here is a string\""; LexerStream ls = new LexerStream(new StringReader(str)); LexStringLiteral lex = new LexStringLiteral(new BaseTokenFactory(), ls); Token tok = lex.read(); assertEquals(TokenType.LiteralUtf8, tok.getType()); assertTrue(tok.getValue() instanceof String); assertEquals("Here is a string", tok.getValue()); assertEquals(1, tok.getLine()); assertEquals(1, tok.getCol()); assertEquals(1, tok.getEndLine()); assertEquals(19, tok.getEndCol()); assertEquals(-1, ls.read()); }
public Token read() throws IOException, LexerException { int start_index = in_stream.getPosition(); int start_line = in_stream.getLine(); int start_col = in_stream.getCol(); boolean is_literal = false; int next = in_stream.peek(); if (next == -1) { throw new LexerException(start_line, start_col, "Unexpected end of input stream when parsing string literal"); } if (next == 'r') { is_literal = true; in_stream.read(); next = in_stream.peek(); } int initial = next; if (initial == '`') { is_literal = true; } TokenType type = TokenType.LiteralUtf8; StringBuilder sb = new StringBuilder(); in_stream.read(); next = in_stream.peek(); while (next != -1 && next != initial) { if (next == '\\' && !is_literal) { sb.append(Character.toChars(LexEscape.read(in_stream))); } else { sb.append(Character.toChars(in_stream.read())); } next = in_stream.peek(); } if (next == -1) { throw new LexerException(start_line, start_col, "Unexpected end of input stream when parsing string literal"); } in_stream.read(); next = in_stream.peek(); if (next == 'c') { type = TokenType.LiteralUtf8; in_stream.read(); } else if (next == 'w') { type = TokenType.LiteralUtf16; in_stream.read(); } else if (next == 'd') { type = TokenType.LiteralUtf32; in_stream.read(); } return factory.create(type, start_index, start_line, start_col, in_stream.getPosition(), in_stream.getLine(), in_stream.getCol(), sb.toString()); }
LexStringLiteral { public Token read() throws IOException, LexerException { int start_index = in_stream.getPosition(); int start_line = in_stream.getLine(); int start_col = in_stream.getCol(); boolean is_literal = false; int next = in_stream.peek(); if (next == -1) { throw new LexerException(start_line, start_col, "Unexpected end of input stream when parsing string literal"); } if (next == 'r') { is_literal = true; in_stream.read(); next = in_stream.peek(); } int initial = next; if (initial == '`') { is_literal = true; } TokenType type = TokenType.LiteralUtf8; StringBuilder sb = new StringBuilder(); in_stream.read(); next = in_stream.peek(); while (next != -1 && next != initial) { if (next == '\\' && !is_literal) { sb.append(Character.toChars(LexEscape.read(in_stream))); } else { sb.append(Character.toChars(in_stream.read())); } next = in_stream.peek(); } if (next == -1) { throw new LexerException(start_line, start_col, "Unexpected end of input stream when parsing string literal"); } in_stream.read(); next = in_stream.peek(); if (next == 'c') { type = TokenType.LiteralUtf8; in_stream.read(); } else if (next == 'w') { type = TokenType.LiteralUtf16; in_stream.read(); } else if (next == 'd') { type = TokenType.LiteralUtf32; in_stream.read(); } return factory.create(type, start_index, start_line, start_col, in_stream.getPosition(), in_stream.getLine(), in_stream.getCol(), sb.toString()); } }
LexStringLiteral { public Token read() throws IOException, LexerException { int start_index = in_stream.getPosition(); int start_line = in_stream.getLine(); int start_col = in_stream.getCol(); boolean is_literal = false; int next = in_stream.peek(); if (next == -1) { throw new LexerException(start_line, start_col, "Unexpected end of input stream when parsing string literal"); } if (next == 'r') { is_literal = true; in_stream.read(); next = in_stream.peek(); } int initial = next; if (initial == '`') { is_literal = true; } TokenType type = TokenType.LiteralUtf8; StringBuilder sb = new StringBuilder(); in_stream.read(); next = in_stream.peek(); while (next != -1 && next != initial) { if (next == '\\' && !is_literal) { sb.append(Character.toChars(LexEscape.read(in_stream))); } else { sb.append(Character.toChars(in_stream.read())); } next = in_stream.peek(); } if (next == -1) { throw new LexerException(start_line, start_col, "Unexpected end of input stream when parsing string literal"); } in_stream.read(); next = in_stream.peek(); if (next == 'c') { type = TokenType.LiteralUtf8; in_stream.read(); } else if (next == 'w') { type = TokenType.LiteralUtf16; in_stream.read(); } else if (next == 'd') { type = TokenType.LiteralUtf32; in_stream.read(); } return factory.create(type, start_index, start_line, start_col, in_stream.getPosition(), in_stream.getLine(), in_stream.getCol(), sb.toString()); } LexStringLiteral(TokenFactory factory, LexerStream in_stream); }
LexStringLiteral { public Token read() throws IOException, LexerException { int start_index = in_stream.getPosition(); int start_line = in_stream.getLine(); int start_col = in_stream.getCol(); boolean is_literal = false; int next = in_stream.peek(); if (next == -1) { throw new LexerException(start_line, start_col, "Unexpected end of input stream when parsing string literal"); } if (next == 'r') { is_literal = true; in_stream.read(); next = in_stream.peek(); } int initial = next; if (initial == '`') { is_literal = true; } TokenType type = TokenType.LiteralUtf8; StringBuilder sb = new StringBuilder(); in_stream.read(); next = in_stream.peek(); while (next != -1 && next != initial) { if (next == '\\' && !is_literal) { sb.append(Character.toChars(LexEscape.read(in_stream))); } else { sb.append(Character.toChars(in_stream.read())); } next = in_stream.peek(); } if (next == -1) { throw new LexerException(start_line, start_col, "Unexpected end of input stream when parsing string literal"); } in_stream.read(); next = in_stream.peek(); if (next == 'c') { type = TokenType.LiteralUtf8; in_stream.read(); } else if (next == 'w') { type = TokenType.LiteralUtf16; in_stream.read(); } else if (next == 'd') { type = TokenType.LiteralUtf32; in_stream.read(); } return factory.create(type, start_index, start_line, start_col, in_stream.getPosition(), in_stream.getLine(), in_stream.getCol(), sb.toString()); } LexStringLiteral(TokenFactory factory, LexerStream in_stream); Token read(); }
LexStringLiteral { public Token read() throws IOException, LexerException { int start_index = in_stream.getPosition(); int start_line = in_stream.getLine(); int start_col = in_stream.getCol(); boolean is_literal = false; int next = in_stream.peek(); if (next == -1) { throw new LexerException(start_line, start_col, "Unexpected end of input stream when parsing string literal"); } if (next == 'r') { is_literal = true; in_stream.read(); next = in_stream.peek(); } int initial = next; if (initial == '`') { is_literal = true; } TokenType type = TokenType.LiteralUtf8; StringBuilder sb = new StringBuilder(); in_stream.read(); next = in_stream.peek(); while (next != -1 && next != initial) { if (next == '\\' && !is_literal) { sb.append(Character.toChars(LexEscape.read(in_stream))); } else { sb.append(Character.toChars(in_stream.read())); } next = in_stream.peek(); } if (next == -1) { throw new LexerException(start_line, start_col, "Unexpected end of input stream when parsing string literal"); } in_stream.read(); next = in_stream.peek(); if (next == 'c') { type = TokenType.LiteralUtf8; in_stream.read(); } else if (next == 'w') { type = TokenType.LiteralUtf16; in_stream.read(); } else if (next == 'd') { type = TokenType.LiteralUtf32; in_stream.read(); } return factory.create(type, start_index, start_line, start_col, in_stream.getPosition(), in_stream.getLine(), in_stream.getCol(), sb.toString()); } LexStringLiteral(TokenFactory factory, LexerStream in_stream); Token read(); }
@Test public void escaped() throws Exception { String str = "\"\\n\\\"\\tfoo\\\" \\x7c\""; LexerStream ls = new LexerStream(new StringReader(str)); LexStringLiteral lex = new LexStringLiteral(new BaseTokenFactory(), ls); Token tok = lex.read(); assertEquals(TokenType.LiteralUtf8, tok.getType()); assertTrue(tok.getValue() instanceof String); assertEquals("\n\"\tfoo\" |", tok.getValue()); assertEquals(1, tok.getLine()); assertEquals(1, tok.getCol()); assertEquals(1, tok.getEndLine()); assertEquals(19, tok.getEndCol()); assertEquals(-1, ls.read()); }
public Token read() throws IOException, LexerException { int start_index = in_stream.getPosition(); int start_line = in_stream.getLine(); int start_col = in_stream.getCol(); boolean is_literal = false; int next = in_stream.peek(); if (next == -1) { throw new LexerException(start_line, start_col, "Unexpected end of input stream when parsing string literal"); } if (next == 'r') { is_literal = true; in_stream.read(); next = in_stream.peek(); } int initial = next; if (initial == '`') { is_literal = true; } TokenType type = TokenType.LiteralUtf8; StringBuilder sb = new StringBuilder(); in_stream.read(); next = in_stream.peek(); while (next != -1 && next != initial) { if (next == '\\' && !is_literal) { sb.append(Character.toChars(LexEscape.read(in_stream))); } else { sb.append(Character.toChars(in_stream.read())); } next = in_stream.peek(); } if (next == -1) { throw new LexerException(start_line, start_col, "Unexpected end of input stream when parsing string literal"); } in_stream.read(); next = in_stream.peek(); if (next == 'c') { type = TokenType.LiteralUtf8; in_stream.read(); } else if (next == 'w') { type = TokenType.LiteralUtf16; in_stream.read(); } else if (next == 'd') { type = TokenType.LiteralUtf32; in_stream.read(); } return factory.create(type, start_index, start_line, start_col, in_stream.getPosition(), in_stream.getLine(), in_stream.getCol(), sb.toString()); }
LexStringLiteral { public Token read() throws IOException, LexerException { int start_index = in_stream.getPosition(); int start_line = in_stream.getLine(); int start_col = in_stream.getCol(); boolean is_literal = false; int next = in_stream.peek(); if (next == -1) { throw new LexerException(start_line, start_col, "Unexpected end of input stream when parsing string literal"); } if (next == 'r') { is_literal = true; in_stream.read(); next = in_stream.peek(); } int initial = next; if (initial == '`') { is_literal = true; } TokenType type = TokenType.LiteralUtf8; StringBuilder sb = new StringBuilder(); in_stream.read(); next = in_stream.peek(); while (next != -1 && next != initial) { if (next == '\\' && !is_literal) { sb.append(Character.toChars(LexEscape.read(in_stream))); } else { sb.append(Character.toChars(in_stream.read())); } next = in_stream.peek(); } if (next == -1) { throw new LexerException(start_line, start_col, "Unexpected end of input stream when parsing string literal"); } in_stream.read(); next = in_stream.peek(); if (next == 'c') { type = TokenType.LiteralUtf8; in_stream.read(); } else if (next == 'w') { type = TokenType.LiteralUtf16; in_stream.read(); } else if (next == 'd') { type = TokenType.LiteralUtf32; in_stream.read(); } return factory.create(type, start_index, start_line, start_col, in_stream.getPosition(), in_stream.getLine(), in_stream.getCol(), sb.toString()); } }
LexStringLiteral { public Token read() throws IOException, LexerException { int start_index = in_stream.getPosition(); int start_line = in_stream.getLine(); int start_col = in_stream.getCol(); boolean is_literal = false; int next = in_stream.peek(); if (next == -1) { throw new LexerException(start_line, start_col, "Unexpected end of input stream when parsing string literal"); } if (next == 'r') { is_literal = true; in_stream.read(); next = in_stream.peek(); } int initial = next; if (initial == '`') { is_literal = true; } TokenType type = TokenType.LiteralUtf8; StringBuilder sb = new StringBuilder(); in_stream.read(); next = in_stream.peek(); while (next != -1 && next != initial) { if (next == '\\' && !is_literal) { sb.append(Character.toChars(LexEscape.read(in_stream))); } else { sb.append(Character.toChars(in_stream.read())); } next = in_stream.peek(); } if (next == -1) { throw new LexerException(start_line, start_col, "Unexpected end of input stream when parsing string literal"); } in_stream.read(); next = in_stream.peek(); if (next == 'c') { type = TokenType.LiteralUtf8; in_stream.read(); } else if (next == 'w') { type = TokenType.LiteralUtf16; in_stream.read(); } else if (next == 'd') { type = TokenType.LiteralUtf32; in_stream.read(); } return factory.create(type, start_index, start_line, start_col, in_stream.getPosition(), in_stream.getLine(), in_stream.getCol(), sb.toString()); } LexStringLiteral(TokenFactory factory, LexerStream in_stream); }
LexStringLiteral { public Token read() throws IOException, LexerException { int start_index = in_stream.getPosition(); int start_line = in_stream.getLine(); int start_col = in_stream.getCol(); boolean is_literal = false; int next = in_stream.peek(); if (next == -1) { throw new LexerException(start_line, start_col, "Unexpected end of input stream when parsing string literal"); } if (next == 'r') { is_literal = true; in_stream.read(); next = in_stream.peek(); } int initial = next; if (initial == '`') { is_literal = true; } TokenType type = TokenType.LiteralUtf8; StringBuilder sb = new StringBuilder(); in_stream.read(); next = in_stream.peek(); while (next != -1 && next != initial) { if (next == '\\' && !is_literal) { sb.append(Character.toChars(LexEscape.read(in_stream))); } else { sb.append(Character.toChars(in_stream.read())); } next = in_stream.peek(); } if (next == -1) { throw new LexerException(start_line, start_col, "Unexpected end of input stream when parsing string literal"); } in_stream.read(); next = in_stream.peek(); if (next == 'c') { type = TokenType.LiteralUtf8; in_stream.read(); } else if (next == 'w') { type = TokenType.LiteralUtf16; in_stream.read(); } else if (next == 'd') { type = TokenType.LiteralUtf32; in_stream.read(); } return factory.create(type, start_index, start_line, start_col, in_stream.getPosition(), in_stream.getLine(), in_stream.getCol(), sb.toString()); } LexStringLiteral(TokenFactory factory, LexerStream in_stream); Token read(); }
LexStringLiteral { public Token read() throws IOException, LexerException { int start_index = in_stream.getPosition(); int start_line = in_stream.getLine(); int start_col = in_stream.getCol(); boolean is_literal = false; int next = in_stream.peek(); if (next == -1) { throw new LexerException(start_line, start_col, "Unexpected end of input stream when parsing string literal"); } if (next == 'r') { is_literal = true; in_stream.read(); next = in_stream.peek(); } int initial = next; if (initial == '`') { is_literal = true; } TokenType type = TokenType.LiteralUtf8; StringBuilder sb = new StringBuilder(); in_stream.read(); next = in_stream.peek(); while (next != -1 && next != initial) { if (next == '\\' && !is_literal) { sb.append(Character.toChars(LexEscape.read(in_stream))); } else { sb.append(Character.toChars(in_stream.read())); } next = in_stream.peek(); } if (next == -1) { throw new LexerException(start_line, start_col, "Unexpected end of input stream when parsing string literal"); } in_stream.read(); next = in_stream.peek(); if (next == 'c') { type = TokenType.LiteralUtf8; in_stream.read(); } else if (next == 'w') { type = TokenType.LiteralUtf16; in_stream.read(); } else if (next == 'd') { type = TokenType.LiteralUtf32; in_stream.read(); } return factory.create(type, start_index, start_line, start_col, in_stream.getPosition(), in_stream.getLine(), in_stream.getCol(), sb.toString()); } LexStringLiteral(TokenFactory factory, LexerStream in_stream); Token read(); }
@Test public void wysiwyg() throws Exception { String str = "r\"a\\b\\nc\""; LexerStream ls = new LexerStream(new StringReader(str)); LexStringLiteral lex = new LexStringLiteral(new BaseTokenFactory(), ls); Token tok = lex.read(); assertEquals(TokenType.LiteralUtf8, tok.getType()); assertTrue(tok.getValue() instanceof String); assertEquals("a\\b\\nc", tok.getValue()); assertEquals(1, tok.getLine()); assertEquals(1, tok.getCol()); assertEquals(1, tok.getEndLine()); assertEquals(10, tok.getEndCol()); assertEquals(-1, ls.read()); }
public Token read() throws IOException, LexerException { int start_index = in_stream.getPosition(); int start_line = in_stream.getLine(); int start_col = in_stream.getCol(); boolean is_literal = false; int next = in_stream.peek(); if (next == -1) { throw new LexerException(start_line, start_col, "Unexpected end of input stream when parsing string literal"); } if (next == 'r') { is_literal = true; in_stream.read(); next = in_stream.peek(); } int initial = next; if (initial == '`') { is_literal = true; } TokenType type = TokenType.LiteralUtf8; StringBuilder sb = new StringBuilder(); in_stream.read(); next = in_stream.peek(); while (next != -1 && next != initial) { if (next == '\\' && !is_literal) { sb.append(Character.toChars(LexEscape.read(in_stream))); } else { sb.append(Character.toChars(in_stream.read())); } next = in_stream.peek(); } if (next == -1) { throw new LexerException(start_line, start_col, "Unexpected end of input stream when parsing string literal"); } in_stream.read(); next = in_stream.peek(); if (next == 'c') { type = TokenType.LiteralUtf8; in_stream.read(); } else if (next == 'w') { type = TokenType.LiteralUtf16; in_stream.read(); } else if (next == 'd') { type = TokenType.LiteralUtf32; in_stream.read(); } return factory.create(type, start_index, start_line, start_col, in_stream.getPosition(), in_stream.getLine(), in_stream.getCol(), sb.toString()); }
LexStringLiteral { public Token read() throws IOException, LexerException { int start_index = in_stream.getPosition(); int start_line = in_stream.getLine(); int start_col = in_stream.getCol(); boolean is_literal = false; int next = in_stream.peek(); if (next == -1) { throw new LexerException(start_line, start_col, "Unexpected end of input stream when parsing string literal"); } if (next == 'r') { is_literal = true; in_stream.read(); next = in_stream.peek(); } int initial = next; if (initial == '`') { is_literal = true; } TokenType type = TokenType.LiteralUtf8; StringBuilder sb = new StringBuilder(); in_stream.read(); next = in_stream.peek(); while (next != -1 && next != initial) { if (next == '\\' && !is_literal) { sb.append(Character.toChars(LexEscape.read(in_stream))); } else { sb.append(Character.toChars(in_stream.read())); } next = in_stream.peek(); } if (next == -1) { throw new LexerException(start_line, start_col, "Unexpected end of input stream when parsing string literal"); } in_stream.read(); next = in_stream.peek(); if (next == 'c') { type = TokenType.LiteralUtf8; in_stream.read(); } else if (next == 'w') { type = TokenType.LiteralUtf16; in_stream.read(); } else if (next == 'd') { type = TokenType.LiteralUtf32; in_stream.read(); } return factory.create(type, start_index, start_line, start_col, in_stream.getPosition(), in_stream.getLine(), in_stream.getCol(), sb.toString()); } }
LexStringLiteral { public Token read() throws IOException, LexerException { int start_index = in_stream.getPosition(); int start_line = in_stream.getLine(); int start_col = in_stream.getCol(); boolean is_literal = false; int next = in_stream.peek(); if (next == -1) { throw new LexerException(start_line, start_col, "Unexpected end of input stream when parsing string literal"); } if (next == 'r') { is_literal = true; in_stream.read(); next = in_stream.peek(); } int initial = next; if (initial == '`') { is_literal = true; } TokenType type = TokenType.LiteralUtf8; StringBuilder sb = new StringBuilder(); in_stream.read(); next = in_stream.peek(); while (next != -1 && next != initial) { if (next == '\\' && !is_literal) { sb.append(Character.toChars(LexEscape.read(in_stream))); } else { sb.append(Character.toChars(in_stream.read())); } next = in_stream.peek(); } if (next == -1) { throw new LexerException(start_line, start_col, "Unexpected end of input stream when parsing string literal"); } in_stream.read(); next = in_stream.peek(); if (next == 'c') { type = TokenType.LiteralUtf8; in_stream.read(); } else if (next == 'w') { type = TokenType.LiteralUtf16; in_stream.read(); } else if (next == 'd') { type = TokenType.LiteralUtf32; in_stream.read(); } return factory.create(type, start_index, start_line, start_col, in_stream.getPosition(), in_stream.getLine(), in_stream.getCol(), sb.toString()); } LexStringLiteral(TokenFactory factory, LexerStream in_stream); }
LexStringLiteral { public Token read() throws IOException, LexerException { int start_index = in_stream.getPosition(); int start_line = in_stream.getLine(); int start_col = in_stream.getCol(); boolean is_literal = false; int next = in_stream.peek(); if (next == -1) { throw new LexerException(start_line, start_col, "Unexpected end of input stream when parsing string literal"); } if (next == 'r') { is_literal = true; in_stream.read(); next = in_stream.peek(); } int initial = next; if (initial == '`') { is_literal = true; } TokenType type = TokenType.LiteralUtf8; StringBuilder sb = new StringBuilder(); in_stream.read(); next = in_stream.peek(); while (next != -1 && next != initial) { if (next == '\\' && !is_literal) { sb.append(Character.toChars(LexEscape.read(in_stream))); } else { sb.append(Character.toChars(in_stream.read())); } next = in_stream.peek(); } if (next == -1) { throw new LexerException(start_line, start_col, "Unexpected end of input stream when parsing string literal"); } in_stream.read(); next = in_stream.peek(); if (next == 'c') { type = TokenType.LiteralUtf8; in_stream.read(); } else if (next == 'w') { type = TokenType.LiteralUtf16; in_stream.read(); } else if (next == 'd') { type = TokenType.LiteralUtf32; in_stream.read(); } return factory.create(type, start_index, start_line, start_col, in_stream.getPosition(), in_stream.getLine(), in_stream.getCol(), sb.toString()); } LexStringLiteral(TokenFactory factory, LexerStream in_stream); Token read(); }
LexStringLiteral { public Token read() throws IOException, LexerException { int start_index = in_stream.getPosition(); int start_line = in_stream.getLine(); int start_col = in_stream.getCol(); boolean is_literal = false; int next = in_stream.peek(); if (next == -1) { throw new LexerException(start_line, start_col, "Unexpected end of input stream when parsing string literal"); } if (next == 'r') { is_literal = true; in_stream.read(); next = in_stream.peek(); } int initial = next; if (initial == '`') { is_literal = true; } TokenType type = TokenType.LiteralUtf8; StringBuilder sb = new StringBuilder(); in_stream.read(); next = in_stream.peek(); while (next != -1 && next != initial) { if (next == '\\' && !is_literal) { sb.append(Character.toChars(LexEscape.read(in_stream))); } else { sb.append(Character.toChars(in_stream.read())); } next = in_stream.peek(); } if (next == -1) { throw new LexerException(start_line, start_col, "Unexpected end of input stream when parsing string literal"); } in_stream.read(); next = in_stream.peek(); if (next == 'c') { type = TokenType.LiteralUtf8; in_stream.read(); } else if (next == 'w') { type = TokenType.LiteralUtf16; in_stream.read(); } else if (next == 'd') { type = TokenType.LiteralUtf32; in_stream.read(); } return factory.create(type, start_index, start_line, start_col, in_stream.getPosition(), in_stream.getLine(), in_stream.getCol(), sb.toString()); } LexStringLiteral(TokenFactory factory, LexerStream in_stream); Token read(); }
@Test public void tick() throws Exception { String str = "`a\\b\\nc`"; LexerStream ls = new LexerStream(new StringReader(str)); LexStringLiteral lex = new LexStringLiteral(new BaseTokenFactory(), ls); Token tok = lex.read(); assertEquals(TokenType.LiteralUtf8, tok.getType()); assertTrue(tok.getValue() instanceof String); assertEquals("a\\b\\nc", tok.getValue()); assertEquals(1, tok.getLine()); assertEquals(1, tok.getCol()); assertEquals(1, tok.getEndLine()); assertEquals(9, tok.getEndCol()); assertEquals(-1, ls.read()); }
public Token read() throws IOException, LexerException { int start_index = in_stream.getPosition(); int start_line = in_stream.getLine(); int start_col = in_stream.getCol(); boolean is_literal = false; int next = in_stream.peek(); if (next == -1) { throw new LexerException(start_line, start_col, "Unexpected end of input stream when parsing string literal"); } if (next == 'r') { is_literal = true; in_stream.read(); next = in_stream.peek(); } int initial = next; if (initial == '`') { is_literal = true; } TokenType type = TokenType.LiteralUtf8; StringBuilder sb = new StringBuilder(); in_stream.read(); next = in_stream.peek(); while (next != -1 && next != initial) { if (next == '\\' && !is_literal) { sb.append(Character.toChars(LexEscape.read(in_stream))); } else { sb.append(Character.toChars(in_stream.read())); } next = in_stream.peek(); } if (next == -1) { throw new LexerException(start_line, start_col, "Unexpected end of input stream when parsing string literal"); } in_stream.read(); next = in_stream.peek(); if (next == 'c') { type = TokenType.LiteralUtf8; in_stream.read(); } else if (next == 'w') { type = TokenType.LiteralUtf16; in_stream.read(); } else if (next == 'd') { type = TokenType.LiteralUtf32; in_stream.read(); } return factory.create(type, start_index, start_line, start_col, in_stream.getPosition(), in_stream.getLine(), in_stream.getCol(), sb.toString()); }
LexStringLiteral { public Token read() throws IOException, LexerException { int start_index = in_stream.getPosition(); int start_line = in_stream.getLine(); int start_col = in_stream.getCol(); boolean is_literal = false; int next = in_stream.peek(); if (next == -1) { throw new LexerException(start_line, start_col, "Unexpected end of input stream when parsing string literal"); } if (next == 'r') { is_literal = true; in_stream.read(); next = in_stream.peek(); } int initial = next; if (initial == '`') { is_literal = true; } TokenType type = TokenType.LiteralUtf8; StringBuilder sb = new StringBuilder(); in_stream.read(); next = in_stream.peek(); while (next != -1 && next != initial) { if (next == '\\' && !is_literal) { sb.append(Character.toChars(LexEscape.read(in_stream))); } else { sb.append(Character.toChars(in_stream.read())); } next = in_stream.peek(); } if (next == -1) { throw new LexerException(start_line, start_col, "Unexpected end of input stream when parsing string literal"); } in_stream.read(); next = in_stream.peek(); if (next == 'c') { type = TokenType.LiteralUtf8; in_stream.read(); } else if (next == 'w') { type = TokenType.LiteralUtf16; in_stream.read(); } else if (next == 'd') { type = TokenType.LiteralUtf32; in_stream.read(); } return factory.create(type, start_index, start_line, start_col, in_stream.getPosition(), in_stream.getLine(), in_stream.getCol(), sb.toString()); } }
LexStringLiteral { public Token read() throws IOException, LexerException { int start_index = in_stream.getPosition(); int start_line = in_stream.getLine(); int start_col = in_stream.getCol(); boolean is_literal = false; int next = in_stream.peek(); if (next == -1) { throw new LexerException(start_line, start_col, "Unexpected end of input stream when parsing string literal"); } if (next == 'r') { is_literal = true; in_stream.read(); next = in_stream.peek(); } int initial = next; if (initial == '`') { is_literal = true; } TokenType type = TokenType.LiteralUtf8; StringBuilder sb = new StringBuilder(); in_stream.read(); next = in_stream.peek(); while (next != -1 && next != initial) { if (next == '\\' && !is_literal) { sb.append(Character.toChars(LexEscape.read(in_stream))); } else { sb.append(Character.toChars(in_stream.read())); } next = in_stream.peek(); } if (next == -1) { throw new LexerException(start_line, start_col, "Unexpected end of input stream when parsing string literal"); } in_stream.read(); next = in_stream.peek(); if (next == 'c') { type = TokenType.LiteralUtf8; in_stream.read(); } else if (next == 'w') { type = TokenType.LiteralUtf16; in_stream.read(); } else if (next == 'd') { type = TokenType.LiteralUtf32; in_stream.read(); } return factory.create(type, start_index, start_line, start_col, in_stream.getPosition(), in_stream.getLine(), in_stream.getCol(), sb.toString()); } LexStringLiteral(TokenFactory factory, LexerStream in_stream); }
LexStringLiteral { public Token read() throws IOException, LexerException { int start_index = in_stream.getPosition(); int start_line = in_stream.getLine(); int start_col = in_stream.getCol(); boolean is_literal = false; int next = in_stream.peek(); if (next == -1) { throw new LexerException(start_line, start_col, "Unexpected end of input stream when parsing string literal"); } if (next == 'r') { is_literal = true; in_stream.read(); next = in_stream.peek(); } int initial = next; if (initial == '`') { is_literal = true; } TokenType type = TokenType.LiteralUtf8; StringBuilder sb = new StringBuilder(); in_stream.read(); next = in_stream.peek(); while (next != -1 && next != initial) { if (next == '\\' && !is_literal) { sb.append(Character.toChars(LexEscape.read(in_stream))); } else { sb.append(Character.toChars(in_stream.read())); } next = in_stream.peek(); } if (next == -1) { throw new LexerException(start_line, start_col, "Unexpected end of input stream when parsing string literal"); } in_stream.read(); next = in_stream.peek(); if (next == 'c') { type = TokenType.LiteralUtf8; in_stream.read(); } else if (next == 'w') { type = TokenType.LiteralUtf16; in_stream.read(); } else if (next == 'd') { type = TokenType.LiteralUtf32; in_stream.read(); } return factory.create(type, start_index, start_line, start_col, in_stream.getPosition(), in_stream.getLine(), in_stream.getCol(), sb.toString()); } LexStringLiteral(TokenFactory factory, LexerStream in_stream); Token read(); }
LexStringLiteral { public Token read() throws IOException, LexerException { int start_index = in_stream.getPosition(); int start_line = in_stream.getLine(); int start_col = in_stream.getCol(); boolean is_literal = false; int next = in_stream.peek(); if (next == -1) { throw new LexerException(start_line, start_col, "Unexpected end of input stream when parsing string literal"); } if (next == 'r') { is_literal = true; in_stream.read(); next = in_stream.peek(); } int initial = next; if (initial == '`') { is_literal = true; } TokenType type = TokenType.LiteralUtf8; StringBuilder sb = new StringBuilder(); in_stream.read(); next = in_stream.peek(); while (next != -1 && next != initial) { if (next == '\\' && !is_literal) { sb.append(Character.toChars(LexEscape.read(in_stream))); } else { sb.append(Character.toChars(in_stream.read())); } next = in_stream.peek(); } if (next == -1) { throw new LexerException(start_line, start_col, "Unexpected end of input stream when parsing string literal"); } in_stream.read(); next = in_stream.peek(); if (next == 'c') { type = TokenType.LiteralUtf8; in_stream.read(); } else if (next == 'w') { type = TokenType.LiteralUtf16; in_stream.read(); } else if (next == 'd') { type = TokenType.LiteralUtf32; in_stream.read(); } return factory.create(type, start_index, start_line, start_col, in_stream.getPosition(), in_stream.getLine(), in_stream.getCol(), sb.toString()); } LexStringLiteral(TokenFactory factory, LexerStream in_stream); Token read(); }
@Test public void explicit_utf8() throws Exception { String str = "\"foo\"c"; LexerStream ls = new LexerStream(new StringReader(str)); LexStringLiteral lex = new LexStringLiteral(new BaseTokenFactory(), ls); Token tok = lex.read(); assertEquals(TokenType.LiteralUtf8, tok.getType()); assertTrue(tok.getValue() instanceof String); assertEquals("foo", tok.getValue()); assertEquals(1, tok.getLine()); assertEquals(1, tok.getCol()); assertEquals(1, tok.getEndLine()); assertEquals(7, tok.getEndCol()); }
public Token read() throws IOException, LexerException { int start_index = in_stream.getPosition(); int start_line = in_stream.getLine(); int start_col = in_stream.getCol(); boolean is_literal = false; int next = in_stream.peek(); if (next == -1) { throw new LexerException(start_line, start_col, "Unexpected end of input stream when parsing string literal"); } if (next == 'r') { is_literal = true; in_stream.read(); next = in_stream.peek(); } int initial = next; if (initial == '`') { is_literal = true; } TokenType type = TokenType.LiteralUtf8; StringBuilder sb = new StringBuilder(); in_stream.read(); next = in_stream.peek(); while (next != -1 && next != initial) { if (next == '\\' && !is_literal) { sb.append(Character.toChars(LexEscape.read(in_stream))); } else { sb.append(Character.toChars(in_stream.read())); } next = in_stream.peek(); } if (next == -1) { throw new LexerException(start_line, start_col, "Unexpected end of input stream when parsing string literal"); } in_stream.read(); next = in_stream.peek(); if (next == 'c') { type = TokenType.LiteralUtf8; in_stream.read(); } else if (next == 'w') { type = TokenType.LiteralUtf16; in_stream.read(); } else if (next == 'd') { type = TokenType.LiteralUtf32; in_stream.read(); } return factory.create(type, start_index, start_line, start_col, in_stream.getPosition(), in_stream.getLine(), in_stream.getCol(), sb.toString()); }
LexStringLiteral { public Token read() throws IOException, LexerException { int start_index = in_stream.getPosition(); int start_line = in_stream.getLine(); int start_col = in_stream.getCol(); boolean is_literal = false; int next = in_stream.peek(); if (next == -1) { throw new LexerException(start_line, start_col, "Unexpected end of input stream when parsing string literal"); } if (next == 'r') { is_literal = true; in_stream.read(); next = in_stream.peek(); } int initial = next; if (initial == '`') { is_literal = true; } TokenType type = TokenType.LiteralUtf8; StringBuilder sb = new StringBuilder(); in_stream.read(); next = in_stream.peek(); while (next != -1 && next != initial) { if (next == '\\' && !is_literal) { sb.append(Character.toChars(LexEscape.read(in_stream))); } else { sb.append(Character.toChars(in_stream.read())); } next = in_stream.peek(); } if (next == -1) { throw new LexerException(start_line, start_col, "Unexpected end of input stream when parsing string literal"); } in_stream.read(); next = in_stream.peek(); if (next == 'c') { type = TokenType.LiteralUtf8; in_stream.read(); } else if (next == 'w') { type = TokenType.LiteralUtf16; in_stream.read(); } else if (next == 'd') { type = TokenType.LiteralUtf32; in_stream.read(); } return factory.create(type, start_index, start_line, start_col, in_stream.getPosition(), in_stream.getLine(), in_stream.getCol(), sb.toString()); } }
LexStringLiteral { public Token read() throws IOException, LexerException { int start_index = in_stream.getPosition(); int start_line = in_stream.getLine(); int start_col = in_stream.getCol(); boolean is_literal = false; int next = in_stream.peek(); if (next == -1) { throw new LexerException(start_line, start_col, "Unexpected end of input stream when parsing string literal"); } if (next == 'r') { is_literal = true; in_stream.read(); next = in_stream.peek(); } int initial = next; if (initial == '`') { is_literal = true; } TokenType type = TokenType.LiteralUtf8; StringBuilder sb = new StringBuilder(); in_stream.read(); next = in_stream.peek(); while (next != -1 && next != initial) { if (next == '\\' && !is_literal) { sb.append(Character.toChars(LexEscape.read(in_stream))); } else { sb.append(Character.toChars(in_stream.read())); } next = in_stream.peek(); } if (next == -1) { throw new LexerException(start_line, start_col, "Unexpected end of input stream when parsing string literal"); } in_stream.read(); next = in_stream.peek(); if (next == 'c') { type = TokenType.LiteralUtf8; in_stream.read(); } else if (next == 'w') { type = TokenType.LiteralUtf16; in_stream.read(); } else if (next == 'd') { type = TokenType.LiteralUtf32; in_stream.read(); } return factory.create(type, start_index, start_line, start_col, in_stream.getPosition(), in_stream.getLine(), in_stream.getCol(), sb.toString()); } LexStringLiteral(TokenFactory factory, LexerStream in_stream); }
LexStringLiteral { public Token read() throws IOException, LexerException { int start_index = in_stream.getPosition(); int start_line = in_stream.getLine(); int start_col = in_stream.getCol(); boolean is_literal = false; int next = in_stream.peek(); if (next == -1) { throw new LexerException(start_line, start_col, "Unexpected end of input stream when parsing string literal"); } if (next == 'r') { is_literal = true; in_stream.read(); next = in_stream.peek(); } int initial = next; if (initial == '`') { is_literal = true; } TokenType type = TokenType.LiteralUtf8; StringBuilder sb = new StringBuilder(); in_stream.read(); next = in_stream.peek(); while (next != -1 && next != initial) { if (next == '\\' && !is_literal) { sb.append(Character.toChars(LexEscape.read(in_stream))); } else { sb.append(Character.toChars(in_stream.read())); } next = in_stream.peek(); } if (next == -1) { throw new LexerException(start_line, start_col, "Unexpected end of input stream when parsing string literal"); } in_stream.read(); next = in_stream.peek(); if (next == 'c') { type = TokenType.LiteralUtf8; in_stream.read(); } else if (next == 'w') { type = TokenType.LiteralUtf16; in_stream.read(); } else if (next == 'd') { type = TokenType.LiteralUtf32; in_stream.read(); } return factory.create(type, start_index, start_line, start_col, in_stream.getPosition(), in_stream.getLine(), in_stream.getCol(), sb.toString()); } LexStringLiteral(TokenFactory factory, LexerStream in_stream); Token read(); }
LexStringLiteral { public Token read() throws IOException, LexerException { int start_index = in_stream.getPosition(); int start_line = in_stream.getLine(); int start_col = in_stream.getCol(); boolean is_literal = false; int next = in_stream.peek(); if (next == -1) { throw new LexerException(start_line, start_col, "Unexpected end of input stream when parsing string literal"); } if (next == 'r') { is_literal = true; in_stream.read(); next = in_stream.peek(); } int initial = next; if (initial == '`') { is_literal = true; } TokenType type = TokenType.LiteralUtf8; StringBuilder sb = new StringBuilder(); in_stream.read(); next = in_stream.peek(); while (next != -1 && next != initial) { if (next == '\\' && !is_literal) { sb.append(Character.toChars(LexEscape.read(in_stream))); } else { sb.append(Character.toChars(in_stream.read())); } next = in_stream.peek(); } if (next == -1) { throw new LexerException(start_line, start_col, "Unexpected end of input stream when parsing string literal"); } in_stream.read(); next = in_stream.peek(); if (next == 'c') { type = TokenType.LiteralUtf8; in_stream.read(); } else if (next == 'w') { type = TokenType.LiteralUtf16; in_stream.read(); } else if (next == 'd') { type = TokenType.LiteralUtf32; in_stream.read(); } return factory.create(type, start_index, start_line, start_col, in_stream.getPosition(), in_stream.getLine(), in_stream.getCol(), sb.toString()); } LexStringLiteral(TokenFactory factory, LexerStream in_stream); Token read(); }
@Test public void explicit_utf16() throws Exception { String str = "\"foo\"w"; LexerStream ls = new LexerStream(new StringReader(str)); LexStringLiteral lex = new LexStringLiteral(new BaseTokenFactory(), ls); Token tok = lex.read(); assertEquals(TokenType.LiteralUtf16, tok.getType()); assertTrue(tok.getValue() instanceof String); assertEquals("foo", tok.getValue()); assertEquals(1, tok.getLine()); assertEquals(1, tok.getCol()); assertEquals(1, tok.getEndLine()); assertEquals(7, tok.getEndCol()); }
public Token read() throws IOException, LexerException { int start_index = in_stream.getPosition(); int start_line = in_stream.getLine(); int start_col = in_stream.getCol(); boolean is_literal = false; int next = in_stream.peek(); if (next == -1) { throw new LexerException(start_line, start_col, "Unexpected end of input stream when parsing string literal"); } if (next == 'r') { is_literal = true; in_stream.read(); next = in_stream.peek(); } int initial = next; if (initial == '`') { is_literal = true; } TokenType type = TokenType.LiteralUtf8; StringBuilder sb = new StringBuilder(); in_stream.read(); next = in_stream.peek(); while (next != -1 && next != initial) { if (next == '\\' && !is_literal) { sb.append(Character.toChars(LexEscape.read(in_stream))); } else { sb.append(Character.toChars(in_stream.read())); } next = in_stream.peek(); } if (next == -1) { throw new LexerException(start_line, start_col, "Unexpected end of input stream when parsing string literal"); } in_stream.read(); next = in_stream.peek(); if (next == 'c') { type = TokenType.LiteralUtf8; in_stream.read(); } else if (next == 'w') { type = TokenType.LiteralUtf16; in_stream.read(); } else if (next == 'd') { type = TokenType.LiteralUtf32; in_stream.read(); } return factory.create(type, start_index, start_line, start_col, in_stream.getPosition(), in_stream.getLine(), in_stream.getCol(), sb.toString()); }
LexStringLiteral { public Token read() throws IOException, LexerException { int start_index = in_stream.getPosition(); int start_line = in_stream.getLine(); int start_col = in_stream.getCol(); boolean is_literal = false; int next = in_stream.peek(); if (next == -1) { throw new LexerException(start_line, start_col, "Unexpected end of input stream when parsing string literal"); } if (next == 'r') { is_literal = true; in_stream.read(); next = in_stream.peek(); } int initial = next; if (initial == '`') { is_literal = true; } TokenType type = TokenType.LiteralUtf8; StringBuilder sb = new StringBuilder(); in_stream.read(); next = in_stream.peek(); while (next != -1 && next != initial) { if (next == '\\' && !is_literal) { sb.append(Character.toChars(LexEscape.read(in_stream))); } else { sb.append(Character.toChars(in_stream.read())); } next = in_stream.peek(); } if (next == -1) { throw new LexerException(start_line, start_col, "Unexpected end of input stream when parsing string literal"); } in_stream.read(); next = in_stream.peek(); if (next == 'c') { type = TokenType.LiteralUtf8; in_stream.read(); } else if (next == 'w') { type = TokenType.LiteralUtf16; in_stream.read(); } else if (next == 'd') { type = TokenType.LiteralUtf32; in_stream.read(); } return factory.create(type, start_index, start_line, start_col, in_stream.getPosition(), in_stream.getLine(), in_stream.getCol(), sb.toString()); } }
LexStringLiteral { public Token read() throws IOException, LexerException { int start_index = in_stream.getPosition(); int start_line = in_stream.getLine(); int start_col = in_stream.getCol(); boolean is_literal = false; int next = in_stream.peek(); if (next == -1) { throw new LexerException(start_line, start_col, "Unexpected end of input stream when parsing string literal"); } if (next == 'r') { is_literal = true; in_stream.read(); next = in_stream.peek(); } int initial = next; if (initial == '`') { is_literal = true; } TokenType type = TokenType.LiteralUtf8; StringBuilder sb = new StringBuilder(); in_stream.read(); next = in_stream.peek(); while (next != -1 && next != initial) { if (next == '\\' && !is_literal) { sb.append(Character.toChars(LexEscape.read(in_stream))); } else { sb.append(Character.toChars(in_stream.read())); } next = in_stream.peek(); } if (next == -1) { throw new LexerException(start_line, start_col, "Unexpected end of input stream when parsing string literal"); } in_stream.read(); next = in_stream.peek(); if (next == 'c') { type = TokenType.LiteralUtf8; in_stream.read(); } else if (next == 'w') { type = TokenType.LiteralUtf16; in_stream.read(); } else if (next == 'd') { type = TokenType.LiteralUtf32; in_stream.read(); } return factory.create(type, start_index, start_line, start_col, in_stream.getPosition(), in_stream.getLine(), in_stream.getCol(), sb.toString()); } LexStringLiteral(TokenFactory factory, LexerStream in_stream); }
LexStringLiteral { public Token read() throws IOException, LexerException { int start_index = in_stream.getPosition(); int start_line = in_stream.getLine(); int start_col = in_stream.getCol(); boolean is_literal = false; int next = in_stream.peek(); if (next == -1) { throw new LexerException(start_line, start_col, "Unexpected end of input stream when parsing string literal"); } if (next == 'r') { is_literal = true; in_stream.read(); next = in_stream.peek(); } int initial = next; if (initial == '`') { is_literal = true; } TokenType type = TokenType.LiteralUtf8; StringBuilder sb = new StringBuilder(); in_stream.read(); next = in_stream.peek(); while (next != -1 && next != initial) { if (next == '\\' && !is_literal) { sb.append(Character.toChars(LexEscape.read(in_stream))); } else { sb.append(Character.toChars(in_stream.read())); } next = in_stream.peek(); } if (next == -1) { throw new LexerException(start_line, start_col, "Unexpected end of input stream when parsing string literal"); } in_stream.read(); next = in_stream.peek(); if (next == 'c') { type = TokenType.LiteralUtf8; in_stream.read(); } else if (next == 'w') { type = TokenType.LiteralUtf16; in_stream.read(); } else if (next == 'd') { type = TokenType.LiteralUtf32; in_stream.read(); } return factory.create(type, start_index, start_line, start_col, in_stream.getPosition(), in_stream.getLine(), in_stream.getCol(), sb.toString()); } LexStringLiteral(TokenFactory factory, LexerStream in_stream); Token read(); }
LexStringLiteral { public Token read() throws IOException, LexerException { int start_index = in_stream.getPosition(); int start_line = in_stream.getLine(); int start_col = in_stream.getCol(); boolean is_literal = false; int next = in_stream.peek(); if (next == -1) { throw new LexerException(start_line, start_col, "Unexpected end of input stream when parsing string literal"); } if (next == 'r') { is_literal = true; in_stream.read(); next = in_stream.peek(); } int initial = next; if (initial == '`') { is_literal = true; } TokenType type = TokenType.LiteralUtf8; StringBuilder sb = new StringBuilder(); in_stream.read(); next = in_stream.peek(); while (next != -1 && next != initial) { if (next == '\\' && !is_literal) { sb.append(Character.toChars(LexEscape.read(in_stream))); } else { sb.append(Character.toChars(in_stream.read())); } next = in_stream.peek(); } if (next == -1) { throw new LexerException(start_line, start_col, "Unexpected end of input stream when parsing string literal"); } in_stream.read(); next = in_stream.peek(); if (next == 'c') { type = TokenType.LiteralUtf8; in_stream.read(); } else if (next == 'w') { type = TokenType.LiteralUtf16; in_stream.read(); } else if (next == 'd') { type = TokenType.LiteralUtf32; in_stream.read(); } return factory.create(type, start_index, start_line, start_col, in_stream.getPosition(), in_stream.getLine(), in_stream.getCol(), sb.toString()); } LexStringLiteral(TokenFactory factory, LexerStream in_stream); Token read(); }
@Test public void explicit_utf32() throws Exception { String str = "\"foo\"d"; LexerStream ls = new LexerStream(new StringReader(str)); LexStringLiteral lex = new LexStringLiteral(new BaseTokenFactory(), ls); Token tok = lex.read(); assertEquals(TokenType.LiteralUtf32, tok.getType()); assertTrue(tok.getValue() instanceof String); assertEquals("foo", tok.getValue()); assertEquals(1, tok.getLine()); assertEquals(1, tok.getCol()); assertEquals(1, tok.getEndLine()); assertEquals(7, tok.getEndCol()); }
public Token read() throws IOException, LexerException { int start_index = in_stream.getPosition(); int start_line = in_stream.getLine(); int start_col = in_stream.getCol(); boolean is_literal = false; int next = in_stream.peek(); if (next == -1) { throw new LexerException(start_line, start_col, "Unexpected end of input stream when parsing string literal"); } if (next == 'r') { is_literal = true; in_stream.read(); next = in_stream.peek(); } int initial = next; if (initial == '`') { is_literal = true; } TokenType type = TokenType.LiteralUtf8; StringBuilder sb = new StringBuilder(); in_stream.read(); next = in_stream.peek(); while (next != -1 && next != initial) { if (next == '\\' && !is_literal) { sb.append(Character.toChars(LexEscape.read(in_stream))); } else { sb.append(Character.toChars(in_stream.read())); } next = in_stream.peek(); } if (next == -1) { throw new LexerException(start_line, start_col, "Unexpected end of input stream when parsing string literal"); } in_stream.read(); next = in_stream.peek(); if (next == 'c') { type = TokenType.LiteralUtf8; in_stream.read(); } else if (next == 'w') { type = TokenType.LiteralUtf16; in_stream.read(); } else if (next == 'd') { type = TokenType.LiteralUtf32; in_stream.read(); } return factory.create(type, start_index, start_line, start_col, in_stream.getPosition(), in_stream.getLine(), in_stream.getCol(), sb.toString()); }
LexStringLiteral { public Token read() throws IOException, LexerException { int start_index = in_stream.getPosition(); int start_line = in_stream.getLine(); int start_col = in_stream.getCol(); boolean is_literal = false; int next = in_stream.peek(); if (next == -1) { throw new LexerException(start_line, start_col, "Unexpected end of input stream when parsing string literal"); } if (next == 'r') { is_literal = true; in_stream.read(); next = in_stream.peek(); } int initial = next; if (initial == '`') { is_literal = true; } TokenType type = TokenType.LiteralUtf8; StringBuilder sb = new StringBuilder(); in_stream.read(); next = in_stream.peek(); while (next != -1 && next != initial) { if (next == '\\' && !is_literal) { sb.append(Character.toChars(LexEscape.read(in_stream))); } else { sb.append(Character.toChars(in_stream.read())); } next = in_stream.peek(); } if (next == -1) { throw new LexerException(start_line, start_col, "Unexpected end of input stream when parsing string literal"); } in_stream.read(); next = in_stream.peek(); if (next == 'c') { type = TokenType.LiteralUtf8; in_stream.read(); } else if (next == 'w') { type = TokenType.LiteralUtf16; in_stream.read(); } else if (next == 'd') { type = TokenType.LiteralUtf32; in_stream.read(); } return factory.create(type, start_index, start_line, start_col, in_stream.getPosition(), in_stream.getLine(), in_stream.getCol(), sb.toString()); } }
LexStringLiteral { public Token read() throws IOException, LexerException { int start_index = in_stream.getPosition(); int start_line = in_stream.getLine(); int start_col = in_stream.getCol(); boolean is_literal = false; int next = in_stream.peek(); if (next == -1) { throw new LexerException(start_line, start_col, "Unexpected end of input stream when parsing string literal"); } if (next == 'r') { is_literal = true; in_stream.read(); next = in_stream.peek(); } int initial = next; if (initial == '`') { is_literal = true; } TokenType type = TokenType.LiteralUtf8; StringBuilder sb = new StringBuilder(); in_stream.read(); next = in_stream.peek(); while (next != -1 && next != initial) { if (next == '\\' && !is_literal) { sb.append(Character.toChars(LexEscape.read(in_stream))); } else { sb.append(Character.toChars(in_stream.read())); } next = in_stream.peek(); } if (next == -1) { throw new LexerException(start_line, start_col, "Unexpected end of input stream when parsing string literal"); } in_stream.read(); next = in_stream.peek(); if (next == 'c') { type = TokenType.LiteralUtf8; in_stream.read(); } else if (next == 'w') { type = TokenType.LiteralUtf16; in_stream.read(); } else if (next == 'd') { type = TokenType.LiteralUtf32; in_stream.read(); } return factory.create(type, start_index, start_line, start_col, in_stream.getPosition(), in_stream.getLine(), in_stream.getCol(), sb.toString()); } LexStringLiteral(TokenFactory factory, LexerStream in_stream); }
LexStringLiteral { public Token read() throws IOException, LexerException { int start_index = in_stream.getPosition(); int start_line = in_stream.getLine(); int start_col = in_stream.getCol(); boolean is_literal = false; int next = in_stream.peek(); if (next == -1) { throw new LexerException(start_line, start_col, "Unexpected end of input stream when parsing string literal"); } if (next == 'r') { is_literal = true; in_stream.read(); next = in_stream.peek(); } int initial = next; if (initial == '`') { is_literal = true; } TokenType type = TokenType.LiteralUtf8; StringBuilder sb = new StringBuilder(); in_stream.read(); next = in_stream.peek(); while (next != -1 && next != initial) { if (next == '\\' && !is_literal) { sb.append(Character.toChars(LexEscape.read(in_stream))); } else { sb.append(Character.toChars(in_stream.read())); } next = in_stream.peek(); } if (next == -1) { throw new LexerException(start_line, start_col, "Unexpected end of input stream when parsing string literal"); } in_stream.read(); next = in_stream.peek(); if (next == 'c') { type = TokenType.LiteralUtf8; in_stream.read(); } else if (next == 'w') { type = TokenType.LiteralUtf16; in_stream.read(); } else if (next == 'd') { type = TokenType.LiteralUtf32; in_stream.read(); } return factory.create(type, start_index, start_line, start_col, in_stream.getPosition(), in_stream.getLine(), in_stream.getCol(), sb.toString()); } LexStringLiteral(TokenFactory factory, LexerStream in_stream); Token read(); }
LexStringLiteral { public Token read() throws IOException, LexerException { int start_index = in_stream.getPosition(); int start_line = in_stream.getLine(); int start_col = in_stream.getCol(); boolean is_literal = false; int next = in_stream.peek(); if (next == -1) { throw new LexerException(start_line, start_col, "Unexpected end of input stream when parsing string literal"); } if (next == 'r') { is_literal = true; in_stream.read(); next = in_stream.peek(); } int initial = next; if (initial == '`') { is_literal = true; } TokenType type = TokenType.LiteralUtf8; StringBuilder sb = new StringBuilder(); in_stream.read(); next = in_stream.peek(); while (next != -1 && next != initial) { if (next == '\\' && !is_literal) { sb.append(Character.toChars(LexEscape.read(in_stream))); } else { sb.append(Character.toChars(in_stream.read())); } next = in_stream.peek(); } if (next == -1) { throw new LexerException(start_line, start_col, "Unexpected end of input stream when parsing string literal"); } in_stream.read(); next = in_stream.peek(); if (next == 'c') { type = TokenType.LiteralUtf8; in_stream.read(); } else if (next == 'w') { type = TokenType.LiteralUtf16; in_stream.read(); } else if (next == 'd') { type = TokenType.LiteralUtf32; in_stream.read(); } return factory.create(type, start_index, start_line, start_col, in_stream.getPosition(), in_stream.getLine(), in_stream.getCol(), sb.toString()); } LexStringLiteral(TokenFactory factory, LexerStream in_stream); Token read(); }
@Test(expected = LexerException.class) public void empty() throws Exception { String str = ""; LexerStream ls = new LexerStream(new StringReader(str)); LexStringLiteral lex = new LexStringLiteral(new BaseTokenFactory(), ls); lex.read(); }
public Token read() throws IOException, LexerException { int start_index = in_stream.getPosition(); int start_line = in_stream.getLine(); int start_col = in_stream.getCol(); boolean is_literal = false; int next = in_stream.peek(); if (next == -1) { throw new LexerException(start_line, start_col, "Unexpected end of input stream when parsing string literal"); } if (next == 'r') { is_literal = true; in_stream.read(); next = in_stream.peek(); } int initial = next; if (initial == '`') { is_literal = true; } TokenType type = TokenType.LiteralUtf8; StringBuilder sb = new StringBuilder(); in_stream.read(); next = in_stream.peek(); while (next != -1 && next != initial) { if (next == '\\' && !is_literal) { sb.append(Character.toChars(LexEscape.read(in_stream))); } else { sb.append(Character.toChars(in_stream.read())); } next = in_stream.peek(); } if (next == -1) { throw new LexerException(start_line, start_col, "Unexpected end of input stream when parsing string literal"); } in_stream.read(); next = in_stream.peek(); if (next == 'c') { type = TokenType.LiteralUtf8; in_stream.read(); } else if (next == 'w') { type = TokenType.LiteralUtf16; in_stream.read(); } else if (next == 'd') { type = TokenType.LiteralUtf32; in_stream.read(); } return factory.create(type, start_index, start_line, start_col, in_stream.getPosition(), in_stream.getLine(), in_stream.getCol(), sb.toString()); }
LexStringLiteral { public Token read() throws IOException, LexerException { int start_index = in_stream.getPosition(); int start_line = in_stream.getLine(); int start_col = in_stream.getCol(); boolean is_literal = false; int next = in_stream.peek(); if (next == -1) { throw new LexerException(start_line, start_col, "Unexpected end of input stream when parsing string literal"); } if (next == 'r') { is_literal = true; in_stream.read(); next = in_stream.peek(); } int initial = next; if (initial == '`') { is_literal = true; } TokenType type = TokenType.LiteralUtf8; StringBuilder sb = new StringBuilder(); in_stream.read(); next = in_stream.peek(); while (next != -1 && next != initial) { if (next == '\\' && !is_literal) { sb.append(Character.toChars(LexEscape.read(in_stream))); } else { sb.append(Character.toChars(in_stream.read())); } next = in_stream.peek(); } if (next == -1) { throw new LexerException(start_line, start_col, "Unexpected end of input stream when parsing string literal"); } in_stream.read(); next = in_stream.peek(); if (next == 'c') { type = TokenType.LiteralUtf8; in_stream.read(); } else if (next == 'w') { type = TokenType.LiteralUtf16; in_stream.read(); } else if (next == 'd') { type = TokenType.LiteralUtf32; in_stream.read(); } return factory.create(type, start_index, start_line, start_col, in_stream.getPosition(), in_stream.getLine(), in_stream.getCol(), sb.toString()); } }
LexStringLiteral { public Token read() throws IOException, LexerException { int start_index = in_stream.getPosition(); int start_line = in_stream.getLine(); int start_col = in_stream.getCol(); boolean is_literal = false; int next = in_stream.peek(); if (next == -1) { throw new LexerException(start_line, start_col, "Unexpected end of input stream when parsing string literal"); } if (next == 'r') { is_literal = true; in_stream.read(); next = in_stream.peek(); } int initial = next; if (initial == '`') { is_literal = true; } TokenType type = TokenType.LiteralUtf8; StringBuilder sb = new StringBuilder(); in_stream.read(); next = in_stream.peek(); while (next != -1 && next != initial) { if (next == '\\' && !is_literal) { sb.append(Character.toChars(LexEscape.read(in_stream))); } else { sb.append(Character.toChars(in_stream.read())); } next = in_stream.peek(); } if (next == -1) { throw new LexerException(start_line, start_col, "Unexpected end of input stream when parsing string literal"); } in_stream.read(); next = in_stream.peek(); if (next == 'c') { type = TokenType.LiteralUtf8; in_stream.read(); } else if (next == 'w') { type = TokenType.LiteralUtf16; in_stream.read(); } else if (next == 'd') { type = TokenType.LiteralUtf32; in_stream.read(); } return factory.create(type, start_index, start_line, start_col, in_stream.getPosition(), in_stream.getLine(), in_stream.getCol(), sb.toString()); } LexStringLiteral(TokenFactory factory, LexerStream in_stream); }
LexStringLiteral { public Token read() throws IOException, LexerException { int start_index = in_stream.getPosition(); int start_line = in_stream.getLine(); int start_col = in_stream.getCol(); boolean is_literal = false; int next = in_stream.peek(); if (next == -1) { throw new LexerException(start_line, start_col, "Unexpected end of input stream when parsing string literal"); } if (next == 'r') { is_literal = true; in_stream.read(); next = in_stream.peek(); } int initial = next; if (initial == '`') { is_literal = true; } TokenType type = TokenType.LiteralUtf8; StringBuilder sb = new StringBuilder(); in_stream.read(); next = in_stream.peek(); while (next != -1 && next != initial) { if (next == '\\' && !is_literal) { sb.append(Character.toChars(LexEscape.read(in_stream))); } else { sb.append(Character.toChars(in_stream.read())); } next = in_stream.peek(); } if (next == -1) { throw new LexerException(start_line, start_col, "Unexpected end of input stream when parsing string literal"); } in_stream.read(); next = in_stream.peek(); if (next == 'c') { type = TokenType.LiteralUtf8; in_stream.read(); } else if (next == 'w') { type = TokenType.LiteralUtf16; in_stream.read(); } else if (next == 'd') { type = TokenType.LiteralUtf32; in_stream.read(); } return factory.create(type, start_index, start_line, start_col, in_stream.getPosition(), in_stream.getLine(), in_stream.getCol(), sb.toString()); } LexStringLiteral(TokenFactory factory, LexerStream in_stream); Token read(); }
LexStringLiteral { public Token read() throws IOException, LexerException { int start_index = in_stream.getPosition(); int start_line = in_stream.getLine(); int start_col = in_stream.getCol(); boolean is_literal = false; int next = in_stream.peek(); if (next == -1) { throw new LexerException(start_line, start_col, "Unexpected end of input stream when parsing string literal"); } if (next == 'r') { is_literal = true; in_stream.read(); next = in_stream.peek(); } int initial = next; if (initial == '`') { is_literal = true; } TokenType type = TokenType.LiteralUtf8; StringBuilder sb = new StringBuilder(); in_stream.read(); next = in_stream.peek(); while (next != -1 && next != initial) { if (next == '\\' && !is_literal) { sb.append(Character.toChars(LexEscape.read(in_stream))); } else { sb.append(Character.toChars(in_stream.read())); } next = in_stream.peek(); } if (next == -1) { throw new LexerException(start_line, start_col, "Unexpected end of input stream when parsing string literal"); } in_stream.read(); next = in_stream.peek(); if (next == 'c') { type = TokenType.LiteralUtf8; in_stream.read(); } else if (next == 'w') { type = TokenType.LiteralUtf16; in_stream.read(); } else if (next == 'd') { type = TokenType.LiteralUtf32; in_stream.read(); } return factory.create(type, start_index, start_line, start_col, in_stream.getPosition(), in_stream.getLine(), in_stream.getCol(), sb.toString()); } LexStringLiteral(TokenFactory factory, LexerStream in_stream); Token read(); }
@Test(expected = LexerException.class) public void unterminated() throws Exception { String str = "\"yadayada"; LexerStream ls = new LexerStream(new StringReader(str)); LexStringLiteral lex = new LexStringLiteral(new BaseTokenFactory(), ls); lex.read(); }
public Token read() throws IOException, LexerException { int start_index = in_stream.getPosition(); int start_line = in_stream.getLine(); int start_col = in_stream.getCol(); boolean is_literal = false; int next = in_stream.peek(); if (next == -1) { throw new LexerException(start_line, start_col, "Unexpected end of input stream when parsing string literal"); } if (next == 'r') { is_literal = true; in_stream.read(); next = in_stream.peek(); } int initial = next; if (initial == '`') { is_literal = true; } TokenType type = TokenType.LiteralUtf8; StringBuilder sb = new StringBuilder(); in_stream.read(); next = in_stream.peek(); while (next != -1 && next != initial) { if (next == '\\' && !is_literal) { sb.append(Character.toChars(LexEscape.read(in_stream))); } else { sb.append(Character.toChars(in_stream.read())); } next = in_stream.peek(); } if (next == -1) { throw new LexerException(start_line, start_col, "Unexpected end of input stream when parsing string literal"); } in_stream.read(); next = in_stream.peek(); if (next == 'c') { type = TokenType.LiteralUtf8; in_stream.read(); } else if (next == 'w') { type = TokenType.LiteralUtf16; in_stream.read(); } else if (next == 'd') { type = TokenType.LiteralUtf32; in_stream.read(); } return factory.create(type, start_index, start_line, start_col, in_stream.getPosition(), in_stream.getLine(), in_stream.getCol(), sb.toString()); }
LexStringLiteral { public Token read() throws IOException, LexerException { int start_index = in_stream.getPosition(); int start_line = in_stream.getLine(); int start_col = in_stream.getCol(); boolean is_literal = false; int next = in_stream.peek(); if (next == -1) { throw new LexerException(start_line, start_col, "Unexpected end of input stream when parsing string literal"); } if (next == 'r') { is_literal = true; in_stream.read(); next = in_stream.peek(); } int initial = next; if (initial == '`') { is_literal = true; } TokenType type = TokenType.LiteralUtf8; StringBuilder sb = new StringBuilder(); in_stream.read(); next = in_stream.peek(); while (next != -1 && next != initial) { if (next == '\\' && !is_literal) { sb.append(Character.toChars(LexEscape.read(in_stream))); } else { sb.append(Character.toChars(in_stream.read())); } next = in_stream.peek(); } if (next == -1) { throw new LexerException(start_line, start_col, "Unexpected end of input stream when parsing string literal"); } in_stream.read(); next = in_stream.peek(); if (next == 'c') { type = TokenType.LiteralUtf8; in_stream.read(); } else if (next == 'w') { type = TokenType.LiteralUtf16; in_stream.read(); } else if (next == 'd') { type = TokenType.LiteralUtf32; in_stream.read(); } return factory.create(type, start_index, start_line, start_col, in_stream.getPosition(), in_stream.getLine(), in_stream.getCol(), sb.toString()); } }
LexStringLiteral { public Token read() throws IOException, LexerException { int start_index = in_stream.getPosition(); int start_line = in_stream.getLine(); int start_col = in_stream.getCol(); boolean is_literal = false; int next = in_stream.peek(); if (next == -1) { throw new LexerException(start_line, start_col, "Unexpected end of input stream when parsing string literal"); } if (next == 'r') { is_literal = true; in_stream.read(); next = in_stream.peek(); } int initial = next; if (initial == '`') { is_literal = true; } TokenType type = TokenType.LiteralUtf8; StringBuilder sb = new StringBuilder(); in_stream.read(); next = in_stream.peek(); while (next != -1 && next != initial) { if (next == '\\' && !is_literal) { sb.append(Character.toChars(LexEscape.read(in_stream))); } else { sb.append(Character.toChars(in_stream.read())); } next = in_stream.peek(); } if (next == -1) { throw new LexerException(start_line, start_col, "Unexpected end of input stream when parsing string literal"); } in_stream.read(); next = in_stream.peek(); if (next == 'c') { type = TokenType.LiteralUtf8; in_stream.read(); } else if (next == 'w') { type = TokenType.LiteralUtf16; in_stream.read(); } else if (next == 'd') { type = TokenType.LiteralUtf32; in_stream.read(); } return factory.create(type, start_index, start_line, start_col, in_stream.getPosition(), in_stream.getLine(), in_stream.getCol(), sb.toString()); } LexStringLiteral(TokenFactory factory, LexerStream in_stream); }
LexStringLiteral { public Token read() throws IOException, LexerException { int start_index = in_stream.getPosition(); int start_line = in_stream.getLine(); int start_col = in_stream.getCol(); boolean is_literal = false; int next = in_stream.peek(); if (next == -1) { throw new LexerException(start_line, start_col, "Unexpected end of input stream when parsing string literal"); } if (next == 'r') { is_literal = true; in_stream.read(); next = in_stream.peek(); } int initial = next; if (initial == '`') { is_literal = true; } TokenType type = TokenType.LiteralUtf8; StringBuilder sb = new StringBuilder(); in_stream.read(); next = in_stream.peek(); while (next != -1 && next != initial) { if (next == '\\' && !is_literal) { sb.append(Character.toChars(LexEscape.read(in_stream))); } else { sb.append(Character.toChars(in_stream.read())); } next = in_stream.peek(); } if (next == -1) { throw new LexerException(start_line, start_col, "Unexpected end of input stream when parsing string literal"); } in_stream.read(); next = in_stream.peek(); if (next == 'c') { type = TokenType.LiteralUtf8; in_stream.read(); } else if (next == 'w') { type = TokenType.LiteralUtf16; in_stream.read(); } else if (next == 'd') { type = TokenType.LiteralUtf32; in_stream.read(); } return factory.create(type, start_index, start_line, start_col, in_stream.getPosition(), in_stream.getLine(), in_stream.getCol(), sb.toString()); } LexStringLiteral(TokenFactory factory, LexerStream in_stream); Token read(); }
LexStringLiteral { public Token read() throws IOException, LexerException { int start_index = in_stream.getPosition(); int start_line = in_stream.getLine(); int start_col = in_stream.getCol(); boolean is_literal = false; int next = in_stream.peek(); if (next == -1) { throw new LexerException(start_line, start_col, "Unexpected end of input stream when parsing string literal"); } if (next == 'r') { is_literal = true; in_stream.read(); next = in_stream.peek(); } int initial = next; if (initial == '`') { is_literal = true; } TokenType type = TokenType.LiteralUtf8; StringBuilder sb = new StringBuilder(); in_stream.read(); next = in_stream.peek(); while (next != -1 && next != initial) { if (next == '\\' && !is_literal) { sb.append(Character.toChars(LexEscape.read(in_stream))); } else { sb.append(Character.toChars(in_stream.read())); } next = in_stream.peek(); } if (next == -1) { throw new LexerException(start_line, start_col, "Unexpected end of input stream when parsing string literal"); } in_stream.read(); next = in_stream.peek(); if (next == 'c') { type = TokenType.LiteralUtf8; in_stream.read(); } else if (next == 'w') { type = TokenType.LiteralUtf16; in_stream.read(); } else if (next == 'd') { type = TokenType.LiteralUtf32; in_stream.read(); } return factory.create(type, start_index, start_line, start_col, in_stream.getPosition(), in_stream.getLine(), in_stream.getCol(), sb.toString()); } LexStringLiteral(TokenFactory factory, LexerStream in_stream); Token read(); }
@Test(expected = LexerException.class) public void empty() throws Exception { String str = ""; LexerStream ls = new LexerStream(new StringReader(str)); LexTokenString lex = new LexTokenString(new BaseTokenFactory(), ls, null); lex.read(); }
public Token read() throws IOException, LexerException { int start_index = in_stream.getPosition(); int start_line = in_stream.getLine(); int start_col = in_stream.getCol(); int next = in_stream.peek(); if (next == -1) { throw new LexerException(start_line, start_col, "Unexpected end of stream when parsing token string literal"); } if (next != 'q') { throw new LexerException(start_line, start_col, "Not a literal hex string"); } in_stream.read(); next = in_stream.peek(); if (next != '{') { throw new LexerException(start_line, start_col, "Not a token string"); } in_stream.read(); StringBuilder result = new StringBuilder(); Token token = parent.next(); while (token != null && token.getType() != TokenType.EOF) { if (token.getType() == TokenType.CloseCurlyBrace) { break; } result.append(token.getValue()); token = parent.next(); if (token == null || token.getType().equals(TokenType.EOF) || token.getType().equals(TokenType.Unknown)) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Invalid token inside token string"); } } if (token == null || token.getType() != TokenType.CloseCurlyBrace) { throw new LexerException(start_line, start_col, "Unexpected end of stream when parsing token string literal"); } return factory.create(TokenType.LiteralUtf8, start_index, start_line, start_col, in_stream.getPosition(), in_stream.getLine(), in_stream.getCol(), result.toString()); }
LexTokenString { public Token read() throws IOException, LexerException { int start_index = in_stream.getPosition(); int start_line = in_stream.getLine(); int start_col = in_stream.getCol(); int next = in_stream.peek(); if (next == -1) { throw new LexerException(start_line, start_col, "Unexpected end of stream when parsing token string literal"); } if (next != 'q') { throw new LexerException(start_line, start_col, "Not a literal hex string"); } in_stream.read(); next = in_stream.peek(); if (next != '{') { throw new LexerException(start_line, start_col, "Not a token string"); } in_stream.read(); StringBuilder result = new StringBuilder(); Token token = parent.next(); while (token != null && token.getType() != TokenType.EOF) { if (token.getType() == TokenType.CloseCurlyBrace) { break; } result.append(token.getValue()); token = parent.next(); if (token == null || token.getType().equals(TokenType.EOF) || token.getType().equals(TokenType.Unknown)) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Invalid token inside token string"); } } if (token == null || token.getType() != TokenType.CloseCurlyBrace) { throw new LexerException(start_line, start_col, "Unexpected end of stream when parsing token string literal"); } return factory.create(TokenType.LiteralUtf8, start_index, start_line, start_col, in_stream.getPosition(), in_stream.getLine(), in_stream.getCol(), result.toString()); } }
LexTokenString { public Token read() throws IOException, LexerException { int start_index = in_stream.getPosition(); int start_line = in_stream.getLine(); int start_col = in_stream.getCol(); int next = in_stream.peek(); if (next == -1) { throw new LexerException(start_line, start_col, "Unexpected end of stream when parsing token string literal"); } if (next != 'q') { throw new LexerException(start_line, start_col, "Not a literal hex string"); } in_stream.read(); next = in_stream.peek(); if (next != '{') { throw new LexerException(start_line, start_col, "Not a token string"); } in_stream.read(); StringBuilder result = new StringBuilder(); Token token = parent.next(); while (token != null && token.getType() != TokenType.EOF) { if (token.getType() == TokenType.CloseCurlyBrace) { break; } result.append(token.getValue()); token = parent.next(); if (token == null || token.getType().equals(TokenType.EOF) || token.getType().equals(TokenType.Unknown)) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Invalid token inside token string"); } } if (token == null || token.getType() != TokenType.CloseCurlyBrace) { throw new LexerException(start_line, start_col, "Unexpected end of stream when parsing token string literal"); } return factory.create(TokenType.LiteralUtf8, start_index, start_line, start_col, in_stream.getPosition(), in_stream.getLine(), in_stream.getCol(), result.toString()); } LexTokenString(TokenFactory factory, LexerStream in_stream, Lexer parent); }
LexTokenString { public Token read() throws IOException, LexerException { int start_index = in_stream.getPosition(); int start_line = in_stream.getLine(); int start_col = in_stream.getCol(); int next = in_stream.peek(); if (next == -1) { throw new LexerException(start_line, start_col, "Unexpected end of stream when parsing token string literal"); } if (next != 'q') { throw new LexerException(start_line, start_col, "Not a literal hex string"); } in_stream.read(); next = in_stream.peek(); if (next != '{') { throw new LexerException(start_line, start_col, "Not a token string"); } in_stream.read(); StringBuilder result = new StringBuilder(); Token token = parent.next(); while (token != null && token.getType() != TokenType.EOF) { if (token.getType() == TokenType.CloseCurlyBrace) { break; } result.append(token.getValue()); token = parent.next(); if (token == null || token.getType().equals(TokenType.EOF) || token.getType().equals(TokenType.Unknown)) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Invalid token inside token string"); } } if (token == null || token.getType() != TokenType.CloseCurlyBrace) { throw new LexerException(start_line, start_col, "Unexpected end of stream when parsing token string literal"); } return factory.create(TokenType.LiteralUtf8, start_index, start_line, start_col, in_stream.getPosition(), in_stream.getLine(), in_stream.getCol(), result.toString()); } LexTokenString(TokenFactory factory, LexerStream in_stream, Lexer parent); Token read(); }
LexTokenString { public Token read() throws IOException, LexerException { int start_index = in_stream.getPosition(); int start_line = in_stream.getLine(); int start_col = in_stream.getCol(); int next = in_stream.peek(); if (next == -1) { throw new LexerException(start_line, start_col, "Unexpected end of stream when parsing token string literal"); } if (next != 'q') { throw new LexerException(start_line, start_col, "Not a literal hex string"); } in_stream.read(); next = in_stream.peek(); if (next != '{') { throw new LexerException(start_line, start_col, "Not a token string"); } in_stream.read(); StringBuilder result = new StringBuilder(); Token token = parent.next(); while (token != null && token.getType() != TokenType.EOF) { if (token.getType() == TokenType.CloseCurlyBrace) { break; } result.append(token.getValue()); token = parent.next(); if (token == null || token.getType().equals(TokenType.EOF) || token.getType().equals(TokenType.Unknown)) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Invalid token inside token string"); } } if (token == null || token.getType() != TokenType.CloseCurlyBrace) { throw new LexerException(start_line, start_col, "Unexpected end of stream when parsing token string literal"); } return factory.create(TokenType.LiteralUtf8, start_index, start_line, start_col, in_stream.getPosition(), in_stream.getLine(), in_stream.getCol(), result.toString()); } LexTokenString(TokenFactory factory, LexerStream in_stream, Lexer parent); Token read(); }
@Test(expected = LexerException.class) public void reserved() throws Exception { String str = "__FOO__"; LexerStream ls = new LexerStream(new StringReader(str)); LexIdentifier lex = new LexIdentifier(new BaseTokenFactory(), ls); lex.read(); }
public Token read() throws IOException, LexerException { int start_index = in_stream.getPosition(); int start_line = in_stream.getLine(); int start_col = in_stream.getCol(); int next = in_stream.peek(); if (next == -1) { throw new LexerException(start_line, start_col, "Unexpected end of input stream when parsing identifier"); } if (!Character.isLetter(next) && next != '_') { throw new LexerException(start_line, start_col, "Invalid character for start of identifier: "+(char)next); } StringBuilder sb = new StringBuilder(); while (valid_ident_char(next)) { sb.append(Character.toChars(in_stream.read())); next = in_stream.peek(); } String result = sb.toString(); TokenType type = TokenType.forValue(result); if (type == null) { type = TokenType.Identifier; } if (type == TokenType.Identifier && result.startsWith("__")) { throw new LexerException(start_line, start_col, "Unknown reserved identifier"); } return factory.create(type, start_index, start_line, start_col, in_stream.getPosition(), in_stream.getLine(), in_stream.getCol(), result); }
LexIdentifier { public Token read() throws IOException, LexerException { int start_index = in_stream.getPosition(); int start_line = in_stream.getLine(); int start_col = in_stream.getCol(); int next = in_stream.peek(); if (next == -1) { throw new LexerException(start_line, start_col, "Unexpected end of input stream when parsing identifier"); } if (!Character.isLetter(next) && next != '_') { throw new LexerException(start_line, start_col, "Invalid character for start of identifier: "+(char)next); } StringBuilder sb = new StringBuilder(); while (valid_ident_char(next)) { sb.append(Character.toChars(in_stream.read())); next = in_stream.peek(); } String result = sb.toString(); TokenType type = TokenType.forValue(result); if (type == null) { type = TokenType.Identifier; } if (type == TokenType.Identifier && result.startsWith("__")) { throw new LexerException(start_line, start_col, "Unknown reserved identifier"); } return factory.create(type, start_index, start_line, start_col, in_stream.getPosition(), in_stream.getLine(), in_stream.getCol(), result); } }
LexIdentifier { public Token read() throws IOException, LexerException { int start_index = in_stream.getPosition(); int start_line = in_stream.getLine(); int start_col = in_stream.getCol(); int next = in_stream.peek(); if (next == -1) { throw new LexerException(start_line, start_col, "Unexpected end of input stream when parsing identifier"); } if (!Character.isLetter(next) && next != '_') { throw new LexerException(start_line, start_col, "Invalid character for start of identifier: "+(char)next); } StringBuilder sb = new StringBuilder(); while (valid_ident_char(next)) { sb.append(Character.toChars(in_stream.read())); next = in_stream.peek(); } String result = sb.toString(); TokenType type = TokenType.forValue(result); if (type == null) { type = TokenType.Identifier; } if (type == TokenType.Identifier && result.startsWith("__")) { throw new LexerException(start_line, start_col, "Unknown reserved identifier"); } return factory.create(type, start_index, start_line, start_col, in_stream.getPosition(), in_stream.getLine(), in_stream.getCol(), result); } LexIdentifier(TokenFactory factory, LexerStream in_stream); }
LexIdentifier { public Token read() throws IOException, LexerException { int start_index = in_stream.getPosition(); int start_line = in_stream.getLine(); int start_col = in_stream.getCol(); int next = in_stream.peek(); if (next == -1) { throw new LexerException(start_line, start_col, "Unexpected end of input stream when parsing identifier"); } if (!Character.isLetter(next) && next != '_') { throw new LexerException(start_line, start_col, "Invalid character for start of identifier: "+(char)next); } StringBuilder sb = new StringBuilder(); while (valid_ident_char(next)) { sb.append(Character.toChars(in_stream.read())); next = in_stream.peek(); } String result = sb.toString(); TokenType type = TokenType.forValue(result); if (type == null) { type = TokenType.Identifier; } if (type == TokenType.Identifier && result.startsWith("__")) { throw new LexerException(start_line, start_col, "Unknown reserved identifier"); } return factory.create(type, start_index, start_line, start_col, in_stream.getPosition(), in_stream.getLine(), in_stream.getCol(), result); } LexIdentifier(TokenFactory factory, LexerStream in_stream); Token read(); }
LexIdentifier { public Token read() throws IOException, LexerException { int start_index = in_stream.getPosition(); int start_line = in_stream.getLine(); int start_col = in_stream.getCol(); int next = in_stream.peek(); if (next == -1) { throw new LexerException(start_line, start_col, "Unexpected end of input stream when parsing identifier"); } if (!Character.isLetter(next) && next != '_') { throw new LexerException(start_line, start_col, "Invalid character for start of identifier: "+(char)next); } StringBuilder sb = new StringBuilder(); while (valid_ident_char(next)) { sb.append(Character.toChars(in_stream.read())); next = in_stream.peek(); } String result = sb.toString(); TokenType type = TokenType.forValue(result); if (type == null) { type = TokenType.Identifier; } if (type == TokenType.Identifier && result.startsWith("__")) { throw new LexerException(start_line, start_col, "Unknown reserved identifier"); } return factory.create(type, start_index, start_line, start_col, in_stream.getPosition(), in_stream.getLine(), in_stream.getCol(), result); } LexIdentifier(TokenFactory factory, LexerStream in_stream); Token read(); }
@Test(expected = LexerException.class) public void not() throws Exception { String str = "q[]"; LexerStream ls = new LexerStream(new StringReader(str)); LexTokenString lex = new LexTokenString(new BaseTokenFactory(), ls, null); lex.read(); }
public Token read() throws IOException, LexerException { int start_index = in_stream.getPosition(); int start_line = in_stream.getLine(); int start_col = in_stream.getCol(); int next = in_stream.peek(); if (next == -1) { throw new LexerException(start_line, start_col, "Unexpected end of stream when parsing token string literal"); } if (next != 'q') { throw new LexerException(start_line, start_col, "Not a literal hex string"); } in_stream.read(); next = in_stream.peek(); if (next != '{') { throw new LexerException(start_line, start_col, "Not a token string"); } in_stream.read(); StringBuilder result = new StringBuilder(); Token token = parent.next(); while (token != null && token.getType() != TokenType.EOF) { if (token.getType() == TokenType.CloseCurlyBrace) { break; } result.append(token.getValue()); token = parent.next(); if (token == null || token.getType().equals(TokenType.EOF) || token.getType().equals(TokenType.Unknown)) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Invalid token inside token string"); } } if (token == null || token.getType() != TokenType.CloseCurlyBrace) { throw new LexerException(start_line, start_col, "Unexpected end of stream when parsing token string literal"); } return factory.create(TokenType.LiteralUtf8, start_index, start_line, start_col, in_stream.getPosition(), in_stream.getLine(), in_stream.getCol(), result.toString()); }
LexTokenString { public Token read() throws IOException, LexerException { int start_index = in_stream.getPosition(); int start_line = in_stream.getLine(); int start_col = in_stream.getCol(); int next = in_stream.peek(); if (next == -1) { throw new LexerException(start_line, start_col, "Unexpected end of stream when parsing token string literal"); } if (next != 'q') { throw new LexerException(start_line, start_col, "Not a literal hex string"); } in_stream.read(); next = in_stream.peek(); if (next != '{') { throw new LexerException(start_line, start_col, "Not a token string"); } in_stream.read(); StringBuilder result = new StringBuilder(); Token token = parent.next(); while (token != null && token.getType() != TokenType.EOF) { if (token.getType() == TokenType.CloseCurlyBrace) { break; } result.append(token.getValue()); token = parent.next(); if (token == null || token.getType().equals(TokenType.EOF) || token.getType().equals(TokenType.Unknown)) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Invalid token inside token string"); } } if (token == null || token.getType() != TokenType.CloseCurlyBrace) { throw new LexerException(start_line, start_col, "Unexpected end of stream when parsing token string literal"); } return factory.create(TokenType.LiteralUtf8, start_index, start_line, start_col, in_stream.getPosition(), in_stream.getLine(), in_stream.getCol(), result.toString()); } }
LexTokenString { public Token read() throws IOException, LexerException { int start_index = in_stream.getPosition(); int start_line = in_stream.getLine(); int start_col = in_stream.getCol(); int next = in_stream.peek(); if (next == -1) { throw new LexerException(start_line, start_col, "Unexpected end of stream when parsing token string literal"); } if (next != 'q') { throw new LexerException(start_line, start_col, "Not a literal hex string"); } in_stream.read(); next = in_stream.peek(); if (next != '{') { throw new LexerException(start_line, start_col, "Not a token string"); } in_stream.read(); StringBuilder result = new StringBuilder(); Token token = parent.next(); while (token != null && token.getType() != TokenType.EOF) { if (token.getType() == TokenType.CloseCurlyBrace) { break; } result.append(token.getValue()); token = parent.next(); if (token == null || token.getType().equals(TokenType.EOF) || token.getType().equals(TokenType.Unknown)) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Invalid token inside token string"); } } if (token == null || token.getType() != TokenType.CloseCurlyBrace) { throw new LexerException(start_line, start_col, "Unexpected end of stream when parsing token string literal"); } return factory.create(TokenType.LiteralUtf8, start_index, start_line, start_col, in_stream.getPosition(), in_stream.getLine(), in_stream.getCol(), result.toString()); } LexTokenString(TokenFactory factory, LexerStream in_stream, Lexer parent); }
LexTokenString { public Token read() throws IOException, LexerException { int start_index = in_stream.getPosition(); int start_line = in_stream.getLine(); int start_col = in_stream.getCol(); int next = in_stream.peek(); if (next == -1) { throw new LexerException(start_line, start_col, "Unexpected end of stream when parsing token string literal"); } if (next != 'q') { throw new LexerException(start_line, start_col, "Not a literal hex string"); } in_stream.read(); next = in_stream.peek(); if (next != '{') { throw new LexerException(start_line, start_col, "Not a token string"); } in_stream.read(); StringBuilder result = new StringBuilder(); Token token = parent.next(); while (token != null && token.getType() != TokenType.EOF) { if (token.getType() == TokenType.CloseCurlyBrace) { break; } result.append(token.getValue()); token = parent.next(); if (token == null || token.getType().equals(TokenType.EOF) || token.getType().equals(TokenType.Unknown)) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Invalid token inside token string"); } } if (token == null || token.getType() != TokenType.CloseCurlyBrace) { throw new LexerException(start_line, start_col, "Unexpected end of stream when parsing token string literal"); } return factory.create(TokenType.LiteralUtf8, start_index, start_line, start_col, in_stream.getPosition(), in_stream.getLine(), in_stream.getCol(), result.toString()); } LexTokenString(TokenFactory factory, LexerStream in_stream, Lexer parent); Token read(); }
LexTokenString { public Token read() throws IOException, LexerException { int start_index = in_stream.getPosition(); int start_line = in_stream.getLine(); int start_col = in_stream.getCol(); int next = in_stream.peek(); if (next == -1) { throw new LexerException(start_line, start_col, "Unexpected end of stream when parsing token string literal"); } if (next != 'q') { throw new LexerException(start_line, start_col, "Not a literal hex string"); } in_stream.read(); next = in_stream.peek(); if (next != '{') { throw new LexerException(start_line, start_col, "Not a token string"); } in_stream.read(); StringBuilder result = new StringBuilder(); Token token = parent.next(); while (token != null && token.getType() != TokenType.EOF) { if (token.getType() == TokenType.CloseCurlyBrace) { break; } result.append(token.getValue()); token = parent.next(); if (token == null || token.getType().equals(TokenType.EOF) || token.getType().equals(TokenType.Unknown)) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Invalid token inside token string"); } } if (token == null || token.getType() != TokenType.CloseCurlyBrace) { throw new LexerException(start_line, start_col, "Unexpected end of stream when parsing token string literal"); } return factory.create(TokenType.LiteralUtf8, start_index, start_line, start_col, in_stream.getPosition(), in_stream.getLine(), in_stream.getCol(), result.toString()); } LexTokenString(TokenFactory factory, LexerStream in_stream, Lexer parent); Token read(); }
@Test(expected = LexerException.class) public void also_not() throws Exception { String str = "\"q{}\""; LexerStream ls = new LexerStream(new StringReader(str)); LexTokenString lex = new LexTokenString(new BaseTokenFactory(), ls, null); lex.read(); }
public Token read() throws IOException, LexerException { int start_index = in_stream.getPosition(); int start_line = in_stream.getLine(); int start_col = in_stream.getCol(); int next = in_stream.peek(); if (next == -1) { throw new LexerException(start_line, start_col, "Unexpected end of stream when parsing token string literal"); } if (next != 'q') { throw new LexerException(start_line, start_col, "Not a literal hex string"); } in_stream.read(); next = in_stream.peek(); if (next != '{') { throw new LexerException(start_line, start_col, "Not a token string"); } in_stream.read(); StringBuilder result = new StringBuilder(); Token token = parent.next(); while (token != null && token.getType() != TokenType.EOF) { if (token.getType() == TokenType.CloseCurlyBrace) { break; } result.append(token.getValue()); token = parent.next(); if (token == null || token.getType().equals(TokenType.EOF) || token.getType().equals(TokenType.Unknown)) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Invalid token inside token string"); } } if (token == null || token.getType() != TokenType.CloseCurlyBrace) { throw new LexerException(start_line, start_col, "Unexpected end of stream when parsing token string literal"); } return factory.create(TokenType.LiteralUtf8, start_index, start_line, start_col, in_stream.getPosition(), in_stream.getLine(), in_stream.getCol(), result.toString()); }
LexTokenString { public Token read() throws IOException, LexerException { int start_index = in_stream.getPosition(); int start_line = in_stream.getLine(); int start_col = in_stream.getCol(); int next = in_stream.peek(); if (next == -1) { throw new LexerException(start_line, start_col, "Unexpected end of stream when parsing token string literal"); } if (next != 'q') { throw new LexerException(start_line, start_col, "Not a literal hex string"); } in_stream.read(); next = in_stream.peek(); if (next != '{') { throw new LexerException(start_line, start_col, "Not a token string"); } in_stream.read(); StringBuilder result = new StringBuilder(); Token token = parent.next(); while (token != null && token.getType() != TokenType.EOF) { if (token.getType() == TokenType.CloseCurlyBrace) { break; } result.append(token.getValue()); token = parent.next(); if (token == null || token.getType().equals(TokenType.EOF) || token.getType().equals(TokenType.Unknown)) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Invalid token inside token string"); } } if (token == null || token.getType() != TokenType.CloseCurlyBrace) { throw new LexerException(start_line, start_col, "Unexpected end of stream when parsing token string literal"); } return factory.create(TokenType.LiteralUtf8, start_index, start_line, start_col, in_stream.getPosition(), in_stream.getLine(), in_stream.getCol(), result.toString()); } }
LexTokenString { public Token read() throws IOException, LexerException { int start_index = in_stream.getPosition(); int start_line = in_stream.getLine(); int start_col = in_stream.getCol(); int next = in_stream.peek(); if (next == -1) { throw new LexerException(start_line, start_col, "Unexpected end of stream when parsing token string literal"); } if (next != 'q') { throw new LexerException(start_line, start_col, "Not a literal hex string"); } in_stream.read(); next = in_stream.peek(); if (next != '{') { throw new LexerException(start_line, start_col, "Not a token string"); } in_stream.read(); StringBuilder result = new StringBuilder(); Token token = parent.next(); while (token != null && token.getType() != TokenType.EOF) { if (token.getType() == TokenType.CloseCurlyBrace) { break; } result.append(token.getValue()); token = parent.next(); if (token == null || token.getType().equals(TokenType.EOF) || token.getType().equals(TokenType.Unknown)) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Invalid token inside token string"); } } if (token == null || token.getType() != TokenType.CloseCurlyBrace) { throw new LexerException(start_line, start_col, "Unexpected end of stream when parsing token string literal"); } return factory.create(TokenType.LiteralUtf8, start_index, start_line, start_col, in_stream.getPosition(), in_stream.getLine(), in_stream.getCol(), result.toString()); } LexTokenString(TokenFactory factory, LexerStream in_stream, Lexer parent); }
LexTokenString { public Token read() throws IOException, LexerException { int start_index = in_stream.getPosition(); int start_line = in_stream.getLine(); int start_col = in_stream.getCol(); int next = in_stream.peek(); if (next == -1) { throw new LexerException(start_line, start_col, "Unexpected end of stream when parsing token string literal"); } if (next != 'q') { throw new LexerException(start_line, start_col, "Not a literal hex string"); } in_stream.read(); next = in_stream.peek(); if (next != '{') { throw new LexerException(start_line, start_col, "Not a token string"); } in_stream.read(); StringBuilder result = new StringBuilder(); Token token = parent.next(); while (token != null && token.getType() != TokenType.EOF) { if (token.getType() == TokenType.CloseCurlyBrace) { break; } result.append(token.getValue()); token = parent.next(); if (token == null || token.getType().equals(TokenType.EOF) || token.getType().equals(TokenType.Unknown)) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Invalid token inside token string"); } } if (token == null || token.getType() != TokenType.CloseCurlyBrace) { throw new LexerException(start_line, start_col, "Unexpected end of stream when parsing token string literal"); } return factory.create(TokenType.LiteralUtf8, start_index, start_line, start_col, in_stream.getPosition(), in_stream.getLine(), in_stream.getCol(), result.toString()); } LexTokenString(TokenFactory factory, LexerStream in_stream, Lexer parent); Token read(); }
LexTokenString { public Token read() throws IOException, LexerException { int start_index = in_stream.getPosition(); int start_line = in_stream.getLine(); int start_col = in_stream.getCol(); int next = in_stream.peek(); if (next == -1) { throw new LexerException(start_line, start_col, "Unexpected end of stream when parsing token string literal"); } if (next != 'q') { throw new LexerException(start_line, start_col, "Not a literal hex string"); } in_stream.read(); next = in_stream.peek(); if (next != '{') { throw new LexerException(start_line, start_col, "Not a token string"); } in_stream.read(); StringBuilder result = new StringBuilder(); Token token = parent.next(); while (token != null && token.getType() != TokenType.EOF) { if (token.getType() == TokenType.CloseCurlyBrace) { break; } result.append(token.getValue()); token = parent.next(); if (token == null || token.getType().equals(TokenType.EOF) || token.getType().equals(TokenType.Unknown)) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Invalid token inside token string"); } } if (token == null || token.getType() != TokenType.CloseCurlyBrace) { throw new LexerException(start_line, start_col, "Unexpected end of stream when parsing token string literal"); } return factory.create(TokenType.LiteralUtf8, start_index, start_line, start_col, in_stream.getPosition(), in_stream.getLine(), in_stream.getCol(), result.toString()); } LexTokenString(TokenFactory factory, LexerStream in_stream, Lexer parent); Token read(); }
@Test public void line() throws Exception { String str = "a LexerStream ls = new LexerStream(new StringReader(str)); LexComment lex = new LexComment(new BaseTokenFactory(), ls); ls.read(); Token tok = lex.read(); assertEquals(TokenType.LineComment, tok.getType()); assertEquals(" assertEquals(1, tok.getLine()); assertEquals(2, tok.getCol()); assertEquals(1, tok.getEndLine()); assertEquals(7, tok.getEndCol()); ls.read(); tok = lex.read(); assertEquals(TokenType.LineComment, tok.getType()); assertEquals(" assertEquals(2, tok.getLine()); assertEquals(1, tok.getCol()); assertEquals(2, tok.getEndLine()); assertEquals(9, tok.getEndCol()); }
public Token read() throws IOException, LexerException { int start_index = in_stream.getPosition(); int start_line = in_stream.getLine(); int start_col = in_stream.getCol(); int next = in_stream.peek(); if (next == -1) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Unexpected end of input stream when parsing comment"); } if (next != '/') { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Comments must start with '/'"); } TokenType type; StringBuilder sb = new StringBuilder(); sb.append((char)in_stream.read()); next = in_stream.peek(); switch (next) { case '+': sb.append((char)in_stream.read()); if (in_stream.peek() == '+') { sb.append((char)in_stream.read()); type = TokenType.DocCommentNest; } else { type = TokenType.BlockCommentNest; } sb.append(read_multi_nested(in_stream)); break; case '*': sb.append((char)in_stream.read()); if (in_stream.peek() == '*') { sb.append((char)in_stream.read()); type = TokenType.DocComment; } else { type = TokenType.BlockComment; } sb.append(read_multi(in_stream)); break; case '/': sb.append((char)in_stream.read()); next = in_stream.peek(); if (next == '/') { sb.append((char)in_stream.read()); type = TokenType.DocLineComment; } else { type = TokenType.LineComment; } while (next != -1 && next != '\n') { sb.append((char)in_stream.read()); next = in_stream.peek(); } break; default: throw new LexerException(start_line, start_col, "Error while reading comment"); } return factory.create(type, start_index, start_line, start_col, in_stream.getPosition(), in_stream.getLine(), in_stream.getCol(), sb.toString()); }
LexComment { public Token read() throws IOException, LexerException { int start_index = in_stream.getPosition(); int start_line = in_stream.getLine(); int start_col = in_stream.getCol(); int next = in_stream.peek(); if (next == -1) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Unexpected end of input stream when parsing comment"); } if (next != '/') { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Comments must start with '/'"); } TokenType type; StringBuilder sb = new StringBuilder(); sb.append((char)in_stream.read()); next = in_stream.peek(); switch (next) { case '+': sb.append((char)in_stream.read()); if (in_stream.peek() == '+') { sb.append((char)in_stream.read()); type = TokenType.DocCommentNest; } else { type = TokenType.BlockCommentNest; } sb.append(read_multi_nested(in_stream)); break; case '*': sb.append((char)in_stream.read()); if (in_stream.peek() == '*') { sb.append((char)in_stream.read()); type = TokenType.DocComment; } else { type = TokenType.BlockComment; } sb.append(read_multi(in_stream)); break; case '/': sb.append((char)in_stream.read()); next = in_stream.peek(); if (next == '/') { sb.append((char)in_stream.read()); type = TokenType.DocLineComment; } else { type = TokenType.LineComment; } while (next != -1 && next != '\n') { sb.append((char)in_stream.read()); next = in_stream.peek(); } break; default: throw new LexerException(start_line, start_col, "Error while reading comment"); } return factory.create(type, start_index, start_line, start_col, in_stream.getPosition(), in_stream.getLine(), in_stream.getCol(), sb.toString()); } }
LexComment { public Token read() throws IOException, LexerException { int start_index = in_stream.getPosition(); int start_line = in_stream.getLine(); int start_col = in_stream.getCol(); int next = in_stream.peek(); if (next == -1) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Unexpected end of input stream when parsing comment"); } if (next != '/') { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Comments must start with '/'"); } TokenType type; StringBuilder sb = new StringBuilder(); sb.append((char)in_stream.read()); next = in_stream.peek(); switch (next) { case '+': sb.append((char)in_stream.read()); if (in_stream.peek() == '+') { sb.append((char)in_stream.read()); type = TokenType.DocCommentNest; } else { type = TokenType.BlockCommentNest; } sb.append(read_multi_nested(in_stream)); break; case '*': sb.append((char)in_stream.read()); if (in_stream.peek() == '*') { sb.append((char)in_stream.read()); type = TokenType.DocComment; } else { type = TokenType.BlockComment; } sb.append(read_multi(in_stream)); break; case '/': sb.append((char)in_stream.read()); next = in_stream.peek(); if (next == '/') { sb.append((char)in_stream.read()); type = TokenType.DocLineComment; } else { type = TokenType.LineComment; } while (next != -1 && next != '\n') { sb.append((char)in_stream.read()); next = in_stream.peek(); } break; default: throw new LexerException(start_line, start_col, "Error while reading comment"); } return factory.create(type, start_index, start_line, start_col, in_stream.getPosition(), in_stream.getLine(), in_stream.getCol(), sb.toString()); } LexComment(TokenFactory factory, LexerStream in_stream); }
LexComment { public Token read() throws IOException, LexerException { int start_index = in_stream.getPosition(); int start_line = in_stream.getLine(); int start_col = in_stream.getCol(); int next = in_stream.peek(); if (next == -1) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Unexpected end of input stream when parsing comment"); } if (next != '/') { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Comments must start with '/'"); } TokenType type; StringBuilder sb = new StringBuilder(); sb.append((char)in_stream.read()); next = in_stream.peek(); switch (next) { case '+': sb.append((char)in_stream.read()); if (in_stream.peek() == '+') { sb.append((char)in_stream.read()); type = TokenType.DocCommentNest; } else { type = TokenType.BlockCommentNest; } sb.append(read_multi_nested(in_stream)); break; case '*': sb.append((char)in_stream.read()); if (in_stream.peek() == '*') { sb.append((char)in_stream.read()); type = TokenType.DocComment; } else { type = TokenType.BlockComment; } sb.append(read_multi(in_stream)); break; case '/': sb.append((char)in_stream.read()); next = in_stream.peek(); if (next == '/') { sb.append((char)in_stream.read()); type = TokenType.DocLineComment; } else { type = TokenType.LineComment; } while (next != -1 && next != '\n') { sb.append((char)in_stream.read()); next = in_stream.peek(); } break; default: throw new LexerException(start_line, start_col, "Error while reading comment"); } return factory.create(type, start_index, start_line, start_col, in_stream.getPosition(), in_stream.getLine(), in_stream.getCol(), sb.toString()); } LexComment(TokenFactory factory, LexerStream in_stream); Token read(); }
LexComment { public Token read() throws IOException, LexerException { int start_index = in_stream.getPosition(); int start_line = in_stream.getLine(); int start_col = in_stream.getCol(); int next = in_stream.peek(); if (next == -1) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Unexpected end of input stream when parsing comment"); } if (next != '/') { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Comments must start with '/'"); } TokenType type; StringBuilder sb = new StringBuilder(); sb.append((char)in_stream.read()); next = in_stream.peek(); switch (next) { case '+': sb.append((char)in_stream.read()); if (in_stream.peek() == '+') { sb.append((char)in_stream.read()); type = TokenType.DocCommentNest; } else { type = TokenType.BlockCommentNest; } sb.append(read_multi_nested(in_stream)); break; case '*': sb.append((char)in_stream.read()); if (in_stream.peek() == '*') { sb.append((char)in_stream.read()); type = TokenType.DocComment; } else { type = TokenType.BlockComment; } sb.append(read_multi(in_stream)); break; case '/': sb.append((char)in_stream.read()); next = in_stream.peek(); if (next == '/') { sb.append((char)in_stream.read()); type = TokenType.DocLineComment; } else { type = TokenType.LineComment; } while (next != -1 && next != '\n') { sb.append((char)in_stream.read()); next = in_stream.peek(); } break; default: throw new LexerException(start_line, start_col, "Error while reading comment"); } return factory.create(type, start_index, start_line, start_col, in_stream.getPosition(), in_stream.getLine(), in_stream.getCol(), sb.toString()); } LexComment(TokenFactory factory, LexerStream in_stream); Token read(); }
@Test public void block() throws Exception { String str = "a\ncde"; LexerStream ls = new LexerStream(new StringReader(str)); LexComment lex = new LexComment(new BaseTokenFactory(), ls); ls.read(); Token tok = lex.read(); assertEquals(TokenType.BlockComment, tok.getType()); assertEquals("", tok.getValue()); assertEquals(1, tok.getLine()); assertEquals(2, tok.getCol()); assertEquals(4, tok.getEndLine()); assertEquals(3, tok.getEndCol()); }
public Token read() throws IOException, LexerException { int start_index = in_stream.getPosition(); int start_line = in_stream.getLine(); int start_col = in_stream.getCol(); int next = in_stream.peek(); if (next == -1) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Unexpected end of input stream when parsing comment"); } if (next != '/') { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Comments must start with '/'"); } TokenType type; StringBuilder sb = new StringBuilder(); sb.append((char)in_stream.read()); next = in_stream.peek(); switch (next) { case '+': sb.append((char)in_stream.read()); if (in_stream.peek() == '+') { sb.append((char)in_stream.read()); type = TokenType.DocCommentNest; } else { type = TokenType.BlockCommentNest; } sb.append(read_multi_nested(in_stream)); break; case '*': sb.append((char)in_stream.read()); if (in_stream.peek() == '*') { sb.append((char)in_stream.read()); type = TokenType.DocComment; } else { type = TokenType.BlockComment; } sb.append(read_multi(in_stream)); break; case '/': sb.append((char)in_stream.read()); next = in_stream.peek(); if (next == '/') { sb.append((char)in_stream.read()); type = TokenType.DocLineComment; } else { type = TokenType.LineComment; } while (next != -1 && next != '\n') { sb.append((char)in_stream.read()); next = in_stream.peek(); } break; default: throw new LexerException(start_line, start_col, "Error while reading comment"); } return factory.create(type, start_index, start_line, start_col, in_stream.getPosition(), in_stream.getLine(), in_stream.getCol(), sb.toString()); }
LexComment { public Token read() throws IOException, LexerException { int start_index = in_stream.getPosition(); int start_line = in_stream.getLine(); int start_col = in_stream.getCol(); int next = in_stream.peek(); if (next == -1) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Unexpected end of input stream when parsing comment"); } if (next != '/') { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Comments must start with '/'"); } TokenType type; StringBuilder sb = new StringBuilder(); sb.append((char)in_stream.read()); next = in_stream.peek(); switch (next) { case '+': sb.append((char)in_stream.read()); if (in_stream.peek() == '+') { sb.append((char)in_stream.read()); type = TokenType.DocCommentNest; } else { type = TokenType.BlockCommentNest; } sb.append(read_multi_nested(in_stream)); break; case '*': sb.append((char)in_stream.read()); if (in_stream.peek() == '*') { sb.append((char)in_stream.read()); type = TokenType.DocComment; } else { type = TokenType.BlockComment; } sb.append(read_multi(in_stream)); break; case '/': sb.append((char)in_stream.read()); next = in_stream.peek(); if (next == '/') { sb.append((char)in_stream.read()); type = TokenType.DocLineComment; } else { type = TokenType.LineComment; } while (next != -1 && next != '\n') { sb.append((char)in_stream.read()); next = in_stream.peek(); } break; default: throw new LexerException(start_line, start_col, "Error while reading comment"); } return factory.create(type, start_index, start_line, start_col, in_stream.getPosition(), in_stream.getLine(), in_stream.getCol(), sb.toString()); } }
LexComment { public Token read() throws IOException, LexerException { int start_index = in_stream.getPosition(); int start_line = in_stream.getLine(); int start_col = in_stream.getCol(); int next = in_stream.peek(); if (next == -1) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Unexpected end of input stream when parsing comment"); } if (next != '/') { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Comments must start with '/'"); } TokenType type; StringBuilder sb = new StringBuilder(); sb.append((char)in_stream.read()); next = in_stream.peek(); switch (next) { case '+': sb.append((char)in_stream.read()); if (in_stream.peek() == '+') { sb.append((char)in_stream.read()); type = TokenType.DocCommentNest; } else { type = TokenType.BlockCommentNest; } sb.append(read_multi_nested(in_stream)); break; case '*': sb.append((char)in_stream.read()); if (in_stream.peek() == '*') { sb.append((char)in_stream.read()); type = TokenType.DocComment; } else { type = TokenType.BlockComment; } sb.append(read_multi(in_stream)); break; case '/': sb.append((char)in_stream.read()); next = in_stream.peek(); if (next == '/') { sb.append((char)in_stream.read()); type = TokenType.DocLineComment; } else { type = TokenType.LineComment; } while (next != -1 && next != '\n') { sb.append((char)in_stream.read()); next = in_stream.peek(); } break; default: throw new LexerException(start_line, start_col, "Error while reading comment"); } return factory.create(type, start_index, start_line, start_col, in_stream.getPosition(), in_stream.getLine(), in_stream.getCol(), sb.toString()); } LexComment(TokenFactory factory, LexerStream in_stream); }
LexComment { public Token read() throws IOException, LexerException { int start_index = in_stream.getPosition(); int start_line = in_stream.getLine(); int start_col = in_stream.getCol(); int next = in_stream.peek(); if (next == -1) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Unexpected end of input stream when parsing comment"); } if (next != '/') { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Comments must start with '/'"); } TokenType type; StringBuilder sb = new StringBuilder(); sb.append((char)in_stream.read()); next = in_stream.peek(); switch (next) { case '+': sb.append((char)in_stream.read()); if (in_stream.peek() == '+') { sb.append((char)in_stream.read()); type = TokenType.DocCommentNest; } else { type = TokenType.BlockCommentNest; } sb.append(read_multi_nested(in_stream)); break; case '*': sb.append((char)in_stream.read()); if (in_stream.peek() == '*') { sb.append((char)in_stream.read()); type = TokenType.DocComment; } else { type = TokenType.BlockComment; } sb.append(read_multi(in_stream)); break; case '/': sb.append((char)in_stream.read()); next = in_stream.peek(); if (next == '/') { sb.append((char)in_stream.read()); type = TokenType.DocLineComment; } else { type = TokenType.LineComment; } while (next != -1 && next != '\n') { sb.append((char)in_stream.read()); next = in_stream.peek(); } break; default: throw new LexerException(start_line, start_col, "Error while reading comment"); } return factory.create(type, start_index, start_line, start_col, in_stream.getPosition(), in_stream.getLine(), in_stream.getCol(), sb.toString()); } LexComment(TokenFactory factory, LexerStream in_stream); Token read(); }
LexComment { public Token read() throws IOException, LexerException { int start_index = in_stream.getPosition(); int start_line = in_stream.getLine(); int start_col = in_stream.getCol(); int next = in_stream.peek(); if (next == -1) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Unexpected end of input stream when parsing comment"); } if (next != '/') { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Comments must start with '/'"); } TokenType type; StringBuilder sb = new StringBuilder(); sb.append((char)in_stream.read()); next = in_stream.peek(); switch (next) { case '+': sb.append((char)in_stream.read()); if (in_stream.peek() == '+') { sb.append((char)in_stream.read()); type = TokenType.DocCommentNest; } else { type = TokenType.BlockCommentNest; } sb.append(read_multi_nested(in_stream)); break; case '*': sb.append((char)in_stream.read()); if (in_stream.peek() == '*') { sb.append((char)in_stream.read()); type = TokenType.DocComment; } else { type = TokenType.BlockComment; } sb.append(read_multi(in_stream)); break; case '/': sb.append((char)in_stream.read()); next = in_stream.peek(); if (next == '/') { sb.append((char)in_stream.read()); type = TokenType.DocLineComment; } else { type = TokenType.LineComment; } while (next != -1 && next != '\n') { sb.append((char)in_stream.read()); next = in_stream.peek(); } break; default: throw new LexerException(start_line, start_col, "Error while reading comment"); } return factory.create(type, start_index, start_line, start_col, in_stream.getPosition(), in_stream.getLine(), in_stream.getCol(), sb.toString()); } LexComment(TokenFactory factory, LexerStream in_stream); Token read(); }
@Test public void block_inline() throws Exception { String str = "a=c"; LexerStream ls = new LexerStream(new StringReader(str)); LexComment lex = new LexComment(new BaseTokenFactory(), ls); ls.read(); ls.read(); Token tok = lex.read(); assertEquals(TokenType.BlockComment, tok.getType()); assertEquals("", tok.getValue()); assertEquals(1, tok.getLine()); assertEquals(3, tok.getCol()); assertEquals(1, tok.getEndLine()); assertEquals(8, tok.getEndCol()); }
public Token read() throws IOException, LexerException { int start_index = in_stream.getPosition(); int start_line = in_stream.getLine(); int start_col = in_stream.getCol(); int next = in_stream.peek(); if (next == -1) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Unexpected end of input stream when parsing comment"); } if (next != '/') { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Comments must start with '/'"); } TokenType type; StringBuilder sb = new StringBuilder(); sb.append((char)in_stream.read()); next = in_stream.peek(); switch (next) { case '+': sb.append((char)in_stream.read()); if (in_stream.peek() == '+') { sb.append((char)in_stream.read()); type = TokenType.DocCommentNest; } else { type = TokenType.BlockCommentNest; } sb.append(read_multi_nested(in_stream)); break; case '*': sb.append((char)in_stream.read()); if (in_stream.peek() == '*') { sb.append((char)in_stream.read()); type = TokenType.DocComment; } else { type = TokenType.BlockComment; } sb.append(read_multi(in_stream)); break; case '/': sb.append((char)in_stream.read()); next = in_stream.peek(); if (next == '/') { sb.append((char)in_stream.read()); type = TokenType.DocLineComment; } else { type = TokenType.LineComment; } while (next != -1 && next != '\n') { sb.append((char)in_stream.read()); next = in_stream.peek(); } break; default: throw new LexerException(start_line, start_col, "Error while reading comment"); } return factory.create(type, start_index, start_line, start_col, in_stream.getPosition(), in_stream.getLine(), in_stream.getCol(), sb.toString()); }
LexComment { public Token read() throws IOException, LexerException { int start_index = in_stream.getPosition(); int start_line = in_stream.getLine(); int start_col = in_stream.getCol(); int next = in_stream.peek(); if (next == -1) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Unexpected end of input stream when parsing comment"); } if (next != '/') { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Comments must start with '/'"); } TokenType type; StringBuilder sb = new StringBuilder(); sb.append((char)in_stream.read()); next = in_stream.peek(); switch (next) { case '+': sb.append((char)in_stream.read()); if (in_stream.peek() == '+') { sb.append((char)in_stream.read()); type = TokenType.DocCommentNest; } else { type = TokenType.BlockCommentNest; } sb.append(read_multi_nested(in_stream)); break; case '*': sb.append((char)in_stream.read()); if (in_stream.peek() == '*') { sb.append((char)in_stream.read()); type = TokenType.DocComment; } else { type = TokenType.BlockComment; } sb.append(read_multi(in_stream)); break; case '/': sb.append((char)in_stream.read()); next = in_stream.peek(); if (next == '/') { sb.append((char)in_stream.read()); type = TokenType.DocLineComment; } else { type = TokenType.LineComment; } while (next != -1 && next != '\n') { sb.append((char)in_stream.read()); next = in_stream.peek(); } break; default: throw new LexerException(start_line, start_col, "Error while reading comment"); } return factory.create(type, start_index, start_line, start_col, in_stream.getPosition(), in_stream.getLine(), in_stream.getCol(), sb.toString()); } }
LexComment { public Token read() throws IOException, LexerException { int start_index = in_stream.getPosition(); int start_line = in_stream.getLine(); int start_col = in_stream.getCol(); int next = in_stream.peek(); if (next == -1) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Unexpected end of input stream when parsing comment"); } if (next != '/') { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Comments must start with '/'"); } TokenType type; StringBuilder sb = new StringBuilder(); sb.append((char)in_stream.read()); next = in_stream.peek(); switch (next) { case '+': sb.append((char)in_stream.read()); if (in_stream.peek() == '+') { sb.append((char)in_stream.read()); type = TokenType.DocCommentNest; } else { type = TokenType.BlockCommentNest; } sb.append(read_multi_nested(in_stream)); break; case '*': sb.append((char)in_stream.read()); if (in_stream.peek() == '*') { sb.append((char)in_stream.read()); type = TokenType.DocComment; } else { type = TokenType.BlockComment; } sb.append(read_multi(in_stream)); break; case '/': sb.append((char)in_stream.read()); next = in_stream.peek(); if (next == '/') { sb.append((char)in_stream.read()); type = TokenType.DocLineComment; } else { type = TokenType.LineComment; } while (next != -1 && next != '\n') { sb.append((char)in_stream.read()); next = in_stream.peek(); } break; default: throw new LexerException(start_line, start_col, "Error while reading comment"); } return factory.create(type, start_index, start_line, start_col, in_stream.getPosition(), in_stream.getLine(), in_stream.getCol(), sb.toString()); } LexComment(TokenFactory factory, LexerStream in_stream); }
LexComment { public Token read() throws IOException, LexerException { int start_index = in_stream.getPosition(); int start_line = in_stream.getLine(); int start_col = in_stream.getCol(); int next = in_stream.peek(); if (next == -1) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Unexpected end of input stream when parsing comment"); } if (next != '/') { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Comments must start with '/'"); } TokenType type; StringBuilder sb = new StringBuilder(); sb.append((char)in_stream.read()); next = in_stream.peek(); switch (next) { case '+': sb.append((char)in_stream.read()); if (in_stream.peek() == '+') { sb.append((char)in_stream.read()); type = TokenType.DocCommentNest; } else { type = TokenType.BlockCommentNest; } sb.append(read_multi_nested(in_stream)); break; case '*': sb.append((char)in_stream.read()); if (in_stream.peek() == '*') { sb.append((char)in_stream.read()); type = TokenType.DocComment; } else { type = TokenType.BlockComment; } sb.append(read_multi(in_stream)); break; case '/': sb.append((char)in_stream.read()); next = in_stream.peek(); if (next == '/') { sb.append((char)in_stream.read()); type = TokenType.DocLineComment; } else { type = TokenType.LineComment; } while (next != -1 && next != '\n') { sb.append((char)in_stream.read()); next = in_stream.peek(); } break; default: throw new LexerException(start_line, start_col, "Error while reading comment"); } return factory.create(type, start_index, start_line, start_col, in_stream.getPosition(), in_stream.getLine(), in_stream.getCol(), sb.toString()); } LexComment(TokenFactory factory, LexerStream in_stream); Token read(); }
LexComment { public Token read() throws IOException, LexerException { int start_index = in_stream.getPosition(); int start_line = in_stream.getLine(); int start_col = in_stream.getCol(); int next = in_stream.peek(); if (next == -1) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Unexpected end of input stream when parsing comment"); } if (next != '/') { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Comments must start with '/'"); } TokenType type; StringBuilder sb = new StringBuilder(); sb.append((char)in_stream.read()); next = in_stream.peek(); switch (next) { case '+': sb.append((char)in_stream.read()); if (in_stream.peek() == '+') { sb.append((char)in_stream.read()); type = TokenType.DocCommentNest; } else { type = TokenType.BlockCommentNest; } sb.append(read_multi_nested(in_stream)); break; case '*': sb.append((char)in_stream.read()); if (in_stream.peek() == '*') { sb.append((char)in_stream.read()); type = TokenType.DocComment; } else { type = TokenType.BlockComment; } sb.append(read_multi(in_stream)); break; case '/': sb.append((char)in_stream.read()); next = in_stream.peek(); if (next == '/') { sb.append((char)in_stream.read()); type = TokenType.DocLineComment; } else { type = TokenType.LineComment; } while (next != -1 && next != '\n') { sb.append((char)in_stream.read()); next = in_stream.peek(); } break; default: throw new LexerException(start_line, start_col, "Error while reading comment"); } return factory.create(type, start_index, start_line, start_col, in_stream.getPosition(), in_stream.getLine(), in_stream.getCol(), sb.toString()); } LexComment(TokenFactory factory, LexerStream in_stream); Token read(); }
@Test public void nest() throws Exception { String str = "a/+ /+b\n+/ /+c /+\nd\n +/ e+ LexerStream ls = new LexerStream(new StringReader(str)); LexComment lex = new LexComment(new BaseTokenFactory(), ls); ls.read(); Token tok = lex.read(); assertEquals(TokenType.BlockCommentNest, tok.getType()); assertEquals("/+ /+b\n" + "+/ /+c /+\n" + "d\n" + " +/ e+ assertEquals(1, tok.getLine()); assertEquals(2, tok.getCol()); assertEquals(4, tok.getEndLine()); assertEquals(12, tok.getEndCol()); }
public Token read() throws IOException, LexerException { int start_index = in_stream.getPosition(); int start_line = in_stream.getLine(); int start_col = in_stream.getCol(); int next = in_stream.peek(); if (next == -1) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Unexpected end of input stream when parsing comment"); } if (next != '/') { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Comments must start with '/'"); } TokenType type; StringBuilder sb = new StringBuilder(); sb.append((char)in_stream.read()); next = in_stream.peek(); switch (next) { case '+': sb.append((char)in_stream.read()); if (in_stream.peek() == '+') { sb.append((char)in_stream.read()); type = TokenType.DocCommentNest; } else { type = TokenType.BlockCommentNest; } sb.append(read_multi_nested(in_stream)); break; case '*': sb.append((char)in_stream.read()); if (in_stream.peek() == '*') { sb.append((char)in_stream.read()); type = TokenType.DocComment; } else { type = TokenType.BlockComment; } sb.append(read_multi(in_stream)); break; case '/': sb.append((char)in_stream.read()); next = in_stream.peek(); if (next == '/') { sb.append((char)in_stream.read()); type = TokenType.DocLineComment; } else { type = TokenType.LineComment; } while (next != -1 && next != '\n') { sb.append((char)in_stream.read()); next = in_stream.peek(); } break; default: throw new LexerException(start_line, start_col, "Error while reading comment"); } return factory.create(type, start_index, start_line, start_col, in_stream.getPosition(), in_stream.getLine(), in_stream.getCol(), sb.toString()); }
LexComment { public Token read() throws IOException, LexerException { int start_index = in_stream.getPosition(); int start_line = in_stream.getLine(); int start_col = in_stream.getCol(); int next = in_stream.peek(); if (next == -1) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Unexpected end of input stream when parsing comment"); } if (next != '/') { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Comments must start with '/'"); } TokenType type; StringBuilder sb = new StringBuilder(); sb.append((char)in_stream.read()); next = in_stream.peek(); switch (next) { case '+': sb.append((char)in_stream.read()); if (in_stream.peek() == '+') { sb.append((char)in_stream.read()); type = TokenType.DocCommentNest; } else { type = TokenType.BlockCommentNest; } sb.append(read_multi_nested(in_stream)); break; case '*': sb.append((char)in_stream.read()); if (in_stream.peek() == '*') { sb.append((char)in_stream.read()); type = TokenType.DocComment; } else { type = TokenType.BlockComment; } sb.append(read_multi(in_stream)); break; case '/': sb.append((char)in_stream.read()); next = in_stream.peek(); if (next == '/') { sb.append((char)in_stream.read()); type = TokenType.DocLineComment; } else { type = TokenType.LineComment; } while (next != -1 && next != '\n') { sb.append((char)in_stream.read()); next = in_stream.peek(); } break; default: throw new LexerException(start_line, start_col, "Error while reading comment"); } return factory.create(type, start_index, start_line, start_col, in_stream.getPosition(), in_stream.getLine(), in_stream.getCol(), sb.toString()); } }
LexComment { public Token read() throws IOException, LexerException { int start_index = in_stream.getPosition(); int start_line = in_stream.getLine(); int start_col = in_stream.getCol(); int next = in_stream.peek(); if (next == -1) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Unexpected end of input stream when parsing comment"); } if (next != '/') { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Comments must start with '/'"); } TokenType type; StringBuilder sb = new StringBuilder(); sb.append((char)in_stream.read()); next = in_stream.peek(); switch (next) { case '+': sb.append((char)in_stream.read()); if (in_stream.peek() == '+') { sb.append((char)in_stream.read()); type = TokenType.DocCommentNest; } else { type = TokenType.BlockCommentNest; } sb.append(read_multi_nested(in_stream)); break; case '*': sb.append((char)in_stream.read()); if (in_stream.peek() == '*') { sb.append((char)in_stream.read()); type = TokenType.DocComment; } else { type = TokenType.BlockComment; } sb.append(read_multi(in_stream)); break; case '/': sb.append((char)in_stream.read()); next = in_stream.peek(); if (next == '/') { sb.append((char)in_stream.read()); type = TokenType.DocLineComment; } else { type = TokenType.LineComment; } while (next != -1 && next != '\n') { sb.append((char)in_stream.read()); next = in_stream.peek(); } break; default: throw new LexerException(start_line, start_col, "Error while reading comment"); } return factory.create(type, start_index, start_line, start_col, in_stream.getPosition(), in_stream.getLine(), in_stream.getCol(), sb.toString()); } LexComment(TokenFactory factory, LexerStream in_stream); }
LexComment { public Token read() throws IOException, LexerException { int start_index = in_stream.getPosition(); int start_line = in_stream.getLine(); int start_col = in_stream.getCol(); int next = in_stream.peek(); if (next == -1) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Unexpected end of input stream when parsing comment"); } if (next != '/') { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Comments must start with '/'"); } TokenType type; StringBuilder sb = new StringBuilder(); sb.append((char)in_stream.read()); next = in_stream.peek(); switch (next) { case '+': sb.append((char)in_stream.read()); if (in_stream.peek() == '+') { sb.append((char)in_stream.read()); type = TokenType.DocCommentNest; } else { type = TokenType.BlockCommentNest; } sb.append(read_multi_nested(in_stream)); break; case '*': sb.append((char)in_stream.read()); if (in_stream.peek() == '*') { sb.append((char)in_stream.read()); type = TokenType.DocComment; } else { type = TokenType.BlockComment; } sb.append(read_multi(in_stream)); break; case '/': sb.append((char)in_stream.read()); next = in_stream.peek(); if (next == '/') { sb.append((char)in_stream.read()); type = TokenType.DocLineComment; } else { type = TokenType.LineComment; } while (next != -1 && next != '\n') { sb.append((char)in_stream.read()); next = in_stream.peek(); } break; default: throw new LexerException(start_line, start_col, "Error while reading comment"); } return factory.create(type, start_index, start_line, start_col, in_stream.getPosition(), in_stream.getLine(), in_stream.getCol(), sb.toString()); } LexComment(TokenFactory factory, LexerStream in_stream); Token read(); }
LexComment { public Token read() throws IOException, LexerException { int start_index = in_stream.getPosition(); int start_line = in_stream.getLine(); int start_col = in_stream.getCol(); int next = in_stream.peek(); if (next == -1) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Unexpected end of input stream when parsing comment"); } if (next != '/') { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Comments must start with '/'"); } TokenType type; StringBuilder sb = new StringBuilder(); sb.append((char)in_stream.read()); next = in_stream.peek(); switch (next) { case '+': sb.append((char)in_stream.read()); if (in_stream.peek() == '+') { sb.append((char)in_stream.read()); type = TokenType.DocCommentNest; } else { type = TokenType.BlockCommentNest; } sb.append(read_multi_nested(in_stream)); break; case '*': sb.append((char)in_stream.read()); if (in_stream.peek() == '*') { sb.append((char)in_stream.read()); type = TokenType.DocComment; } else { type = TokenType.BlockComment; } sb.append(read_multi(in_stream)); break; case '/': sb.append((char)in_stream.read()); next = in_stream.peek(); if (next == '/') { sb.append((char)in_stream.read()); type = TokenType.DocLineComment; } else { type = TokenType.LineComment; } while (next != -1 && next != '\n') { sb.append((char)in_stream.read()); next = in_stream.peek(); } break; default: throw new LexerException(start_line, start_col, "Error while reading comment"); } return factory.create(type, start_index, start_line, start_col, in_stream.getPosition(), in_stream.getLine(), in_stream.getCol(), sb.toString()); } LexComment(TokenFactory factory, LexerStream in_stream); Token read(); }
@Test public void nest_inline() throws Exception { String str = "a = /+ "a = /+ \"+/\" +/ 1\";\n" + "a = /+ 3;"; LexerStream ls = new LexerStream(new StringReader(str)); LexComment lex = new LexComment(new BaseTokenFactory(), ls); ls.read(); ls.read(); ls.read(); ls.read(); Token tok = lex.read(); assertEquals(TokenType.BlockCommentNest, tok.getType()); assertEquals("/+ assertEquals(1, tok.getLine()); assertEquals(5, tok.getCol()); assertEquals(1, tok.getEndLine()); assertEquals(13, tok.getEndCol()); ls.readLine(); ls.read(); ls.read(); ls.read(); ls.read(); tok = lex.read(); assertEquals(TokenType.BlockCommentNest, tok.getType()); assertEquals("/+ \"+/", tok.getValue()); assertEquals(2, tok.getLine()); assertEquals(5, tok.getCol()); assertEquals(2, tok.getEndLine()); assertEquals(11, tok.getEndCol()); ls.readLine(); ls.read(); ls.read(); ls.read(); ls.read(); tok = lex.read(); assertEquals(TokenType.BlockCommentNest, tok.getType()); assertEquals("/+ /* +/", tok.getValue()); assertEquals(3, tok.getLine()); assertEquals(5, tok.getCol()); assertEquals(3, tok.getEndLine()); assertEquals(13, tok.getEndCol()); }
public Token read() throws IOException, LexerException { int start_index = in_stream.getPosition(); int start_line = in_stream.getLine(); int start_col = in_stream.getCol(); int next = in_stream.peek(); if (next == -1) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Unexpected end of input stream when parsing comment"); } if (next != '/') { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Comments must start with '/'"); } TokenType type; StringBuilder sb = new StringBuilder(); sb.append((char)in_stream.read()); next = in_stream.peek(); switch (next) { case '+': sb.append((char)in_stream.read()); if (in_stream.peek() == '+') { sb.append((char)in_stream.read()); type = TokenType.DocCommentNest; } else { type = TokenType.BlockCommentNest; } sb.append(read_multi_nested(in_stream)); break; case '*': sb.append((char)in_stream.read()); if (in_stream.peek() == '*') { sb.append((char)in_stream.read()); type = TokenType.DocComment; } else { type = TokenType.BlockComment; } sb.append(read_multi(in_stream)); break; case '/': sb.append((char)in_stream.read()); next = in_stream.peek(); if (next == '/') { sb.append((char)in_stream.read()); type = TokenType.DocLineComment; } else { type = TokenType.LineComment; } while (next != -1 && next != '\n') { sb.append((char)in_stream.read()); next = in_stream.peek(); } break; default: throw new LexerException(start_line, start_col, "Error while reading comment"); } return factory.create(type, start_index, start_line, start_col, in_stream.getPosition(), in_stream.getLine(), in_stream.getCol(), sb.toString()); }
LexComment { public Token read() throws IOException, LexerException { int start_index = in_stream.getPosition(); int start_line = in_stream.getLine(); int start_col = in_stream.getCol(); int next = in_stream.peek(); if (next == -1) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Unexpected end of input stream when parsing comment"); } if (next != '/') { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Comments must start with '/'"); } TokenType type; StringBuilder sb = new StringBuilder(); sb.append((char)in_stream.read()); next = in_stream.peek(); switch (next) { case '+': sb.append((char)in_stream.read()); if (in_stream.peek() == '+') { sb.append((char)in_stream.read()); type = TokenType.DocCommentNest; } else { type = TokenType.BlockCommentNest; } sb.append(read_multi_nested(in_stream)); break; case '*': sb.append((char)in_stream.read()); if (in_stream.peek() == '*') { sb.append((char)in_stream.read()); type = TokenType.DocComment; } else { type = TokenType.BlockComment; } sb.append(read_multi(in_stream)); break; case '/': sb.append((char)in_stream.read()); next = in_stream.peek(); if (next == '/') { sb.append((char)in_stream.read()); type = TokenType.DocLineComment; } else { type = TokenType.LineComment; } while (next != -1 && next != '\n') { sb.append((char)in_stream.read()); next = in_stream.peek(); } break; default: throw new LexerException(start_line, start_col, "Error while reading comment"); } return factory.create(type, start_index, start_line, start_col, in_stream.getPosition(), in_stream.getLine(), in_stream.getCol(), sb.toString()); } }
LexComment { public Token read() throws IOException, LexerException { int start_index = in_stream.getPosition(); int start_line = in_stream.getLine(); int start_col = in_stream.getCol(); int next = in_stream.peek(); if (next == -1) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Unexpected end of input stream when parsing comment"); } if (next != '/') { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Comments must start with '/'"); } TokenType type; StringBuilder sb = new StringBuilder(); sb.append((char)in_stream.read()); next = in_stream.peek(); switch (next) { case '+': sb.append((char)in_stream.read()); if (in_stream.peek() == '+') { sb.append((char)in_stream.read()); type = TokenType.DocCommentNest; } else { type = TokenType.BlockCommentNest; } sb.append(read_multi_nested(in_stream)); break; case '*': sb.append((char)in_stream.read()); if (in_stream.peek() == '*') { sb.append((char)in_stream.read()); type = TokenType.DocComment; } else { type = TokenType.BlockComment; } sb.append(read_multi(in_stream)); break; case '/': sb.append((char)in_stream.read()); next = in_stream.peek(); if (next == '/') { sb.append((char)in_stream.read()); type = TokenType.DocLineComment; } else { type = TokenType.LineComment; } while (next != -1 && next != '\n') { sb.append((char)in_stream.read()); next = in_stream.peek(); } break; default: throw new LexerException(start_line, start_col, "Error while reading comment"); } return factory.create(type, start_index, start_line, start_col, in_stream.getPosition(), in_stream.getLine(), in_stream.getCol(), sb.toString()); } LexComment(TokenFactory factory, LexerStream in_stream); }
LexComment { public Token read() throws IOException, LexerException { int start_index = in_stream.getPosition(); int start_line = in_stream.getLine(); int start_col = in_stream.getCol(); int next = in_stream.peek(); if (next == -1) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Unexpected end of input stream when parsing comment"); } if (next != '/') { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Comments must start with '/'"); } TokenType type; StringBuilder sb = new StringBuilder(); sb.append((char)in_stream.read()); next = in_stream.peek(); switch (next) { case '+': sb.append((char)in_stream.read()); if (in_stream.peek() == '+') { sb.append((char)in_stream.read()); type = TokenType.DocCommentNest; } else { type = TokenType.BlockCommentNest; } sb.append(read_multi_nested(in_stream)); break; case '*': sb.append((char)in_stream.read()); if (in_stream.peek() == '*') { sb.append((char)in_stream.read()); type = TokenType.DocComment; } else { type = TokenType.BlockComment; } sb.append(read_multi(in_stream)); break; case '/': sb.append((char)in_stream.read()); next = in_stream.peek(); if (next == '/') { sb.append((char)in_stream.read()); type = TokenType.DocLineComment; } else { type = TokenType.LineComment; } while (next != -1 && next != '\n') { sb.append((char)in_stream.read()); next = in_stream.peek(); } break; default: throw new LexerException(start_line, start_col, "Error while reading comment"); } return factory.create(type, start_index, start_line, start_col, in_stream.getPosition(), in_stream.getLine(), in_stream.getCol(), sb.toString()); } LexComment(TokenFactory factory, LexerStream in_stream); Token read(); }
LexComment { public Token read() throws IOException, LexerException { int start_index = in_stream.getPosition(); int start_line = in_stream.getLine(); int start_col = in_stream.getCol(); int next = in_stream.peek(); if (next == -1) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Unexpected end of input stream when parsing comment"); } if (next != '/') { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Comments must start with '/'"); } TokenType type; StringBuilder sb = new StringBuilder(); sb.append((char)in_stream.read()); next = in_stream.peek(); switch (next) { case '+': sb.append((char)in_stream.read()); if (in_stream.peek() == '+') { sb.append((char)in_stream.read()); type = TokenType.DocCommentNest; } else { type = TokenType.BlockCommentNest; } sb.append(read_multi_nested(in_stream)); break; case '*': sb.append((char)in_stream.read()); if (in_stream.peek() == '*') { sb.append((char)in_stream.read()); type = TokenType.DocComment; } else { type = TokenType.BlockComment; } sb.append(read_multi(in_stream)); break; case '/': sb.append((char)in_stream.read()); next = in_stream.peek(); if (next == '/') { sb.append((char)in_stream.read()); type = TokenType.DocLineComment; } else { type = TokenType.LineComment; } while (next != -1 && next != '\n') { sb.append((char)in_stream.read()); next = in_stream.peek(); } break; default: throw new LexerException(start_line, start_col, "Error while reading comment"); } return factory.create(type, start_index, start_line, start_col, in_stream.getPosition(), in_stream.getLine(), in_stream.getCol(), sb.toString()); } LexComment(TokenFactory factory, LexerStream in_stream); Token read(); }
@Test public void escape() throws Exception { String str = "\\'" + "\\\"" + "\\?" + "\\\\" + "\\a" + "\\b" + "\\f" + "\\n" + "\\r" + "\\t" + "\\v" + "\\0" + ""; LexerStream ls = new LexerStream(new StringReader(str)); assertEquals('\'', (char)LexEscape.read(ls)); assertEquals('"', (char)LexEscape.read(ls)); assertEquals('?', (char)LexEscape.read(ls)); assertEquals('\\', (char)LexEscape.read(ls)); assertEquals(0x07, LexEscape.read(ls)); assertEquals('\b', (char)LexEscape.read(ls)); assertEquals('\f', (char)LexEscape.read(ls)); assertEquals('\n', (char)LexEscape.read(ls)); assertEquals('\r', (char)LexEscape.read(ls)); assertEquals('\t', (char)LexEscape.read(ls)); assertEquals(0x0B, LexEscape.read(ls)); assertEquals(0x00, LexEscape.read(ls)); }
public static int read(final LexerStream in_stream) throws IOException, LexerException { int next = in_stream.peek(); if (next == -1) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Unexpected end of input stream when inside escape sequence"); } if (next != '\\') { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Not an escape sequence"); } int result; in_stream.read(); int cur = in_stream.read(); next = in_stream.peek(); switch (cur) { case '\'': result = '\''; break; case '\"': result = '\"'; break; case '?': result = '?'; break; case '\\': result = '\\'; break; case '0': result = '\0'; break; case 'a': result = 0x07; break; case 'b': result = '\b'; break; case 'f': result = '\f'; break; case 'n': result = '\n'; break; case 'r': result = '\r'; break; case 't': result = '\t'; break; case 'v': result = 0x0B; break; case 'x': result = 0; for (int i = 0; i < 2; i++) { if (next == -1) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Unexpected end of stream"); } try { int n = Integer.parseInt(String.valueOf((char)next), 16); result = (16 * result) + n; } catch (NumberFormatException e) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Invalid character in literal: "+next, e); } in_stream.read(); next = in_stream.peek(); } break; case 'u': result = 0; for (int i = 0; i < 4; i++) { if (next == -1) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Unexpected end of stream"); } try { int n = Integer.parseInt(String.valueOf((char)next), 16); result = (16 * result) + n; } catch (NumberFormatException e) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Invalid character in literal: "+next, e); } in_stream.read(); next = in_stream.peek(); } break; case 'U': result = 0; for (int i = 0; i < 8; i++) { if (next == -1) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Unexpected end of stream"); } try { int n = Integer.parseInt(String.valueOf((char)next), 16); result = (16 * result) + n; } catch (NumberFormatException e) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Invalid character in literal: "+next, e); } in_stream.read(); next = in_stream.peek(); } break; case '&': StringBuilder entity = new StringBuilder(); entity.append((char)cur); while (true) { if (next == -1) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Unexpected end of stream"); } if (next == ';') { entity.append((char)in_stream.read()); break; } if (Character.isLetter(next)) { entity.append((char)in_stream.read()); } else { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Unexpected character found in named entity: "+next); } next = in_stream.peek(); } if (entity.length() < 1) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Empty named character entity encountered"); } String val = StringEscapeUtils.unescapeHtml4(entity.toString()); result = Character.codePointAt(val, 0); break; default: if (!is_oct(cur)) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Unrecognized escape sequence: "+next); } result = Integer.parseInt(String.valueOf((char)cur), 8); int cnt = 1; while (is_oct(next) && cnt < 3) { cnt++; try { int n = Integer.parseInt(String.valueOf((char)next), 8); result = (8 * result) + n; } catch (NumberFormatException e) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Invalid character in octal literal: "+next, e); } in_stream.read(); next = in_stream.peek(); } break; } return result; }
LexEscape { public static int read(final LexerStream in_stream) throws IOException, LexerException { int next = in_stream.peek(); if (next == -1) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Unexpected end of input stream when inside escape sequence"); } if (next != '\\') { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Not an escape sequence"); } int result; in_stream.read(); int cur = in_stream.read(); next = in_stream.peek(); switch (cur) { case '\'': result = '\''; break; case '\"': result = '\"'; break; case '?': result = '?'; break; case '\\': result = '\\'; break; case '0': result = '\0'; break; case 'a': result = 0x07; break; case 'b': result = '\b'; break; case 'f': result = '\f'; break; case 'n': result = '\n'; break; case 'r': result = '\r'; break; case 't': result = '\t'; break; case 'v': result = 0x0B; break; case 'x': result = 0; for (int i = 0; i < 2; i++) { if (next == -1) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Unexpected end of stream"); } try { int n = Integer.parseInt(String.valueOf((char)next), 16); result = (16 * result) + n; } catch (NumberFormatException e) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Invalid character in literal: "+next, e); } in_stream.read(); next = in_stream.peek(); } break; case 'u': result = 0; for (int i = 0; i < 4; i++) { if (next == -1) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Unexpected end of stream"); } try { int n = Integer.parseInt(String.valueOf((char)next), 16); result = (16 * result) + n; } catch (NumberFormatException e) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Invalid character in literal: "+next, e); } in_stream.read(); next = in_stream.peek(); } break; case 'U': result = 0; for (int i = 0; i < 8; i++) { if (next == -1) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Unexpected end of stream"); } try { int n = Integer.parseInt(String.valueOf((char)next), 16); result = (16 * result) + n; } catch (NumberFormatException e) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Invalid character in literal: "+next, e); } in_stream.read(); next = in_stream.peek(); } break; case '&': StringBuilder entity = new StringBuilder(); entity.append((char)cur); while (true) { if (next == -1) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Unexpected end of stream"); } if (next == ';') { entity.append((char)in_stream.read()); break; } if (Character.isLetter(next)) { entity.append((char)in_stream.read()); } else { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Unexpected character found in named entity: "+next); } next = in_stream.peek(); } if (entity.length() < 1) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Empty named character entity encountered"); } String val = StringEscapeUtils.unescapeHtml4(entity.toString()); result = Character.codePointAt(val, 0); break; default: if (!is_oct(cur)) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Unrecognized escape sequence: "+next); } result = Integer.parseInt(String.valueOf((char)cur), 8); int cnt = 1; while (is_oct(next) && cnt < 3) { cnt++; try { int n = Integer.parseInt(String.valueOf((char)next), 8); result = (8 * result) + n; } catch (NumberFormatException e) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Invalid character in octal literal: "+next, e); } in_stream.read(); next = in_stream.peek(); } break; } return result; } }
LexEscape { public static int read(final LexerStream in_stream) throws IOException, LexerException { int next = in_stream.peek(); if (next == -1) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Unexpected end of input stream when inside escape sequence"); } if (next != '\\') { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Not an escape sequence"); } int result; in_stream.read(); int cur = in_stream.read(); next = in_stream.peek(); switch (cur) { case '\'': result = '\''; break; case '\"': result = '\"'; break; case '?': result = '?'; break; case '\\': result = '\\'; break; case '0': result = '\0'; break; case 'a': result = 0x07; break; case 'b': result = '\b'; break; case 'f': result = '\f'; break; case 'n': result = '\n'; break; case 'r': result = '\r'; break; case 't': result = '\t'; break; case 'v': result = 0x0B; break; case 'x': result = 0; for (int i = 0; i < 2; i++) { if (next == -1) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Unexpected end of stream"); } try { int n = Integer.parseInt(String.valueOf((char)next), 16); result = (16 * result) + n; } catch (NumberFormatException e) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Invalid character in literal: "+next, e); } in_stream.read(); next = in_stream.peek(); } break; case 'u': result = 0; for (int i = 0; i < 4; i++) { if (next == -1) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Unexpected end of stream"); } try { int n = Integer.parseInt(String.valueOf((char)next), 16); result = (16 * result) + n; } catch (NumberFormatException e) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Invalid character in literal: "+next, e); } in_stream.read(); next = in_stream.peek(); } break; case 'U': result = 0; for (int i = 0; i < 8; i++) { if (next == -1) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Unexpected end of stream"); } try { int n = Integer.parseInt(String.valueOf((char)next), 16); result = (16 * result) + n; } catch (NumberFormatException e) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Invalid character in literal: "+next, e); } in_stream.read(); next = in_stream.peek(); } break; case '&': StringBuilder entity = new StringBuilder(); entity.append((char)cur); while (true) { if (next == -1) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Unexpected end of stream"); } if (next == ';') { entity.append((char)in_stream.read()); break; } if (Character.isLetter(next)) { entity.append((char)in_stream.read()); } else { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Unexpected character found in named entity: "+next); } next = in_stream.peek(); } if (entity.length() < 1) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Empty named character entity encountered"); } String val = StringEscapeUtils.unescapeHtml4(entity.toString()); result = Character.codePointAt(val, 0); break; default: if (!is_oct(cur)) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Unrecognized escape sequence: "+next); } result = Integer.parseInt(String.valueOf((char)cur), 8); int cnt = 1; while (is_oct(next) && cnt < 3) { cnt++; try { int n = Integer.parseInt(String.valueOf((char)next), 8); result = (8 * result) + n; } catch (NumberFormatException e) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Invalid character in octal literal: "+next, e); } in_stream.read(); next = in_stream.peek(); } break; } return result; } }
LexEscape { public static int read(final LexerStream in_stream) throws IOException, LexerException { int next = in_stream.peek(); if (next == -1) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Unexpected end of input stream when inside escape sequence"); } if (next != '\\') { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Not an escape sequence"); } int result; in_stream.read(); int cur = in_stream.read(); next = in_stream.peek(); switch (cur) { case '\'': result = '\''; break; case '\"': result = '\"'; break; case '?': result = '?'; break; case '\\': result = '\\'; break; case '0': result = '\0'; break; case 'a': result = 0x07; break; case 'b': result = '\b'; break; case 'f': result = '\f'; break; case 'n': result = '\n'; break; case 'r': result = '\r'; break; case 't': result = '\t'; break; case 'v': result = 0x0B; break; case 'x': result = 0; for (int i = 0; i < 2; i++) { if (next == -1) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Unexpected end of stream"); } try { int n = Integer.parseInt(String.valueOf((char)next), 16); result = (16 * result) + n; } catch (NumberFormatException e) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Invalid character in literal: "+next, e); } in_stream.read(); next = in_stream.peek(); } break; case 'u': result = 0; for (int i = 0; i < 4; i++) { if (next == -1) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Unexpected end of stream"); } try { int n = Integer.parseInt(String.valueOf((char)next), 16); result = (16 * result) + n; } catch (NumberFormatException e) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Invalid character in literal: "+next, e); } in_stream.read(); next = in_stream.peek(); } break; case 'U': result = 0; for (int i = 0; i < 8; i++) { if (next == -1) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Unexpected end of stream"); } try { int n = Integer.parseInt(String.valueOf((char)next), 16); result = (16 * result) + n; } catch (NumberFormatException e) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Invalid character in literal: "+next, e); } in_stream.read(); next = in_stream.peek(); } break; case '&': StringBuilder entity = new StringBuilder(); entity.append((char)cur); while (true) { if (next == -1) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Unexpected end of stream"); } if (next == ';') { entity.append((char)in_stream.read()); break; } if (Character.isLetter(next)) { entity.append((char)in_stream.read()); } else { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Unexpected character found in named entity: "+next); } next = in_stream.peek(); } if (entity.length() < 1) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Empty named character entity encountered"); } String val = StringEscapeUtils.unescapeHtml4(entity.toString()); result = Character.codePointAt(val, 0); break; default: if (!is_oct(cur)) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Unrecognized escape sequence: "+next); } result = Integer.parseInt(String.valueOf((char)cur), 8); int cnt = 1; while (is_oct(next) && cnt < 3) { cnt++; try { int n = Integer.parseInt(String.valueOf((char)next), 8); result = (8 * result) + n; } catch (NumberFormatException e) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Invalid character in octal literal: "+next, e); } in_stream.read(); next = in_stream.peek(); } break; } return result; } static int read(final LexerStream in_stream); }
LexEscape { public static int read(final LexerStream in_stream) throws IOException, LexerException { int next = in_stream.peek(); if (next == -1) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Unexpected end of input stream when inside escape sequence"); } if (next != '\\') { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Not an escape sequence"); } int result; in_stream.read(); int cur = in_stream.read(); next = in_stream.peek(); switch (cur) { case '\'': result = '\''; break; case '\"': result = '\"'; break; case '?': result = '?'; break; case '\\': result = '\\'; break; case '0': result = '\0'; break; case 'a': result = 0x07; break; case 'b': result = '\b'; break; case 'f': result = '\f'; break; case 'n': result = '\n'; break; case 'r': result = '\r'; break; case 't': result = '\t'; break; case 'v': result = 0x0B; break; case 'x': result = 0; for (int i = 0; i < 2; i++) { if (next == -1) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Unexpected end of stream"); } try { int n = Integer.parseInt(String.valueOf((char)next), 16); result = (16 * result) + n; } catch (NumberFormatException e) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Invalid character in literal: "+next, e); } in_stream.read(); next = in_stream.peek(); } break; case 'u': result = 0; for (int i = 0; i < 4; i++) { if (next == -1) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Unexpected end of stream"); } try { int n = Integer.parseInt(String.valueOf((char)next), 16); result = (16 * result) + n; } catch (NumberFormatException e) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Invalid character in literal: "+next, e); } in_stream.read(); next = in_stream.peek(); } break; case 'U': result = 0; for (int i = 0; i < 8; i++) { if (next == -1) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Unexpected end of stream"); } try { int n = Integer.parseInt(String.valueOf((char)next), 16); result = (16 * result) + n; } catch (NumberFormatException e) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Invalid character in literal: "+next, e); } in_stream.read(); next = in_stream.peek(); } break; case '&': StringBuilder entity = new StringBuilder(); entity.append((char)cur); while (true) { if (next == -1) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Unexpected end of stream"); } if (next == ';') { entity.append((char)in_stream.read()); break; } if (Character.isLetter(next)) { entity.append((char)in_stream.read()); } else { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Unexpected character found in named entity: "+next); } next = in_stream.peek(); } if (entity.length() < 1) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Empty named character entity encountered"); } String val = StringEscapeUtils.unescapeHtml4(entity.toString()); result = Character.codePointAt(val, 0); break; default: if (!is_oct(cur)) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Unrecognized escape sequence: "+next); } result = Integer.parseInt(String.valueOf((char)cur), 8); int cnt = 1; while (is_oct(next) && cnt < 3) { cnt++; try { int n = Integer.parseInt(String.valueOf((char)next), 8); result = (8 * result) + n; } catch (NumberFormatException e) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Invalid character in octal literal: "+next, e); } in_stream.read(); next = in_stream.peek(); } break; } return result; } static int read(final LexerStream in_stream); }
@Test public void oct() throws Exception { String str = "\\123 \\37 \\4"; LexerStream ls = new LexerStream(new StringReader(str)); assertEquals(83, LexEscape.read(ls)); ls.read(); assertEquals(31, LexEscape.read(ls)); ls.read(); assertEquals(4, LexEscape.read(ls)); }
public static int read(final LexerStream in_stream) throws IOException, LexerException { int next = in_stream.peek(); if (next == -1) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Unexpected end of input stream when inside escape sequence"); } if (next != '\\') { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Not an escape sequence"); } int result; in_stream.read(); int cur = in_stream.read(); next = in_stream.peek(); switch (cur) { case '\'': result = '\''; break; case '\"': result = '\"'; break; case '?': result = '?'; break; case '\\': result = '\\'; break; case '0': result = '\0'; break; case 'a': result = 0x07; break; case 'b': result = '\b'; break; case 'f': result = '\f'; break; case 'n': result = '\n'; break; case 'r': result = '\r'; break; case 't': result = '\t'; break; case 'v': result = 0x0B; break; case 'x': result = 0; for (int i = 0; i < 2; i++) { if (next == -1) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Unexpected end of stream"); } try { int n = Integer.parseInt(String.valueOf((char)next), 16); result = (16 * result) + n; } catch (NumberFormatException e) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Invalid character in literal: "+next, e); } in_stream.read(); next = in_stream.peek(); } break; case 'u': result = 0; for (int i = 0; i < 4; i++) { if (next == -1) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Unexpected end of stream"); } try { int n = Integer.parseInt(String.valueOf((char)next), 16); result = (16 * result) + n; } catch (NumberFormatException e) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Invalid character in literal: "+next, e); } in_stream.read(); next = in_stream.peek(); } break; case 'U': result = 0; for (int i = 0; i < 8; i++) { if (next == -1) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Unexpected end of stream"); } try { int n = Integer.parseInt(String.valueOf((char)next), 16); result = (16 * result) + n; } catch (NumberFormatException e) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Invalid character in literal: "+next, e); } in_stream.read(); next = in_stream.peek(); } break; case '&': StringBuilder entity = new StringBuilder(); entity.append((char)cur); while (true) { if (next == -1) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Unexpected end of stream"); } if (next == ';') { entity.append((char)in_stream.read()); break; } if (Character.isLetter(next)) { entity.append((char)in_stream.read()); } else { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Unexpected character found in named entity: "+next); } next = in_stream.peek(); } if (entity.length() < 1) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Empty named character entity encountered"); } String val = StringEscapeUtils.unescapeHtml4(entity.toString()); result = Character.codePointAt(val, 0); break; default: if (!is_oct(cur)) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Unrecognized escape sequence: "+next); } result = Integer.parseInt(String.valueOf((char)cur), 8); int cnt = 1; while (is_oct(next) && cnt < 3) { cnt++; try { int n = Integer.parseInt(String.valueOf((char)next), 8); result = (8 * result) + n; } catch (NumberFormatException e) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Invalid character in octal literal: "+next, e); } in_stream.read(); next = in_stream.peek(); } break; } return result; }
LexEscape { public static int read(final LexerStream in_stream) throws IOException, LexerException { int next = in_stream.peek(); if (next == -1) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Unexpected end of input stream when inside escape sequence"); } if (next != '\\') { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Not an escape sequence"); } int result; in_stream.read(); int cur = in_stream.read(); next = in_stream.peek(); switch (cur) { case '\'': result = '\''; break; case '\"': result = '\"'; break; case '?': result = '?'; break; case '\\': result = '\\'; break; case '0': result = '\0'; break; case 'a': result = 0x07; break; case 'b': result = '\b'; break; case 'f': result = '\f'; break; case 'n': result = '\n'; break; case 'r': result = '\r'; break; case 't': result = '\t'; break; case 'v': result = 0x0B; break; case 'x': result = 0; for (int i = 0; i < 2; i++) { if (next == -1) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Unexpected end of stream"); } try { int n = Integer.parseInt(String.valueOf((char)next), 16); result = (16 * result) + n; } catch (NumberFormatException e) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Invalid character in literal: "+next, e); } in_stream.read(); next = in_stream.peek(); } break; case 'u': result = 0; for (int i = 0; i < 4; i++) { if (next == -1) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Unexpected end of stream"); } try { int n = Integer.parseInt(String.valueOf((char)next), 16); result = (16 * result) + n; } catch (NumberFormatException e) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Invalid character in literal: "+next, e); } in_stream.read(); next = in_stream.peek(); } break; case 'U': result = 0; for (int i = 0; i < 8; i++) { if (next == -1) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Unexpected end of stream"); } try { int n = Integer.parseInt(String.valueOf((char)next), 16); result = (16 * result) + n; } catch (NumberFormatException e) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Invalid character in literal: "+next, e); } in_stream.read(); next = in_stream.peek(); } break; case '&': StringBuilder entity = new StringBuilder(); entity.append((char)cur); while (true) { if (next == -1) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Unexpected end of stream"); } if (next == ';') { entity.append((char)in_stream.read()); break; } if (Character.isLetter(next)) { entity.append((char)in_stream.read()); } else { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Unexpected character found in named entity: "+next); } next = in_stream.peek(); } if (entity.length() < 1) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Empty named character entity encountered"); } String val = StringEscapeUtils.unescapeHtml4(entity.toString()); result = Character.codePointAt(val, 0); break; default: if (!is_oct(cur)) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Unrecognized escape sequence: "+next); } result = Integer.parseInt(String.valueOf((char)cur), 8); int cnt = 1; while (is_oct(next) && cnt < 3) { cnt++; try { int n = Integer.parseInt(String.valueOf((char)next), 8); result = (8 * result) + n; } catch (NumberFormatException e) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Invalid character in octal literal: "+next, e); } in_stream.read(); next = in_stream.peek(); } break; } return result; } }
LexEscape { public static int read(final LexerStream in_stream) throws IOException, LexerException { int next = in_stream.peek(); if (next == -1) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Unexpected end of input stream when inside escape sequence"); } if (next != '\\') { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Not an escape sequence"); } int result; in_stream.read(); int cur = in_stream.read(); next = in_stream.peek(); switch (cur) { case '\'': result = '\''; break; case '\"': result = '\"'; break; case '?': result = '?'; break; case '\\': result = '\\'; break; case '0': result = '\0'; break; case 'a': result = 0x07; break; case 'b': result = '\b'; break; case 'f': result = '\f'; break; case 'n': result = '\n'; break; case 'r': result = '\r'; break; case 't': result = '\t'; break; case 'v': result = 0x0B; break; case 'x': result = 0; for (int i = 0; i < 2; i++) { if (next == -1) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Unexpected end of stream"); } try { int n = Integer.parseInt(String.valueOf((char)next), 16); result = (16 * result) + n; } catch (NumberFormatException e) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Invalid character in literal: "+next, e); } in_stream.read(); next = in_stream.peek(); } break; case 'u': result = 0; for (int i = 0; i < 4; i++) { if (next == -1) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Unexpected end of stream"); } try { int n = Integer.parseInt(String.valueOf((char)next), 16); result = (16 * result) + n; } catch (NumberFormatException e) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Invalid character in literal: "+next, e); } in_stream.read(); next = in_stream.peek(); } break; case 'U': result = 0; for (int i = 0; i < 8; i++) { if (next == -1) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Unexpected end of stream"); } try { int n = Integer.parseInt(String.valueOf((char)next), 16); result = (16 * result) + n; } catch (NumberFormatException e) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Invalid character in literal: "+next, e); } in_stream.read(); next = in_stream.peek(); } break; case '&': StringBuilder entity = new StringBuilder(); entity.append((char)cur); while (true) { if (next == -1) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Unexpected end of stream"); } if (next == ';') { entity.append((char)in_stream.read()); break; } if (Character.isLetter(next)) { entity.append((char)in_stream.read()); } else { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Unexpected character found in named entity: "+next); } next = in_stream.peek(); } if (entity.length() < 1) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Empty named character entity encountered"); } String val = StringEscapeUtils.unescapeHtml4(entity.toString()); result = Character.codePointAt(val, 0); break; default: if (!is_oct(cur)) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Unrecognized escape sequence: "+next); } result = Integer.parseInt(String.valueOf((char)cur), 8); int cnt = 1; while (is_oct(next) && cnt < 3) { cnt++; try { int n = Integer.parseInt(String.valueOf((char)next), 8); result = (8 * result) + n; } catch (NumberFormatException e) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Invalid character in octal literal: "+next, e); } in_stream.read(); next = in_stream.peek(); } break; } return result; } }
LexEscape { public static int read(final LexerStream in_stream) throws IOException, LexerException { int next = in_stream.peek(); if (next == -1) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Unexpected end of input stream when inside escape sequence"); } if (next != '\\') { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Not an escape sequence"); } int result; in_stream.read(); int cur = in_stream.read(); next = in_stream.peek(); switch (cur) { case '\'': result = '\''; break; case '\"': result = '\"'; break; case '?': result = '?'; break; case '\\': result = '\\'; break; case '0': result = '\0'; break; case 'a': result = 0x07; break; case 'b': result = '\b'; break; case 'f': result = '\f'; break; case 'n': result = '\n'; break; case 'r': result = '\r'; break; case 't': result = '\t'; break; case 'v': result = 0x0B; break; case 'x': result = 0; for (int i = 0; i < 2; i++) { if (next == -1) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Unexpected end of stream"); } try { int n = Integer.parseInt(String.valueOf((char)next), 16); result = (16 * result) + n; } catch (NumberFormatException e) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Invalid character in literal: "+next, e); } in_stream.read(); next = in_stream.peek(); } break; case 'u': result = 0; for (int i = 0; i < 4; i++) { if (next == -1) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Unexpected end of stream"); } try { int n = Integer.parseInt(String.valueOf((char)next), 16); result = (16 * result) + n; } catch (NumberFormatException e) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Invalid character in literal: "+next, e); } in_stream.read(); next = in_stream.peek(); } break; case 'U': result = 0; for (int i = 0; i < 8; i++) { if (next == -1) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Unexpected end of stream"); } try { int n = Integer.parseInt(String.valueOf((char)next), 16); result = (16 * result) + n; } catch (NumberFormatException e) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Invalid character in literal: "+next, e); } in_stream.read(); next = in_stream.peek(); } break; case '&': StringBuilder entity = new StringBuilder(); entity.append((char)cur); while (true) { if (next == -1) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Unexpected end of stream"); } if (next == ';') { entity.append((char)in_stream.read()); break; } if (Character.isLetter(next)) { entity.append((char)in_stream.read()); } else { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Unexpected character found in named entity: "+next); } next = in_stream.peek(); } if (entity.length() < 1) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Empty named character entity encountered"); } String val = StringEscapeUtils.unescapeHtml4(entity.toString()); result = Character.codePointAt(val, 0); break; default: if (!is_oct(cur)) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Unrecognized escape sequence: "+next); } result = Integer.parseInt(String.valueOf((char)cur), 8); int cnt = 1; while (is_oct(next) && cnt < 3) { cnt++; try { int n = Integer.parseInt(String.valueOf((char)next), 8); result = (8 * result) + n; } catch (NumberFormatException e) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Invalid character in octal literal: "+next, e); } in_stream.read(); next = in_stream.peek(); } break; } return result; } static int read(final LexerStream in_stream); }
LexEscape { public static int read(final LexerStream in_stream) throws IOException, LexerException { int next = in_stream.peek(); if (next == -1) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Unexpected end of input stream when inside escape sequence"); } if (next != '\\') { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Not an escape sequence"); } int result; in_stream.read(); int cur = in_stream.read(); next = in_stream.peek(); switch (cur) { case '\'': result = '\''; break; case '\"': result = '\"'; break; case '?': result = '?'; break; case '\\': result = '\\'; break; case '0': result = '\0'; break; case 'a': result = 0x07; break; case 'b': result = '\b'; break; case 'f': result = '\f'; break; case 'n': result = '\n'; break; case 'r': result = '\r'; break; case 't': result = '\t'; break; case 'v': result = 0x0B; break; case 'x': result = 0; for (int i = 0; i < 2; i++) { if (next == -1) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Unexpected end of stream"); } try { int n = Integer.parseInt(String.valueOf((char)next), 16); result = (16 * result) + n; } catch (NumberFormatException e) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Invalid character in literal: "+next, e); } in_stream.read(); next = in_stream.peek(); } break; case 'u': result = 0; for (int i = 0; i < 4; i++) { if (next == -1) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Unexpected end of stream"); } try { int n = Integer.parseInt(String.valueOf((char)next), 16); result = (16 * result) + n; } catch (NumberFormatException e) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Invalid character in literal: "+next, e); } in_stream.read(); next = in_stream.peek(); } break; case 'U': result = 0; for (int i = 0; i < 8; i++) { if (next == -1) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Unexpected end of stream"); } try { int n = Integer.parseInt(String.valueOf((char)next), 16); result = (16 * result) + n; } catch (NumberFormatException e) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Invalid character in literal: "+next, e); } in_stream.read(); next = in_stream.peek(); } break; case '&': StringBuilder entity = new StringBuilder(); entity.append((char)cur); while (true) { if (next == -1) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Unexpected end of stream"); } if (next == ';') { entity.append((char)in_stream.read()); break; } if (Character.isLetter(next)) { entity.append((char)in_stream.read()); } else { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Unexpected character found in named entity: "+next); } next = in_stream.peek(); } if (entity.length() < 1) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Empty named character entity encountered"); } String val = StringEscapeUtils.unescapeHtml4(entity.toString()); result = Character.codePointAt(val, 0); break; default: if (!is_oct(cur)) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Unrecognized escape sequence: "+next); } result = Integer.parseInt(String.valueOf((char)cur), 8); int cnt = 1; while (is_oct(next) && cnt < 3) { cnt++; try { int n = Integer.parseInt(String.valueOf((char)next), 8); result = (8 * result) + n; } catch (NumberFormatException e) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Invalid character in octal literal: "+next, e); } in_stream.read(); next = in_stream.peek(); } break; } return result; } static int read(final LexerStream in_stream); }
@Test public void hex() throws Exception { String str = "\\xA1 \\uF0a5 \\UdeaDbEef"; LexerStream ls = new LexerStream(new StringReader(str)); assertEquals(0xa1, LexEscape.read(ls)); ls.read(); assertEquals(0xf0a5, LexEscape.read(ls)); ls.read(); assertEquals(0xdeadbeef, LexEscape.read(ls)); }
public static int read(final LexerStream in_stream) throws IOException, LexerException { int next = in_stream.peek(); if (next == -1) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Unexpected end of input stream when inside escape sequence"); } if (next != '\\') { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Not an escape sequence"); } int result; in_stream.read(); int cur = in_stream.read(); next = in_stream.peek(); switch (cur) { case '\'': result = '\''; break; case '\"': result = '\"'; break; case '?': result = '?'; break; case '\\': result = '\\'; break; case '0': result = '\0'; break; case 'a': result = 0x07; break; case 'b': result = '\b'; break; case 'f': result = '\f'; break; case 'n': result = '\n'; break; case 'r': result = '\r'; break; case 't': result = '\t'; break; case 'v': result = 0x0B; break; case 'x': result = 0; for (int i = 0; i < 2; i++) { if (next == -1) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Unexpected end of stream"); } try { int n = Integer.parseInt(String.valueOf((char)next), 16); result = (16 * result) + n; } catch (NumberFormatException e) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Invalid character in literal: "+next, e); } in_stream.read(); next = in_stream.peek(); } break; case 'u': result = 0; for (int i = 0; i < 4; i++) { if (next == -1) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Unexpected end of stream"); } try { int n = Integer.parseInt(String.valueOf((char)next), 16); result = (16 * result) + n; } catch (NumberFormatException e) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Invalid character in literal: "+next, e); } in_stream.read(); next = in_stream.peek(); } break; case 'U': result = 0; for (int i = 0; i < 8; i++) { if (next == -1) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Unexpected end of stream"); } try { int n = Integer.parseInt(String.valueOf((char)next), 16); result = (16 * result) + n; } catch (NumberFormatException e) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Invalid character in literal: "+next, e); } in_stream.read(); next = in_stream.peek(); } break; case '&': StringBuilder entity = new StringBuilder(); entity.append((char)cur); while (true) { if (next == -1) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Unexpected end of stream"); } if (next == ';') { entity.append((char)in_stream.read()); break; } if (Character.isLetter(next)) { entity.append((char)in_stream.read()); } else { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Unexpected character found in named entity: "+next); } next = in_stream.peek(); } if (entity.length() < 1) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Empty named character entity encountered"); } String val = StringEscapeUtils.unescapeHtml4(entity.toString()); result = Character.codePointAt(val, 0); break; default: if (!is_oct(cur)) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Unrecognized escape sequence: "+next); } result = Integer.parseInt(String.valueOf((char)cur), 8); int cnt = 1; while (is_oct(next) && cnt < 3) { cnt++; try { int n = Integer.parseInt(String.valueOf((char)next), 8); result = (8 * result) + n; } catch (NumberFormatException e) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Invalid character in octal literal: "+next, e); } in_stream.read(); next = in_stream.peek(); } break; } return result; }
LexEscape { public static int read(final LexerStream in_stream) throws IOException, LexerException { int next = in_stream.peek(); if (next == -1) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Unexpected end of input stream when inside escape sequence"); } if (next != '\\') { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Not an escape sequence"); } int result; in_stream.read(); int cur = in_stream.read(); next = in_stream.peek(); switch (cur) { case '\'': result = '\''; break; case '\"': result = '\"'; break; case '?': result = '?'; break; case '\\': result = '\\'; break; case '0': result = '\0'; break; case 'a': result = 0x07; break; case 'b': result = '\b'; break; case 'f': result = '\f'; break; case 'n': result = '\n'; break; case 'r': result = '\r'; break; case 't': result = '\t'; break; case 'v': result = 0x0B; break; case 'x': result = 0; for (int i = 0; i < 2; i++) { if (next == -1) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Unexpected end of stream"); } try { int n = Integer.parseInt(String.valueOf((char)next), 16); result = (16 * result) + n; } catch (NumberFormatException e) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Invalid character in literal: "+next, e); } in_stream.read(); next = in_stream.peek(); } break; case 'u': result = 0; for (int i = 0; i < 4; i++) { if (next == -1) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Unexpected end of stream"); } try { int n = Integer.parseInt(String.valueOf((char)next), 16); result = (16 * result) + n; } catch (NumberFormatException e) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Invalid character in literal: "+next, e); } in_stream.read(); next = in_stream.peek(); } break; case 'U': result = 0; for (int i = 0; i < 8; i++) { if (next == -1) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Unexpected end of stream"); } try { int n = Integer.parseInt(String.valueOf((char)next), 16); result = (16 * result) + n; } catch (NumberFormatException e) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Invalid character in literal: "+next, e); } in_stream.read(); next = in_stream.peek(); } break; case '&': StringBuilder entity = new StringBuilder(); entity.append((char)cur); while (true) { if (next == -1) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Unexpected end of stream"); } if (next == ';') { entity.append((char)in_stream.read()); break; } if (Character.isLetter(next)) { entity.append((char)in_stream.read()); } else { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Unexpected character found in named entity: "+next); } next = in_stream.peek(); } if (entity.length() < 1) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Empty named character entity encountered"); } String val = StringEscapeUtils.unescapeHtml4(entity.toString()); result = Character.codePointAt(val, 0); break; default: if (!is_oct(cur)) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Unrecognized escape sequence: "+next); } result = Integer.parseInt(String.valueOf((char)cur), 8); int cnt = 1; while (is_oct(next) && cnt < 3) { cnt++; try { int n = Integer.parseInt(String.valueOf((char)next), 8); result = (8 * result) + n; } catch (NumberFormatException e) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Invalid character in octal literal: "+next, e); } in_stream.read(); next = in_stream.peek(); } break; } return result; } }
LexEscape { public static int read(final LexerStream in_stream) throws IOException, LexerException { int next = in_stream.peek(); if (next == -1) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Unexpected end of input stream when inside escape sequence"); } if (next != '\\') { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Not an escape sequence"); } int result; in_stream.read(); int cur = in_stream.read(); next = in_stream.peek(); switch (cur) { case '\'': result = '\''; break; case '\"': result = '\"'; break; case '?': result = '?'; break; case '\\': result = '\\'; break; case '0': result = '\0'; break; case 'a': result = 0x07; break; case 'b': result = '\b'; break; case 'f': result = '\f'; break; case 'n': result = '\n'; break; case 'r': result = '\r'; break; case 't': result = '\t'; break; case 'v': result = 0x0B; break; case 'x': result = 0; for (int i = 0; i < 2; i++) { if (next == -1) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Unexpected end of stream"); } try { int n = Integer.parseInt(String.valueOf((char)next), 16); result = (16 * result) + n; } catch (NumberFormatException e) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Invalid character in literal: "+next, e); } in_stream.read(); next = in_stream.peek(); } break; case 'u': result = 0; for (int i = 0; i < 4; i++) { if (next == -1) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Unexpected end of stream"); } try { int n = Integer.parseInt(String.valueOf((char)next), 16); result = (16 * result) + n; } catch (NumberFormatException e) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Invalid character in literal: "+next, e); } in_stream.read(); next = in_stream.peek(); } break; case 'U': result = 0; for (int i = 0; i < 8; i++) { if (next == -1) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Unexpected end of stream"); } try { int n = Integer.parseInt(String.valueOf((char)next), 16); result = (16 * result) + n; } catch (NumberFormatException e) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Invalid character in literal: "+next, e); } in_stream.read(); next = in_stream.peek(); } break; case '&': StringBuilder entity = new StringBuilder(); entity.append((char)cur); while (true) { if (next == -1) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Unexpected end of stream"); } if (next == ';') { entity.append((char)in_stream.read()); break; } if (Character.isLetter(next)) { entity.append((char)in_stream.read()); } else { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Unexpected character found in named entity: "+next); } next = in_stream.peek(); } if (entity.length() < 1) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Empty named character entity encountered"); } String val = StringEscapeUtils.unescapeHtml4(entity.toString()); result = Character.codePointAt(val, 0); break; default: if (!is_oct(cur)) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Unrecognized escape sequence: "+next); } result = Integer.parseInt(String.valueOf((char)cur), 8); int cnt = 1; while (is_oct(next) && cnt < 3) { cnt++; try { int n = Integer.parseInt(String.valueOf((char)next), 8); result = (8 * result) + n; } catch (NumberFormatException e) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Invalid character in octal literal: "+next, e); } in_stream.read(); next = in_stream.peek(); } break; } return result; } }
LexEscape { public static int read(final LexerStream in_stream) throws IOException, LexerException { int next = in_stream.peek(); if (next == -1) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Unexpected end of input stream when inside escape sequence"); } if (next != '\\') { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Not an escape sequence"); } int result; in_stream.read(); int cur = in_stream.read(); next = in_stream.peek(); switch (cur) { case '\'': result = '\''; break; case '\"': result = '\"'; break; case '?': result = '?'; break; case '\\': result = '\\'; break; case '0': result = '\0'; break; case 'a': result = 0x07; break; case 'b': result = '\b'; break; case 'f': result = '\f'; break; case 'n': result = '\n'; break; case 'r': result = '\r'; break; case 't': result = '\t'; break; case 'v': result = 0x0B; break; case 'x': result = 0; for (int i = 0; i < 2; i++) { if (next == -1) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Unexpected end of stream"); } try { int n = Integer.parseInt(String.valueOf((char)next), 16); result = (16 * result) + n; } catch (NumberFormatException e) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Invalid character in literal: "+next, e); } in_stream.read(); next = in_stream.peek(); } break; case 'u': result = 0; for (int i = 0; i < 4; i++) { if (next == -1) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Unexpected end of stream"); } try { int n = Integer.parseInt(String.valueOf((char)next), 16); result = (16 * result) + n; } catch (NumberFormatException e) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Invalid character in literal: "+next, e); } in_stream.read(); next = in_stream.peek(); } break; case 'U': result = 0; for (int i = 0; i < 8; i++) { if (next == -1) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Unexpected end of stream"); } try { int n = Integer.parseInt(String.valueOf((char)next), 16); result = (16 * result) + n; } catch (NumberFormatException e) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Invalid character in literal: "+next, e); } in_stream.read(); next = in_stream.peek(); } break; case '&': StringBuilder entity = new StringBuilder(); entity.append((char)cur); while (true) { if (next == -1) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Unexpected end of stream"); } if (next == ';') { entity.append((char)in_stream.read()); break; } if (Character.isLetter(next)) { entity.append((char)in_stream.read()); } else { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Unexpected character found in named entity: "+next); } next = in_stream.peek(); } if (entity.length() < 1) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Empty named character entity encountered"); } String val = StringEscapeUtils.unescapeHtml4(entity.toString()); result = Character.codePointAt(val, 0); break; default: if (!is_oct(cur)) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Unrecognized escape sequence: "+next); } result = Integer.parseInt(String.valueOf((char)cur), 8); int cnt = 1; while (is_oct(next) && cnt < 3) { cnt++; try { int n = Integer.parseInt(String.valueOf((char)next), 8); result = (8 * result) + n; } catch (NumberFormatException e) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Invalid character in octal literal: "+next, e); } in_stream.read(); next = in_stream.peek(); } break; } return result; } static int read(final LexerStream in_stream); }
LexEscape { public static int read(final LexerStream in_stream) throws IOException, LexerException { int next = in_stream.peek(); if (next == -1) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Unexpected end of input stream when inside escape sequence"); } if (next != '\\') { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Not an escape sequence"); } int result; in_stream.read(); int cur = in_stream.read(); next = in_stream.peek(); switch (cur) { case '\'': result = '\''; break; case '\"': result = '\"'; break; case '?': result = '?'; break; case '\\': result = '\\'; break; case '0': result = '\0'; break; case 'a': result = 0x07; break; case 'b': result = '\b'; break; case 'f': result = '\f'; break; case 'n': result = '\n'; break; case 'r': result = '\r'; break; case 't': result = '\t'; break; case 'v': result = 0x0B; break; case 'x': result = 0; for (int i = 0; i < 2; i++) { if (next == -1) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Unexpected end of stream"); } try { int n = Integer.parseInt(String.valueOf((char)next), 16); result = (16 * result) + n; } catch (NumberFormatException e) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Invalid character in literal: "+next, e); } in_stream.read(); next = in_stream.peek(); } break; case 'u': result = 0; for (int i = 0; i < 4; i++) { if (next == -1) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Unexpected end of stream"); } try { int n = Integer.parseInt(String.valueOf((char)next), 16); result = (16 * result) + n; } catch (NumberFormatException e) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Invalid character in literal: "+next, e); } in_stream.read(); next = in_stream.peek(); } break; case 'U': result = 0; for (int i = 0; i < 8; i++) { if (next == -1) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Unexpected end of stream"); } try { int n = Integer.parseInt(String.valueOf((char)next), 16); result = (16 * result) + n; } catch (NumberFormatException e) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Invalid character in literal: "+next, e); } in_stream.read(); next = in_stream.peek(); } break; case '&': StringBuilder entity = new StringBuilder(); entity.append((char)cur); while (true) { if (next == -1) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Unexpected end of stream"); } if (next == ';') { entity.append((char)in_stream.read()); break; } if (Character.isLetter(next)) { entity.append((char)in_stream.read()); } else { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Unexpected character found in named entity: "+next); } next = in_stream.peek(); } if (entity.length() < 1) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Empty named character entity encountered"); } String val = StringEscapeUtils.unescapeHtml4(entity.toString()); result = Character.codePointAt(val, 0); break; default: if (!is_oct(cur)) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Unrecognized escape sequence: "+next); } result = Integer.parseInt(String.valueOf((char)cur), 8); int cnt = 1; while (is_oct(next) && cnt < 3) { cnt++; try { int n = Integer.parseInt(String.valueOf((char)next), 8); result = (8 * result) + n; } catch (NumberFormatException e) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Invalid character in octal literal: "+next, e); } in_stream.read(); next = in_stream.peek(); } break; } return result; } static int read(final LexerStream in_stream); }
@Test(expected = LexerException.class) public void empty() throws Exception { String str = ""; LexerStream ls = new LexerStream(new StringReader(str)); LexIdentifier lex = new LexIdentifier(new BaseTokenFactory(), ls); lex.read(); }
public Token read() throws IOException, LexerException { int start_index = in_stream.getPosition(); int start_line = in_stream.getLine(); int start_col = in_stream.getCol(); int next = in_stream.peek(); if (next == -1) { throw new LexerException(start_line, start_col, "Unexpected end of input stream when parsing identifier"); } if (!Character.isLetter(next) && next != '_') { throw new LexerException(start_line, start_col, "Invalid character for start of identifier: "+(char)next); } StringBuilder sb = new StringBuilder(); while (valid_ident_char(next)) { sb.append(Character.toChars(in_stream.read())); next = in_stream.peek(); } String result = sb.toString(); TokenType type = TokenType.forValue(result); if (type == null) { type = TokenType.Identifier; } if (type == TokenType.Identifier && result.startsWith("__")) { throw new LexerException(start_line, start_col, "Unknown reserved identifier"); } return factory.create(type, start_index, start_line, start_col, in_stream.getPosition(), in_stream.getLine(), in_stream.getCol(), result); }
LexIdentifier { public Token read() throws IOException, LexerException { int start_index = in_stream.getPosition(); int start_line = in_stream.getLine(); int start_col = in_stream.getCol(); int next = in_stream.peek(); if (next == -1) { throw new LexerException(start_line, start_col, "Unexpected end of input stream when parsing identifier"); } if (!Character.isLetter(next) && next != '_') { throw new LexerException(start_line, start_col, "Invalid character for start of identifier: "+(char)next); } StringBuilder sb = new StringBuilder(); while (valid_ident_char(next)) { sb.append(Character.toChars(in_stream.read())); next = in_stream.peek(); } String result = sb.toString(); TokenType type = TokenType.forValue(result); if (type == null) { type = TokenType.Identifier; } if (type == TokenType.Identifier && result.startsWith("__")) { throw new LexerException(start_line, start_col, "Unknown reserved identifier"); } return factory.create(type, start_index, start_line, start_col, in_stream.getPosition(), in_stream.getLine(), in_stream.getCol(), result); } }
LexIdentifier { public Token read() throws IOException, LexerException { int start_index = in_stream.getPosition(); int start_line = in_stream.getLine(); int start_col = in_stream.getCol(); int next = in_stream.peek(); if (next == -1) { throw new LexerException(start_line, start_col, "Unexpected end of input stream when parsing identifier"); } if (!Character.isLetter(next) && next != '_') { throw new LexerException(start_line, start_col, "Invalid character for start of identifier: "+(char)next); } StringBuilder sb = new StringBuilder(); while (valid_ident_char(next)) { sb.append(Character.toChars(in_stream.read())); next = in_stream.peek(); } String result = sb.toString(); TokenType type = TokenType.forValue(result); if (type == null) { type = TokenType.Identifier; } if (type == TokenType.Identifier && result.startsWith("__")) { throw new LexerException(start_line, start_col, "Unknown reserved identifier"); } return factory.create(type, start_index, start_line, start_col, in_stream.getPosition(), in_stream.getLine(), in_stream.getCol(), result); } LexIdentifier(TokenFactory factory, LexerStream in_stream); }
LexIdentifier { public Token read() throws IOException, LexerException { int start_index = in_stream.getPosition(); int start_line = in_stream.getLine(); int start_col = in_stream.getCol(); int next = in_stream.peek(); if (next == -1) { throw new LexerException(start_line, start_col, "Unexpected end of input stream when parsing identifier"); } if (!Character.isLetter(next) && next != '_') { throw new LexerException(start_line, start_col, "Invalid character for start of identifier: "+(char)next); } StringBuilder sb = new StringBuilder(); while (valid_ident_char(next)) { sb.append(Character.toChars(in_stream.read())); next = in_stream.peek(); } String result = sb.toString(); TokenType type = TokenType.forValue(result); if (type == null) { type = TokenType.Identifier; } if (type == TokenType.Identifier && result.startsWith("__")) { throw new LexerException(start_line, start_col, "Unknown reserved identifier"); } return factory.create(type, start_index, start_line, start_col, in_stream.getPosition(), in_stream.getLine(), in_stream.getCol(), result); } LexIdentifier(TokenFactory factory, LexerStream in_stream); Token read(); }
LexIdentifier { public Token read() throws IOException, LexerException { int start_index = in_stream.getPosition(); int start_line = in_stream.getLine(); int start_col = in_stream.getCol(); int next = in_stream.peek(); if (next == -1) { throw new LexerException(start_line, start_col, "Unexpected end of input stream when parsing identifier"); } if (!Character.isLetter(next) && next != '_') { throw new LexerException(start_line, start_col, "Invalid character for start of identifier: "+(char)next); } StringBuilder sb = new StringBuilder(); while (valid_ident_char(next)) { sb.append(Character.toChars(in_stream.read())); next = in_stream.peek(); } String result = sb.toString(); TokenType type = TokenType.forValue(result); if (type == null) { type = TokenType.Identifier; } if (type == TokenType.Identifier && result.startsWith("__")) { throw new LexerException(start_line, start_col, "Unknown reserved identifier"); } return factory.create(type, start_index, start_line, start_col, in_stream.getPosition(), in_stream.getLine(), in_stream.getCol(), result); } LexIdentifier(TokenFactory factory, LexerStream in_stream); Token read(); }
@Test public void html() throws Exception { String str = "\\&gt; \\&yuml; \\&perp;"; LexerStream ls = new LexerStream(new StringReader(str)); assertEquals(62, LexEscape.read(ls)); ls.read(); assertEquals(255, LexEscape.read(ls)); ls.read(); assertEquals(8869, LexEscape.read(ls)); }
public static int read(final LexerStream in_stream) throws IOException, LexerException { int next = in_stream.peek(); if (next == -1) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Unexpected end of input stream when inside escape sequence"); } if (next != '\\') { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Not an escape sequence"); } int result; in_stream.read(); int cur = in_stream.read(); next = in_stream.peek(); switch (cur) { case '\'': result = '\''; break; case '\"': result = '\"'; break; case '?': result = '?'; break; case '\\': result = '\\'; break; case '0': result = '\0'; break; case 'a': result = 0x07; break; case 'b': result = '\b'; break; case 'f': result = '\f'; break; case 'n': result = '\n'; break; case 'r': result = '\r'; break; case 't': result = '\t'; break; case 'v': result = 0x0B; break; case 'x': result = 0; for (int i = 0; i < 2; i++) { if (next == -1) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Unexpected end of stream"); } try { int n = Integer.parseInt(String.valueOf((char)next), 16); result = (16 * result) + n; } catch (NumberFormatException e) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Invalid character in literal: "+next, e); } in_stream.read(); next = in_stream.peek(); } break; case 'u': result = 0; for (int i = 0; i < 4; i++) { if (next == -1) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Unexpected end of stream"); } try { int n = Integer.parseInt(String.valueOf((char)next), 16); result = (16 * result) + n; } catch (NumberFormatException e) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Invalid character in literal: "+next, e); } in_stream.read(); next = in_stream.peek(); } break; case 'U': result = 0; for (int i = 0; i < 8; i++) { if (next == -1) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Unexpected end of stream"); } try { int n = Integer.parseInt(String.valueOf((char)next), 16); result = (16 * result) + n; } catch (NumberFormatException e) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Invalid character in literal: "+next, e); } in_stream.read(); next = in_stream.peek(); } break; case '&': StringBuilder entity = new StringBuilder(); entity.append((char)cur); while (true) { if (next == -1) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Unexpected end of stream"); } if (next == ';') { entity.append((char)in_stream.read()); break; } if (Character.isLetter(next)) { entity.append((char)in_stream.read()); } else { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Unexpected character found in named entity: "+next); } next = in_stream.peek(); } if (entity.length() < 1) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Empty named character entity encountered"); } String val = StringEscapeUtils.unescapeHtml4(entity.toString()); result = Character.codePointAt(val, 0); break; default: if (!is_oct(cur)) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Unrecognized escape sequence: "+next); } result = Integer.parseInt(String.valueOf((char)cur), 8); int cnt = 1; while (is_oct(next) && cnt < 3) { cnt++; try { int n = Integer.parseInt(String.valueOf((char)next), 8); result = (8 * result) + n; } catch (NumberFormatException e) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Invalid character in octal literal: "+next, e); } in_stream.read(); next = in_stream.peek(); } break; } return result; }
LexEscape { public static int read(final LexerStream in_stream) throws IOException, LexerException { int next = in_stream.peek(); if (next == -1) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Unexpected end of input stream when inside escape sequence"); } if (next != '\\') { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Not an escape sequence"); } int result; in_stream.read(); int cur = in_stream.read(); next = in_stream.peek(); switch (cur) { case '\'': result = '\''; break; case '\"': result = '\"'; break; case '?': result = '?'; break; case '\\': result = '\\'; break; case '0': result = '\0'; break; case 'a': result = 0x07; break; case 'b': result = '\b'; break; case 'f': result = '\f'; break; case 'n': result = '\n'; break; case 'r': result = '\r'; break; case 't': result = '\t'; break; case 'v': result = 0x0B; break; case 'x': result = 0; for (int i = 0; i < 2; i++) { if (next == -1) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Unexpected end of stream"); } try { int n = Integer.parseInt(String.valueOf((char)next), 16); result = (16 * result) + n; } catch (NumberFormatException e) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Invalid character in literal: "+next, e); } in_stream.read(); next = in_stream.peek(); } break; case 'u': result = 0; for (int i = 0; i < 4; i++) { if (next == -1) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Unexpected end of stream"); } try { int n = Integer.parseInt(String.valueOf((char)next), 16); result = (16 * result) + n; } catch (NumberFormatException e) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Invalid character in literal: "+next, e); } in_stream.read(); next = in_stream.peek(); } break; case 'U': result = 0; for (int i = 0; i < 8; i++) { if (next == -1) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Unexpected end of stream"); } try { int n = Integer.parseInt(String.valueOf((char)next), 16); result = (16 * result) + n; } catch (NumberFormatException e) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Invalid character in literal: "+next, e); } in_stream.read(); next = in_stream.peek(); } break; case '&': StringBuilder entity = new StringBuilder(); entity.append((char)cur); while (true) { if (next == -1) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Unexpected end of stream"); } if (next == ';') { entity.append((char)in_stream.read()); break; } if (Character.isLetter(next)) { entity.append((char)in_stream.read()); } else { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Unexpected character found in named entity: "+next); } next = in_stream.peek(); } if (entity.length() < 1) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Empty named character entity encountered"); } String val = StringEscapeUtils.unescapeHtml4(entity.toString()); result = Character.codePointAt(val, 0); break; default: if (!is_oct(cur)) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Unrecognized escape sequence: "+next); } result = Integer.parseInt(String.valueOf((char)cur), 8); int cnt = 1; while (is_oct(next) && cnt < 3) { cnt++; try { int n = Integer.parseInt(String.valueOf((char)next), 8); result = (8 * result) + n; } catch (NumberFormatException e) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Invalid character in octal literal: "+next, e); } in_stream.read(); next = in_stream.peek(); } break; } return result; } }
LexEscape { public static int read(final LexerStream in_stream) throws IOException, LexerException { int next = in_stream.peek(); if (next == -1) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Unexpected end of input stream when inside escape sequence"); } if (next != '\\') { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Not an escape sequence"); } int result; in_stream.read(); int cur = in_stream.read(); next = in_stream.peek(); switch (cur) { case '\'': result = '\''; break; case '\"': result = '\"'; break; case '?': result = '?'; break; case '\\': result = '\\'; break; case '0': result = '\0'; break; case 'a': result = 0x07; break; case 'b': result = '\b'; break; case 'f': result = '\f'; break; case 'n': result = '\n'; break; case 'r': result = '\r'; break; case 't': result = '\t'; break; case 'v': result = 0x0B; break; case 'x': result = 0; for (int i = 0; i < 2; i++) { if (next == -1) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Unexpected end of stream"); } try { int n = Integer.parseInt(String.valueOf((char)next), 16); result = (16 * result) + n; } catch (NumberFormatException e) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Invalid character in literal: "+next, e); } in_stream.read(); next = in_stream.peek(); } break; case 'u': result = 0; for (int i = 0; i < 4; i++) { if (next == -1) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Unexpected end of stream"); } try { int n = Integer.parseInt(String.valueOf((char)next), 16); result = (16 * result) + n; } catch (NumberFormatException e) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Invalid character in literal: "+next, e); } in_stream.read(); next = in_stream.peek(); } break; case 'U': result = 0; for (int i = 0; i < 8; i++) { if (next == -1) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Unexpected end of stream"); } try { int n = Integer.parseInt(String.valueOf((char)next), 16); result = (16 * result) + n; } catch (NumberFormatException e) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Invalid character in literal: "+next, e); } in_stream.read(); next = in_stream.peek(); } break; case '&': StringBuilder entity = new StringBuilder(); entity.append((char)cur); while (true) { if (next == -1) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Unexpected end of stream"); } if (next == ';') { entity.append((char)in_stream.read()); break; } if (Character.isLetter(next)) { entity.append((char)in_stream.read()); } else { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Unexpected character found in named entity: "+next); } next = in_stream.peek(); } if (entity.length() < 1) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Empty named character entity encountered"); } String val = StringEscapeUtils.unescapeHtml4(entity.toString()); result = Character.codePointAt(val, 0); break; default: if (!is_oct(cur)) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Unrecognized escape sequence: "+next); } result = Integer.parseInt(String.valueOf((char)cur), 8); int cnt = 1; while (is_oct(next) && cnt < 3) { cnt++; try { int n = Integer.parseInt(String.valueOf((char)next), 8); result = (8 * result) + n; } catch (NumberFormatException e) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Invalid character in octal literal: "+next, e); } in_stream.read(); next = in_stream.peek(); } break; } return result; } }
LexEscape { public static int read(final LexerStream in_stream) throws IOException, LexerException { int next = in_stream.peek(); if (next == -1) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Unexpected end of input stream when inside escape sequence"); } if (next != '\\') { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Not an escape sequence"); } int result; in_stream.read(); int cur = in_stream.read(); next = in_stream.peek(); switch (cur) { case '\'': result = '\''; break; case '\"': result = '\"'; break; case '?': result = '?'; break; case '\\': result = '\\'; break; case '0': result = '\0'; break; case 'a': result = 0x07; break; case 'b': result = '\b'; break; case 'f': result = '\f'; break; case 'n': result = '\n'; break; case 'r': result = '\r'; break; case 't': result = '\t'; break; case 'v': result = 0x0B; break; case 'x': result = 0; for (int i = 0; i < 2; i++) { if (next == -1) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Unexpected end of stream"); } try { int n = Integer.parseInt(String.valueOf((char)next), 16); result = (16 * result) + n; } catch (NumberFormatException e) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Invalid character in literal: "+next, e); } in_stream.read(); next = in_stream.peek(); } break; case 'u': result = 0; for (int i = 0; i < 4; i++) { if (next == -1) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Unexpected end of stream"); } try { int n = Integer.parseInt(String.valueOf((char)next), 16); result = (16 * result) + n; } catch (NumberFormatException e) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Invalid character in literal: "+next, e); } in_stream.read(); next = in_stream.peek(); } break; case 'U': result = 0; for (int i = 0; i < 8; i++) { if (next == -1) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Unexpected end of stream"); } try { int n = Integer.parseInt(String.valueOf((char)next), 16); result = (16 * result) + n; } catch (NumberFormatException e) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Invalid character in literal: "+next, e); } in_stream.read(); next = in_stream.peek(); } break; case '&': StringBuilder entity = new StringBuilder(); entity.append((char)cur); while (true) { if (next == -1) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Unexpected end of stream"); } if (next == ';') { entity.append((char)in_stream.read()); break; } if (Character.isLetter(next)) { entity.append((char)in_stream.read()); } else { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Unexpected character found in named entity: "+next); } next = in_stream.peek(); } if (entity.length() < 1) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Empty named character entity encountered"); } String val = StringEscapeUtils.unescapeHtml4(entity.toString()); result = Character.codePointAt(val, 0); break; default: if (!is_oct(cur)) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Unrecognized escape sequence: "+next); } result = Integer.parseInt(String.valueOf((char)cur), 8); int cnt = 1; while (is_oct(next) && cnt < 3) { cnt++; try { int n = Integer.parseInt(String.valueOf((char)next), 8); result = (8 * result) + n; } catch (NumberFormatException e) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Invalid character in octal literal: "+next, e); } in_stream.read(); next = in_stream.peek(); } break; } return result; } static int read(final LexerStream in_stream); }
LexEscape { public static int read(final LexerStream in_stream) throws IOException, LexerException { int next = in_stream.peek(); if (next == -1) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Unexpected end of input stream when inside escape sequence"); } if (next != '\\') { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Not an escape sequence"); } int result; in_stream.read(); int cur = in_stream.read(); next = in_stream.peek(); switch (cur) { case '\'': result = '\''; break; case '\"': result = '\"'; break; case '?': result = '?'; break; case '\\': result = '\\'; break; case '0': result = '\0'; break; case 'a': result = 0x07; break; case 'b': result = '\b'; break; case 'f': result = '\f'; break; case 'n': result = '\n'; break; case 'r': result = '\r'; break; case 't': result = '\t'; break; case 'v': result = 0x0B; break; case 'x': result = 0; for (int i = 0; i < 2; i++) { if (next == -1) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Unexpected end of stream"); } try { int n = Integer.parseInt(String.valueOf((char)next), 16); result = (16 * result) + n; } catch (NumberFormatException e) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Invalid character in literal: "+next, e); } in_stream.read(); next = in_stream.peek(); } break; case 'u': result = 0; for (int i = 0; i < 4; i++) { if (next == -1) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Unexpected end of stream"); } try { int n = Integer.parseInt(String.valueOf((char)next), 16); result = (16 * result) + n; } catch (NumberFormatException e) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Invalid character in literal: "+next, e); } in_stream.read(); next = in_stream.peek(); } break; case 'U': result = 0; for (int i = 0; i < 8; i++) { if (next == -1) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Unexpected end of stream"); } try { int n = Integer.parseInt(String.valueOf((char)next), 16); result = (16 * result) + n; } catch (NumberFormatException e) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Invalid character in literal: "+next, e); } in_stream.read(); next = in_stream.peek(); } break; case '&': StringBuilder entity = new StringBuilder(); entity.append((char)cur); while (true) { if (next == -1) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Unexpected end of stream"); } if (next == ';') { entity.append((char)in_stream.read()); break; } if (Character.isLetter(next)) { entity.append((char)in_stream.read()); } else { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Unexpected character found in named entity: "+next); } next = in_stream.peek(); } if (entity.length() < 1) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Empty named character entity encountered"); } String val = StringEscapeUtils.unescapeHtml4(entity.toString()); result = Character.codePointAt(val, 0); break; default: if (!is_oct(cur)) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Unrecognized escape sequence: "+next); } result = Integer.parseInt(String.valueOf((char)cur), 8); int cnt = 1; while (is_oct(next) && cnt < 3) { cnt++; try { int n = Integer.parseInt(String.valueOf((char)next), 8); result = (8 * result) + n; } catch (NumberFormatException e) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Invalid character in octal literal: "+next, e); } in_stream.read(); next = in_stream.peek(); } break; } return result; } static int read(final LexerStream in_stream); }
@Test(expected = LexerException.class) public void empty() throws Exception { String str = ""; LexerStream ls = new LexerStream(new StringReader(str)); LexEscape.read(ls); }
public static int read(final LexerStream in_stream) throws IOException, LexerException { int next = in_stream.peek(); if (next == -1) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Unexpected end of input stream when inside escape sequence"); } if (next != '\\') { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Not an escape sequence"); } int result; in_stream.read(); int cur = in_stream.read(); next = in_stream.peek(); switch (cur) { case '\'': result = '\''; break; case '\"': result = '\"'; break; case '?': result = '?'; break; case '\\': result = '\\'; break; case '0': result = '\0'; break; case 'a': result = 0x07; break; case 'b': result = '\b'; break; case 'f': result = '\f'; break; case 'n': result = '\n'; break; case 'r': result = '\r'; break; case 't': result = '\t'; break; case 'v': result = 0x0B; break; case 'x': result = 0; for (int i = 0; i < 2; i++) { if (next == -1) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Unexpected end of stream"); } try { int n = Integer.parseInt(String.valueOf((char)next), 16); result = (16 * result) + n; } catch (NumberFormatException e) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Invalid character in literal: "+next, e); } in_stream.read(); next = in_stream.peek(); } break; case 'u': result = 0; for (int i = 0; i < 4; i++) { if (next == -1) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Unexpected end of stream"); } try { int n = Integer.parseInt(String.valueOf((char)next), 16); result = (16 * result) + n; } catch (NumberFormatException e) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Invalid character in literal: "+next, e); } in_stream.read(); next = in_stream.peek(); } break; case 'U': result = 0; for (int i = 0; i < 8; i++) { if (next == -1) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Unexpected end of stream"); } try { int n = Integer.parseInt(String.valueOf((char)next), 16); result = (16 * result) + n; } catch (NumberFormatException e) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Invalid character in literal: "+next, e); } in_stream.read(); next = in_stream.peek(); } break; case '&': StringBuilder entity = new StringBuilder(); entity.append((char)cur); while (true) { if (next == -1) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Unexpected end of stream"); } if (next == ';') { entity.append((char)in_stream.read()); break; } if (Character.isLetter(next)) { entity.append((char)in_stream.read()); } else { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Unexpected character found in named entity: "+next); } next = in_stream.peek(); } if (entity.length() < 1) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Empty named character entity encountered"); } String val = StringEscapeUtils.unescapeHtml4(entity.toString()); result = Character.codePointAt(val, 0); break; default: if (!is_oct(cur)) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Unrecognized escape sequence: "+next); } result = Integer.parseInt(String.valueOf((char)cur), 8); int cnt = 1; while (is_oct(next) && cnt < 3) { cnt++; try { int n = Integer.parseInt(String.valueOf((char)next), 8); result = (8 * result) + n; } catch (NumberFormatException e) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Invalid character in octal literal: "+next, e); } in_stream.read(); next = in_stream.peek(); } break; } return result; }
LexEscape { public static int read(final LexerStream in_stream) throws IOException, LexerException { int next = in_stream.peek(); if (next == -1) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Unexpected end of input stream when inside escape sequence"); } if (next != '\\') { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Not an escape sequence"); } int result; in_stream.read(); int cur = in_stream.read(); next = in_stream.peek(); switch (cur) { case '\'': result = '\''; break; case '\"': result = '\"'; break; case '?': result = '?'; break; case '\\': result = '\\'; break; case '0': result = '\0'; break; case 'a': result = 0x07; break; case 'b': result = '\b'; break; case 'f': result = '\f'; break; case 'n': result = '\n'; break; case 'r': result = '\r'; break; case 't': result = '\t'; break; case 'v': result = 0x0B; break; case 'x': result = 0; for (int i = 0; i < 2; i++) { if (next == -1) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Unexpected end of stream"); } try { int n = Integer.parseInt(String.valueOf((char)next), 16); result = (16 * result) + n; } catch (NumberFormatException e) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Invalid character in literal: "+next, e); } in_stream.read(); next = in_stream.peek(); } break; case 'u': result = 0; for (int i = 0; i < 4; i++) { if (next == -1) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Unexpected end of stream"); } try { int n = Integer.parseInt(String.valueOf((char)next), 16); result = (16 * result) + n; } catch (NumberFormatException e) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Invalid character in literal: "+next, e); } in_stream.read(); next = in_stream.peek(); } break; case 'U': result = 0; for (int i = 0; i < 8; i++) { if (next == -1) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Unexpected end of stream"); } try { int n = Integer.parseInt(String.valueOf((char)next), 16); result = (16 * result) + n; } catch (NumberFormatException e) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Invalid character in literal: "+next, e); } in_stream.read(); next = in_stream.peek(); } break; case '&': StringBuilder entity = new StringBuilder(); entity.append((char)cur); while (true) { if (next == -1) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Unexpected end of stream"); } if (next == ';') { entity.append((char)in_stream.read()); break; } if (Character.isLetter(next)) { entity.append((char)in_stream.read()); } else { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Unexpected character found in named entity: "+next); } next = in_stream.peek(); } if (entity.length() < 1) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Empty named character entity encountered"); } String val = StringEscapeUtils.unescapeHtml4(entity.toString()); result = Character.codePointAt(val, 0); break; default: if (!is_oct(cur)) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Unrecognized escape sequence: "+next); } result = Integer.parseInt(String.valueOf((char)cur), 8); int cnt = 1; while (is_oct(next) && cnt < 3) { cnt++; try { int n = Integer.parseInt(String.valueOf((char)next), 8); result = (8 * result) + n; } catch (NumberFormatException e) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Invalid character in octal literal: "+next, e); } in_stream.read(); next = in_stream.peek(); } break; } return result; } }
LexEscape { public static int read(final LexerStream in_stream) throws IOException, LexerException { int next = in_stream.peek(); if (next == -1) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Unexpected end of input stream when inside escape sequence"); } if (next != '\\') { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Not an escape sequence"); } int result; in_stream.read(); int cur = in_stream.read(); next = in_stream.peek(); switch (cur) { case '\'': result = '\''; break; case '\"': result = '\"'; break; case '?': result = '?'; break; case '\\': result = '\\'; break; case '0': result = '\0'; break; case 'a': result = 0x07; break; case 'b': result = '\b'; break; case 'f': result = '\f'; break; case 'n': result = '\n'; break; case 'r': result = '\r'; break; case 't': result = '\t'; break; case 'v': result = 0x0B; break; case 'x': result = 0; for (int i = 0; i < 2; i++) { if (next == -1) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Unexpected end of stream"); } try { int n = Integer.parseInt(String.valueOf((char)next), 16); result = (16 * result) + n; } catch (NumberFormatException e) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Invalid character in literal: "+next, e); } in_stream.read(); next = in_stream.peek(); } break; case 'u': result = 0; for (int i = 0; i < 4; i++) { if (next == -1) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Unexpected end of stream"); } try { int n = Integer.parseInt(String.valueOf((char)next), 16); result = (16 * result) + n; } catch (NumberFormatException e) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Invalid character in literal: "+next, e); } in_stream.read(); next = in_stream.peek(); } break; case 'U': result = 0; for (int i = 0; i < 8; i++) { if (next == -1) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Unexpected end of stream"); } try { int n = Integer.parseInt(String.valueOf((char)next), 16); result = (16 * result) + n; } catch (NumberFormatException e) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Invalid character in literal: "+next, e); } in_stream.read(); next = in_stream.peek(); } break; case '&': StringBuilder entity = new StringBuilder(); entity.append((char)cur); while (true) { if (next == -1) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Unexpected end of stream"); } if (next == ';') { entity.append((char)in_stream.read()); break; } if (Character.isLetter(next)) { entity.append((char)in_stream.read()); } else { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Unexpected character found in named entity: "+next); } next = in_stream.peek(); } if (entity.length() < 1) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Empty named character entity encountered"); } String val = StringEscapeUtils.unescapeHtml4(entity.toString()); result = Character.codePointAt(val, 0); break; default: if (!is_oct(cur)) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Unrecognized escape sequence: "+next); } result = Integer.parseInt(String.valueOf((char)cur), 8); int cnt = 1; while (is_oct(next) && cnt < 3) { cnt++; try { int n = Integer.parseInt(String.valueOf((char)next), 8); result = (8 * result) + n; } catch (NumberFormatException e) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Invalid character in octal literal: "+next, e); } in_stream.read(); next = in_stream.peek(); } break; } return result; } }
LexEscape { public static int read(final LexerStream in_stream) throws IOException, LexerException { int next = in_stream.peek(); if (next == -1) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Unexpected end of input stream when inside escape sequence"); } if (next != '\\') { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Not an escape sequence"); } int result; in_stream.read(); int cur = in_stream.read(); next = in_stream.peek(); switch (cur) { case '\'': result = '\''; break; case '\"': result = '\"'; break; case '?': result = '?'; break; case '\\': result = '\\'; break; case '0': result = '\0'; break; case 'a': result = 0x07; break; case 'b': result = '\b'; break; case 'f': result = '\f'; break; case 'n': result = '\n'; break; case 'r': result = '\r'; break; case 't': result = '\t'; break; case 'v': result = 0x0B; break; case 'x': result = 0; for (int i = 0; i < 2; i++) { if (next == -1) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Unexpected end of stream"); } try { int n = Integer.parseInt(String.valueOf((char)next), 16); result = (16 * result) + n; } catch (NumberFormatException e) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Invalid character in literal: "+next, e); } in_stream.read(); next = in_stream.peek(); } break; case 'u': result = 0; for (int i = 0; i < 4; i++) { if (next == -1) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Unexpected end of stream"); } try { int n = Integer.parseInt(String.valueOf((char)next), 16); result = (16 * result) + n; } catch (NumberFormatException e) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Invalid character in literal: "+next, e); } in_stream.read(); next = in_stream.peek(); } break; case 'U': result = 0; for (int i = 0; i < 8; i++) { if (next == -1) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Unexpected end of stream"); } try { int n = Integer.parseInt(String.valueOf((char)next), 16); result = (16 * result) + n; } catch (NumberFormatException e) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Invalid character in literal: "+next, e); } in_stream.read(); next = in_stream.peek(); } break; case '&': StringBuilder entity = new StringBuilder(); entity.append((char)cur); while (true) { if (next == -1) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Unexpected end of stream"); } if (next == ';') { entity.append((char)in_stream.read()); break; } if (Character.isLetter(next)) { entity.append((char)in_stream.read()); } else { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Unexpected character found in named entity: "+next); } next = in_stream.peek(); } if (entity.length() < 1) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Empty named character entity encountered"); } String val = StringEscapeUtils.unescapeHtml4(entity.toString()); result = Character.codePointAt(val, 0); break; default: if (!is_oct(cur)) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Unrecognized escape sequence: "+next); } result = Integer.parseInt(String.valueOf((char)cur), 8); int cnt = 1; while (is_oct(next) && cnt < 3) { cnt++; try { int n = Integer.parseInt(String.valueOf((char)next), 8); result = (8 * result) + n; } catch (NumberFormatException e) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Invalid character in octal literal: "+next, e); } in_stream.read(); next = in_stream.peek(); } break; } return result; } static int read(final LexerStream in_stream); }
LexEscape { public static int read(final LexerStream in_stream) throws IOException, LexerException { int next = in_stream.peek(); if (next == -1) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Unexpected end of input stream when inside escape sequence"); } if (next != '\\') { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Not an escape sequence"); } int result; in_stream.read(); int cur = in_stream.read(); next = in_stream.peek(); switch (cur) { case '\'': result = '\''; break; case '\"': result = '\"'; break; case '?': result = '?'; break; case '\\': result = '\\'; break; case '0': result = '\0'; break; case 'a': result = 0x07; break; case 'b': result = '\b'; break; case 'f': result = '\f'; break; case 'n': result = '\n'; break; case 'r': result = '\r'; break; case 't': result = '\t'; break; case 'v': result = 0x0B; break; case 'x': result = 0; for (int i = 0; i < 2; i++) { if (next == -1) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Unexpected end of stream"); } try { int n = Integer.parseInt(String.valueOf((char)next), 16); result = (16 * result) + n; } catch (NumberFormatException e) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Invalid character in literal: "+next, e); } in_stream.read(); next = in_stream.peek(); } break; case 'u': result = 0; for (int i = 0; i < 4; i++) { if (next == -1) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Unexpected end of stream"); } try { int n = Integer.parseInt(String.valueOf((char)next), 16); result = (16 * result) + n; } catch (NumberFormatException e) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Invalid character in literal: "+next, e); } in_stream.read(); next = in_stream.peek(); } break; case 'U': result = 0; for (int i = 0; i < 8; i++) { if (next == -1) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Unexpected end of stream"); } try { int n = Integer.parseInt(String.valueOf((char)next), 16); result = (16 * result) + n; } catch (NumberFormatException e) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Invalid character in literal: "+next, e); } in_stream.read(); next = in_stream.peek(); } break; case '&': StringBuilder entity = new StringBuilder(); entity.append((char)cur); while (true) { if (next == -1) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Unexpected end of stream"); } if (next == ';') { entity.append((char)in_stream.read()); break; } if (Character.isLetter(next)) { entity.append((char)in_stream.read()); } else { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Unexpected character found in named entity: "+next); } next = in_stream.peek(); } if (entity.length() < 1) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Empty named character entity encountered"); } String val = StringEscapeUtils.unescapeHtml4(entity.toString()); result = Character.codePointAt(val, 0); break; default: if (!is_oct(cur)) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Unrecognized escape sequence: "+next); } result = Integer.parseInt(String.valueOf((char)cur), 8); int cnt = 1; while (is_oct(next) && cnt < 3) { cnt++; try { int n = Integer.parseInt(String.valueOf((char)next), 8); result = (8 * result) + n; } catch (NumberFormatException e) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Invalid character in octal literal: "+next, e); } in_stream.read(); next = in_stream.peek(); } break; } return result; } static int read(final LexerStream in_stream); }
@Test(expected = LexerException.class) public void not() throws Exception { String str = "escape"; LexerStream ls = new LexerStream(new StringReader(str)); LexEscape.read(ls); }
public static int read(final LexerStream in_stream) throws IOException, LexerException { int next = in_stream.peek(); if (next == -1) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Unexpected end of input stream when inside escape sequence"); } if (next != '\\') { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Not an escape sequence"); } int result; in_stream.read(); int cur = in_stream.read(); next = in_stream.peek(); switch (cur) { case '\'': result = '\''; break; case '\"': result = '\"'; break; case '?': result = '?'; break; case '\\': result = '\\'; break; case '0': result = '\0'; break; case 'a': result = 0x07; break; case 'b': result = '\b'; break; case 'f': result = '\f'; break; case 'n': result = '\n'; break; case 'r': result = '\r'; break; case 't': result = '\t'; break; case 'v': result = 0x0B; break; case 'x': result = 0; for (int i = 0; i < 2; i++) { if (next == -1) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Unexpected end of stream"); } try { int n = Integer.parseInt(String.valueOf((char)next), 16); result = (16 * result) + n; } catch (NumberFormatException e) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Invalid character in literal: "+next, e); } in_stream.read(); next = in_stream.peek(); } break; case 'u': result = 0; for (int i = 0; i < 4; i++) { if (next == -1) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Unexpected end of stream"); } try { int n = Integer.parseInt(String.valueOf((char)next), 16); result = (16 * result) + n; } catch (NumberFormatException e) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Invalid character in literal: "+next, e); } in_stream.read(); next = in_stream.peek(); } break; case 'U': result = 0; for (int i = 0; i < 8; i++) { if (next == -1) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Unexpected end of stream"); } try { int n = Integer.parseInt(String.valueOf((char)next), 16); result = (16 * result) + n; } catch (NumberFormatException e) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Invalid character in literal: "+next, e); } in_stream.read(); next = in_stream.peek(); } break; case '&': StringBuilder entity = new StringBuilder(); entity.append((char)cur); while (true) { if (next == -1) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Unexpected end of stream"); } if (next == ';') { entity.append((char)in_stream.read()); break; } if (Character.isLetter(next)) { entity.append((char)in_stream.read()); } else { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Unexpected character found in named entity: "+next); } next = in_stream.peek(); } if (entity.length() < 1) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Empty named character entity encountered"); } String val = StringEscapeUtils.unescapeHtml4(entity.toString()); result = Character.codePointAt(val, 0); break; default: if (!is_oct(cur)) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Unrecognized escape sequence: "+next); } result = Integer.parseInt(String.valueOf((char)cur), 8); int cnt = 1; while (is_oct(next) && cnt < 3) { cnt++; try { int n = Integer.parseInt(String.valueOf((char)next), 8); result = (8 * result) + n; } catch (NumberFormatException e) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Invalid character in octal literal: "+next, e); } in_stream.read(); next = in_stream.peek(); } break; } return result; }
LexEscape { public static int read(final LexerStream in_stream) throws IOException, LexerException { int next = in_stream.peek(); if (next == -1) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Unexpected end of input stream when inside escape sequence"); } if (next != '\\') { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Not an escape sequence"); } int result; in_stream.read(); int cur = in_stream.read(); next = in_stream.peek(); switch (cur) { case '\'': result = '\''; break; case '\"': result = '\"'; break; case '?': result = '?'; break; case '\\': result = '\\'; break; case '0': result = '\0'; break; case 'a': result = 0x07; break; case 'b': result = '\b'; break; case 'f': result = '\f'; break; case 'n': result = '\n'; break; case 'r': result = '\r'; break; case 't': result = '\t'; break; case 'v': result = 0x0B; break; case 'x': result = 0; for (int i = 0; i < 2; i++) { if (next == -1) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Unexpected end of stream"); } try { int n = Integer.parseInt(String.valueOf((char)next), 16); result = (16 * result) + n; } catch (NumberFormatException e) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Invalid character in literal: "+next, e); } in_stream.read(); next = in_stream.peek(); } break; case 'u': result = 0; for (int i = 0; i < 4; i++) { if (next == -1) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Unexpected end of stream"); } try { int n = Integer.parseInt(String.valueOf((char)next), 16); result = (16 * result) + n; } catch (NumberFormatException e) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Invalid character in literal: "+next, e); } in_stream.read(); next = in_stream.peek(); } break; case 'U': result = 0; for (int i = 0; i < 8; i++) { if (next == -1) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Unexpected end of stream"); } try { int n = Integer.parseInt(String.valueOf((char)next), 16); result = (16 * result) + n; } catch (NumberFormatException e) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Invalid character in literal: "+next, e); } in_stream.read(); next = in_stream.peek(); } break; case '&': StringBuilder entity = new StringBuilder(); entity.append((char)cur); while (true) { if (next == -1) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Unexpected end of stream"); } if (next == ';') { entity.append((char)in_stream.read()); break; } if (Character.isLetter(next)) { entity.append((char)in_stream.read()); } else { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Unexpected character found in named entity: "+next); } next = in_stream.peek(); } if (entity.length() < 1) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Empty named character entity encountered"); } String val = StringEscapeUtils.unescapeHtml4(entity.toString()); result = Character.codePointAt(val, 0); break; default: if (!is_oct(cur)) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Unrecognized escape sequence: "+next); } result = Integer.parseInt(String.valueOf((char)cur), 8); int cnt = 1; while (is_oct(next) && cnt < 3) { cnt++; try { int n = Integer.parseInt(String.valueOf((char)next), 8); result = (8 * result) + n; } catch (NumberFormatException e) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Invalid character in octal literal: "+next, e); } in_stream.read(); next = in_stream.peek(); } break; } return result; } }
LexEscape { public static int read(final LexerStream in_stream) throws IOException, LexerException { int next = in_stream.peek(); if (next == -1) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Unexpected end of input stream when inside escape sequence"); } if (next != '\\') { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Not an escape sequence"); } int result; in_stream.read(); int cur = in_stream.read(); next = in_stream.peek(); switch (cur) { case '\'': result = '\''; break; case '\"': result = '\"'; break; case '?': result = '?'; break; case '\\': result = '\\'; break; case '0': result = '\0'; break; case 'a': result = 0x07; break; case 'b': result = '\b'; break; case 'f': result = '\f'; break; case 'n': result = '\n'; break; case 'r': result = '\r'; break; case 't': result = '\t'; break; case 'v': result = 0x0B; break; case 'x': result = 0; for (int i = 0; i < 2; i++) { if (next == -1) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Unexpected end of stream"); } try { int n = Integer.parseInt(String.valueOf((char)next), 16); result = (16 * result) + n; } catch (NumberFormatException e) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Invalid character in literal: "+next, e); } in_stream.read(); next = in_stream.peek(); } break; case 'u': result = 0; for (int i = 0; i < 4; i++) { if (next == -1) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Unexpected end of stream"); } try { int n = Integer.parseInt(String.valueOf((char)next), 16); result = (16 * result) + n; } catch (NumberFormatException e) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Invalid character in literal: "+next, e); } in_stream.read(); next = in_stream.peek(); } break; case 'U': result = 0; for (int i = 0; i < 8; i++) { if (next == -1) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Unexpected end of stream"); } try { int n = Integer.parseInt(String.valueOf((char)next), 16); result = (16 * result) + n; } catch (NumberFormatException e) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Invalid character in literal: "+next, e); } in_stream.read(); next = in_stream.peek(); } break; case '&': StringBuilder entity = new StringBuilder(); entity.append((char)cur); while (true) { if (next == -1) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Unexpected end of stream"); } if (next == ';') { entity.append((char)in_stream.read()); break; } if (Character.isLetter(next)) { entity.append((char)in_stream.read()); } else { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Unexpected character found in named entity: "+next); } next = in_stream.peek(); } if (entity.length() < 1) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Empty named character entity encountered"); } String val = StringEscapeUtils.unescapeHtml4(entity.toString()); result = Character.codePointAt(val, 0); break; default: if (!is_oct(cur)) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Unrecognized escape sequence: "+next); } result = Integer.parseInt(String.valueOf((char)cur), 8); int cnt = 1; while (is_oct(next) && cnt < 3) { cnt++; try { int n = Integer.parseInt(String.valueOf((char)next), 8); result = (8 * result) + n; } catch (NumberFormatException e) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Invalid character in octal literal: "+next, e); } in_stream.read(); next = in_stream.peek(); } break; } return result; } }
LexEscape { public static int read(final LexerStream in_stream) throws IOException, LexerException { int next = in_stream.peek(); if (next == -1) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Unexpected end of input stream when inside escape sequence"); } if (next != '\\') { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Not an escape sequence"); } int result; in_stream.read(); int cur = in_stream.read(); next = in_stream.peek(); switch (cur) { case '\'': result = '\''; break; case '\"': result = '\"'; break; case '?': result = '?'; break; case '\\': result = '\\'; break; case '0': result = '\0'; break; case 'a': result = 0x07; break; case 'b': result = '\b'; break; case 'f': result = '\f'; break; case 'n': result = '\n'; break; case 'r': result = '\r'; break; case 't': result = '\t'; break; case 'v': result = 0x0B; break; case 'x': result = 0; for (int i = 0; i < 2; i++) { if (next == -1) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Unexpected end of stream"); } try { int n = Integer.parseInt(String.valueOf((char)next), 16); result = (16 * result) + n; } catch (NumberFormatException e) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Invalid character in literal: "+next, e); } in_stream.read(); next = in_stream.peek(); } break; case 'u': result = 0; for (int i = 0; i < 4; i++) { if (next == -1) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Unexpected end of stream"); } try { int n = Integer.parseInt(String.valueOf((char)next), 16); result = (16 * result) + n; } catch (NumberFormatException e) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Invalid character in literal: "+next, e); } in_stream.read(); next = in_stream.peek(); } break; case 'U': result = 0; for (int i = 0; i < 8; i++) { if (next == -1) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Unexpected end of stream"); } try { int n = Integer.parseInt(String.valueOf((char)next), 16); result = (16 * result) + n; } catch (NumberFormatException e) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Invalid character in literal: "+next, e); } in_stream.read(); next = in_stream.peek(); } break; case '&': StringBuilder entity = new StringBuilder(); entity.append((char)cur); while (true) { if (next == -1) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Unexpected end of stream"); } if (next == ';') { entity.append((char)in_stream.read()); break; } if (Character.isLetter(next)) { entity.append((char)in_stream.read()); } else { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Unexpected character found in named entity: "+next); } next = in_stream.peek(); } if (entity.length() < 1) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Empty named character entity encountered"); } String val = StringEscapeUtils.unescapeHtml4(entity.toString()); result = Character.codePointAt(val, 0); break; default: if (!is_oct(cur)) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Unrecognized escape sequence: "+next); } result = Integer.parseInt(String.valueOf((char)cur), 8); int cnt = 1; while (is_oct(next) && cnt < 3) { cnt++; try { int n = Integer.parseInt(String.valueOf((char)next), 8); result = (8 * result) + n; } catch (NumberFormatException e) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Invalid character in octal literal: "+next, e); } in_stream.read(); next = in_stream.peek(); } break; } return result; } static int read(final LexerStream in_stream); }
LexEscape { public static int read(final LexerStream in_stream) throws IOException, LexerException { int next = in_stream.peek(); if (next == -1) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Unexpected end of input stream when inside escape sequence"); } if (next != '\\') { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Not an escape sequence"); } int result; in_stream.read(); int cur = in_stream.read(); next = in_stream.peek(); switch (cur) { case '\'': result = '\''; break; case '\"': result = '\"'; break; case '?': result = '?'; break; case '\\': result = '\\'; break; case '0': result = '\0'; break; case 'a': result = 0x07; break; case 'b': result = '\b'; break; case 'f': result = '\f'; break; case 'n': result = '\n'; break; case 'r': result = '\r'; break; case 't': result = '\t'; break; case 'v': result = 0x0B; break; case 'x': result = 0; for (int i = 0; i < 2; i++) { if (next == -1) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Unexpected end of stream"); } try { int n = Integer.parseInt(String.valueOf((char)next), 16); result = (16 * result) + n; } catch (NumberFormatException e) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Invalid character in literal: "+next, e); } in_stream.read(); next = in_stream.peek(); } break; case 'u': result = 0; for (int i = 0; i < 4; i++) { if (next == -1) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Unexpected end of stream"); } try { int n = Integer.parseInt(String.valueOf((char)next), 16); result = (16 * result) + n; } catch (NumberFormatException e) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Invalid character in literal: "+next, e); } in_stream.read(); next = in_stream.peek(); } break; case 'U': result = 0; for (int i = 0; i < 8; i++) { if (next == -1) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Unexpected end of stream"); } try { int n = Integer.parseInt(String.valueOf((char)next), 16); result = (16 * result) + n; } catch (NumberFormatException e) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Invalid character in literal: "+next, e); } in_stream.read(); next = in_stream.peek(); } break; case '&': StringBuilder entity = new StringBuilder(); entity.append((char)cur); while (true) { if (next == -1) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Unexpected end of stream"); } if (next == ';') { entity.append((char)in_stream.read()); break; } if (Character.isLetter(next)) { entity.append((char)in_stream.read()); } else { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Unexpected character found in named entity: "+next); } next = in_stream.peek(); } if (entity.length() < 1) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Empty named character entity encountered"); } String val = StringEscapeUtils.unescapeHtml4(entity.toString()); result = Character.codePointAt(val, 0); break; default: if (!is_oct(cur)) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Unrecognized escape sequence: "+next); } result = Integer.parseInt(String.valueOf((char)cur), 8); int cnt = 1; while (is_oct(next) && cnt < 3) { cnt++; try { int n = Integer.parseInt(String.valueOf((char)next), 8); result = (8 * result) + n; } catch (NumberFormatException e) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Invalid character in octal literal: "+next, e); } in_stream.read(); next = in_stream.peek(); } break; } return result; } static int read(final LexerStream in_stream); }
@Test(expected = LexerException.class) public void unknown() throws Exception { String str = "\\z"; LexerStream ls = new LexerStream(new StringReader(str)); LexEscape.read(ls); }
public static int read(final LexerStream in_stream) throws IOException, LexerException { int next = in_stream.peek(); if (next == -1) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Unexpected end of input stream when inside escape sequence"); } if (next != '\\') { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Not an escape sequence"); } int result; in_stream.read(); int cur = in_stream.read(); next = in_stream.peek(); switch (cur) { case '\'': result = '\''; break; case '\"': result = '\"'; break; case '?': result = '?'; break; case '\\': result = '\\'; break; case '0': result = '\0'; break; case 'a': result = 0x07; break; case 'b': result = '\b'; break; case 'f': result = '\f'; break; case 'n': result = '\n'; break; case 'r': result = '\r'; break; case 't': result = '\t'; break; case 'v': result = 0x0B; break; case 'x': result = 0; for (int i = 0; i < 2; i++) { if (next == -1) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Unexpected end of stream"); } try { int n = Integer.parseInt(String.valueOf((char)next), 16); result = (16 * result) + n; } catch (NumberFormatException e) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Invalid character in literal: "+next, e); } in_stream.read(); next = in_stream.peek(); } break; case 'u': result = 0; for (int i = 0; i < 4; i++) { if (next == -1) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Unexpected end of stream"); } try { int n = Integer.parseInt(String.valueOf((char)next), 16); result = (16 * result) + n; } catch (NumberFormatException e) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Invalid character in literal: "+next, e); } in_stream.read(); next = in_stream.peek(); } break; case 'U': result = 0; for (int i = 0; i < 8; i++) { if (next == -1) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Unexpected end of stream"); } try { int n = Integer.parseInt(String.valueOf((char)next), 16); result = (16 * result) + n; } catch (NumberFormatException e) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Invalid character in literal: "+next, e); } in_stream.read(); next = in_stream.peek(); } break; case '&': StringBuilder entity = new StringBuilder(); entity.append((char)cur); while (true) { if (next == -1) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Unexpected end of stream"); } if (next == ';') { entity.append((char)in_stream.read()); break; } if (Character.isLetter(next)) { entity.append((char)in_stream.read()); } else { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Unexpected character found in named entity: "+next); } next = in_stream.peek(); } if (entity.length() < 1) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Empty named character entity encountered"); } String val = StringEscapeUtils.unescapeHtml4(entity.toString()); result = Character.codePointAt(val, 0); break; default: if (!is_oct(cur)) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Unrecognized escape sequence: "+next); } result = Integer.parseInt(String.valueOf((char)cur), 8); int cnt = 1; while (is_oct(next) && cnt < 3) { cnt++; try { int n = Integer.parseInt(String.valueOf((char)next), 8); result = (8 * result) + n; } catch (NumberFormatException e) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Invalid character in octal literal: "+next, e); } in_stream.read(); next = in_stream.peek(); } break; } return result; }
LexEscape { public static int read(final LexerStream in_stream) throws IOException, LexerException { int next = in_stream.peek(); if (next == -1) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Unexpected end of input stream when inside escape sequence"); } if (next != '\\') { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Not an escape sequence"); } int result; in_stream.read(); int cur = in_stream.read(); next = in_stream.peek(); switch (cur) { case '\'': result = '\''; break; case '\"': result = '\"'; break; case '?': result = '?'; break; case '\\': result = '\\'; break; case '0': result = '\0'; break; case 'a': result = 0x07; break; case 'b': result = '\b'; break; case 'f': result = '\f'; break; case 'n': result = '\n'; break; case 'r': result = '\r'; break; case 't': result = '\t'; break; case 'v': result = 0x0B; break; case 'x': result = 0; for (int i = 0; i < 2; i++) { if (next == -1) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Unexpected end of stream"); } try { int n = Integer.parseInt(String.valueOf((char)next), 16); result = (16 * result) + n; } catch (NumberFormatException e) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Invalid character in literal: "+next, e); } in_stream.read(); next = in_stream.peek(); } break; case 'u': result = 0; for (int i = 0; i < 4; i++) { if (next == -1) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Unexpected end of stream"); } try { int n = Integer.parseInt(String.valueOf((char)next), 16); result = (16 * result) + n; } catch (NumberFormatException e) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Invalid character in literal: "+next, e); } in_stream.read(); next = in_stream.peek(); } break; case 'U': result = 0; for (int i = 0; i < 8; i++) { if (next == -1) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Unexpected end of stream"); } try { int n = Integer.parseInt(String.valueOf((char)next), 16); result = (16 * result) + n; } catch (NumberFormatException e) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Invalid character in literal: "+next, e); } in_stream.read(); next = in_stream.peek(); } break; case '&': StringBuilder entity = new StringBuilder(); entity.append((char)cur); while (true) { if (next == -1) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Unexpected end of stream"); } if (next == ';') { entity.append((char)in_stream.read()); break; } if (Character.isLetter(next)) { entity.append((char)in_stream.read()); } else { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Unexpected character found in named entity: "+next); } next = in_stream.peek(); } if (entity.length() < 1) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Empty named character entity encountered"); } String val = StringEscapeUtils.unescapeHtml4(entity.toString()); result = Character.codePointAt(val, 0); break; default: if (!is_oct(cur)) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Unrecognized escape sequence: "+next); } result = Integer.parseInt(String.valueOf((char)cur), 8); int cnt = 1; while (is_oct(next) && cnt < 3) { cnt++; try { int n = Integer.parseInt(String.valueOf((char)next), 8); result = (8 * result) + n; } catch (NumberFormatException e) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Invalid character in octal literal: "+next, e); } in_stream.read(); next = in_stream.peek(); } break; } return result; } }
LexEscape { public static int read(final LexerStream in_stream) throws IOException, LexerException { int next = in_stream.peek(); if (next == -1) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Unexpected end of input stream when inside escape sequence"); } if (next != '\\') { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Not an escape sequence"); } int result; in_stream.read(); int cur = in_stream.read(); next = in_stream.peek(); switch (cur) { case '\'': result = '\''; break; case '\"': result = '\"'; break; case '?': result = '?'; break; case '\\': result = '\\'; break; case '0': result = '\0'; break; case 'a': result = 0x07; break; case 'b': result = '\b'; break; case 'f': result = '\f'; break; case 'n': result = '\n'; break; case 'r': result = '\r'; break; case 't': result = '\t'; break; case 'v': result = 0x0B; break; case 'x': result = 0; for (int i = 0; i < 2; i++) { if (next == -1) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Unexpected end of stream"); } try { int n = Integer.parseInt(String.valueOf((char)next), 16); result = (16 * result) + n; } catch (NumberFormatException e) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Invalid character in literal: "+next, e); } in_stream.read(); next = in_stream.peek(); } break; case 'u': result = 0; for (int i = 0; i < 4; i++) { if (next == -1) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Unexpected end of stream"); } try { int n = Integer.parseInt(String.valueOf((char)next), 16); result = (16 * result) + n; } catch (NumberFormatException e) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Invalid character in literal: "+next, e); } in_stream.read(); next = in_stream.peek(); } break; case 'U': result = 0; for (int i = 0; i < 8; i++) { if (next == -1) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Unexpected end of stream"); } try { int n = Integer.parseInt(String.valueOf((char)next), 16); result = (16 * result) + n; } catch (NumberFormatException e) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Invalid character in literal: "+next, e); } in_stream.read(); next = in_stream.peek(); } break; case '&': StringBuilder entity = new StringBuilder(); entity.append((char)cur); while (true) { if (next == -1) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Unexpected end of stream"); } if (next == ';') { entity.append((char)in_stream.read()); break; } if (Character.isLetter(next)) { entity.append((char)in_stream.read()); } else { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Unexpected character found in named entity: "+next); } next = in_stream.peek(); } if (entity.length() < 1) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Empty named character entity encountered"); } String val = StringEscapeUtils.unescapeHtml4(entity.toString()); result = Character.codePointAt(val, 0); break; default: if (!is_oct(cur)) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Unrecognized escape sequence: "+next); } result = Integer.parseInt(String.valueOf((char)cur), 8); int cnt = 1; while (is_oct(next) && cnt < 3) { cnt++; try { int n = Integer.parseInt(String.valueOf((char)next), 8); result = (8 * result) + n; } catch (NumberFormatException e) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Invalid character in octal literal: "+next, e); } in_stream.read(); next = in_stream.peek(); } break; } return result; } }
LexEscape { public static int read(final LexerStream in_stream) throws IOException, LexerException { int next = in_stream.peek(); if (next == -1) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Unexpected end of input stream when inside escape sequence"); } if (next != '\\') { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Not an escape sequence"); } int result; in_stream.read(); int cur = in_stream.read(); next = in_stream.peek(); switch (cur) { case '\'': result = '\''; break; case '\"': result = '\"'; break; case '?': result = '?'; break; case '\\': result = '\\'; break; case '0': result = '\0'; break; case 'a': result = 0x07; break; case 'b': result = '\b'; break; case 'f': result = '\f'; break; case 'n': result = '\n'; break; case 'r': result = '\r'; break; case 't': result = '\t'; break; case 'v': result = 0x0B; break; case 'x': result = 0; for (int i = 0; i < 2; i++) { if (next == -1) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Unexpected end of stream"); } try { int n = Integer.parseInt(String.valueOf((char)next), 16); result = (16 * result) + n; } catch (NumberFormatException e) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Invalid character in literal: "+next, e); } in_stream.read(); next = in_stream.peek(); } break; case 'u': result = 0; for (int i = 0; i < 4; i++) { if (next == -1) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Unexpected end of stream"); } try { int n = Integer.parseInt(String.valueOf((char)next), 16); result = (16 * result) + n; } catch (NumberFormatException e) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Invalid character in literal: "+next, e); } in_stream.read(); next = in_stream.peek(); } break; case 'U': result = 0; for (int i = 0; i < 8; i++) { if (next == -1) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Unexpected end of stream"); } try { int n = Integer.parseInt(String.valueOf((char)next), 16); result = (16 * result) + n; } catch (NumberFormatException e) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Invalid character in literal: "+next, e); } in_stream.read(); next = in_stream.peek(); } break; case '&': StringBuilder entity = new StringBuilder(); entity.append((char)cur); while (true) { if (next == -1) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Unexpected end of stream"); } if (next == ';') { entity.append((char)in_stream.read()); break; } if (Character.isLetter(next)) { entity.append((char)in_stream.read()); } else { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Unexpected character found in named entity: "+next); } next = in_stream.peek(); } if (entity.length() < 1) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Empty named character entity encountered"); } String val = StringEscapeUtils.unescapeHtml4(entity.toString()); result = Character.codePointAt(val, 0); break; default: if (!is_oct(cur)) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Unrecognized escape sequence: "+next); } result = Integer.parseInt(String.valueOf((char)cur), 8); int cnt = 1; while (is_oct(next) && cnt < 3) { cnt++; try { int n = Integer.parseInt(String.valueOf((char)next), 8); result = (8 * result) + n; } catch (NumberFormatException e) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Invalid character in octal literal: "+next, e); } in_stream.read(); next = in_stream.peek(); } break; } return result; } static int read(final LexerStream in_stream); }
LexEscape { public static int read(final LexerStream in_stream) throws IOException, LexerException { int next = in_stream.peek(); if (next == -1) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Unexpected end of input stream when inside escape sequence"); } if (next != '\\') { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Not an escape sequence"); } int result; in_stream.read(); int cur = in_stream.read(); next = in_stream.peek(); switch (cur) { case '\'': result = '\''; break; case '\"': result = '\"'; break; case '?': result = '?'; break; case '\\': result = '\\'; break; case '0': result = '\0'; break; case 'a': result = 0x07; break; case 'b': result = '\b'; break; case 'f': result = '\f'; break; case 'n': result = '\n'; break; case 'r': result = '\r'; break; case 't': result = '\t'; break; case 'v': result = 0x0B; break; case 'x': result = 0; for (int i = 0; i < 2; i++) { if (next == -1) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Unexpected end of stream"); } try { int n = Integer.parseInt(String.valueOf((char)next), 16); result = (16 * result) + n; } catch (NumberFormatException e) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Invalid character in literal: "+next, e); } in_stream.read(); next = in_stream.peek(); } break; case 'u': result = 0; for (int i = 0; i < 4; i++) { if (next == -1) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Unexpected end of stream"); } try { int n = Integer.parseInt(String.valueOf((char)next), 16); result = (16 * result) + n; } catch (NumberFormatException e) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Invalid character in literal: "+next, e); } in_stream.read(); next = in_stream.peek(); } break; case 'U': result = 0; for (int i = 0; i < 8; i++) { if (next == -1) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Unexpected end of stream"); } try { int n = Integer.parseInt(String.valueOf((char)next), 16); result = (16 * result) + n; } catch (NumberFormatException e) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Invalid character in literal: "+next, e); } in_stream.read(); next = in_stream.peek(); } break; case '&': StringBuilder entity = new StringBuilder(); entity.append((char)cur); while (true) { if (next == -1) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Unexpected end of stream"); } if (next == ';') { entity.append((char)in_stream.read()); break; } if (Character.isLetter(next)) { entity.append((char)in_stream.read()); } else { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Unexpected character found in named entity: "+next); } next = in_stream.peek(); } if (entity.length() < 1) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Empty named character entity encountered"); } String val = StringEscapeUtils.unescapeHtml4(entity.toString()); result = Character.codePointAt(val, 0); break; default: if (!is_oct(cur)) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Unrecognized escape sequence: "+next); } result = Integer.parseInt(String.valueOf((char)cur), 8); int cnt = 1; while (is_oct(next) && cnt < 3) { cnt++; try { int n = Integer.parseInt(String.valueOf((char)next), 8); result = (8 * result) + n; } catch (NumberFormatException e) { throw new LexerException(in_stream.getLine(), in_stream.getCol(), "Invalid character in octal literal: "+next, e); } in_stream.read(); next = in_stream.peek(); } break; } return result; } static int read(final LexerStream in_stream); }
@Test(expected = LexerException.class) public void not() throws Exception { String str = "9d"; LexerStream ls = new LexerStream(new StringReader(str)); LexIdentifier lex = new LexIdentifier(new BaseTokenFactory(), ls); lex.read(); }
public Token read() throws IOException, LexerException { int start_index = in_stream.getPosition(); int start_line = in_stream.getLine(); int start_col = in_stream.getCol(); int next = in_stream.peek(); if (next == -1) { throw new LexerException(start_line, start_col, "Unexpected end of input stream when parsing identifier"); } if (!Character.isLetter(next) && next != '_') { throw new LexerException(start_line, start_col, "Invalid character for start of identifier: "+(char)next); } StringBuilder sb = new StringBuilder(); while (valid_ident_char(next)) { sb.append(Character.toChars(in_stream.read())); next = in_stream.peek(); } String result = sb.toString(); TokenType type = TokenType.forValue(result); if (type == null) { type = TokenType.Identifier; } if (type == TokenType.Identifier && result.startsWith("__")) { throw new LexerException(start_line, start_col, "Unknown reserved identifier"); } return factory.create(type, start_index, start_line, start_col, in_stream.getPosition(), in_stream.getLine(), in_stream.getCol(), result); }
LexIdentifier { public Token read() throws IOException, LexerException { int start_index = in_stream.getPosition(); int start_line = in_stream.getLine(); int start_col = in_stream.getCol(); int next = in_stream.peek(); if (next == -1) { throw new LexerException(start_line, start_col, "Unexpected end of input stream when parsing identifier"); } if (!Character.isLetter(next) && next != '_') { throw new LexerException(start_line, start_col, "Invalid character for start of identifier: "+(char)next); } StringBuilder sb = new StringBuilder(); while (valid_ident_char(next)) { sb.append(Character.toChars(in_stream.read())); next = in_stream.peek(); } String result = sb.toString(); TokenType type = TokenType.forValue(result); if (type == null) { type = TokenType.Identifier; } if (type == TokenType.Identifier && result.startsWith("__")) { throw new LexerException(start_line, start_col, "Unknown reserved identifier"); } return factory.create(type, start_index, start_line, start_col, in_stream.getPosition(), in_stream.getLine(), in_stream.getCol(), result); } }
LexIdentifier { public Token read() throws IOException, LexerException { int start_index = in_stream.getPosition(); int start_line = in_stream.getLine(); int start_col = in_stream.getCol(); int next = in_stream.peek(); if (next == -1) { throw new LexerException(start_line, start_col, "Unexpected end of input stream when parsing identifier"); } if (!Character.isLetter(next) && next != '_') { throw new LexerException(start_line, start_col, "Invalid character for start of identifier: "+(char)next); } StringBuilder sb = new StringBuilder(); while (valid_ident_char(next)) { sb.append(Character.toChars(in_stream.read())); next = in_stream.peek(); } String result = sb.toString(); TokenType type = TokenType.forValue(result); if (type == null) { type = TokenType.Identifier; } if (type == TokenType.Identifier && result.startsWith("__")) { throw new LexerException(start_line, start_col, "Unknown reserved identifier"); } return factory.create(type, start_index, start_line, start_col, in_stream.getPosition(), in_stream.getLine(), in_stream.getCol(), result); } LexIdentifier(TokenFactory factory, LexerStream in_stream); }
LexIdentifier { public Token read() throws IOException, LexerException { int start_index = in_stream.getPosition(); int start_line = in_stream.getLine(); int start_col = in_stream.getCol(); int next = in_stream.peek(); if (next == -1) { throw new LexerException(start_line, start_col, "Unexpected end of input stream when parsing identifier"); } if (!Character.isLetter(next) && next != '_') { throw new LexerException(start_line, start_col, "Invalid character for start of identifier: "+(char)next); } StringBuilder sb = new StringBuilder(); while (valid_ident_char(next)) { sb.append(Character.toChars(in_stream.read())); next = in_stream.peek(); } String result = sb.toString(); TokenType type = TokenType.forValue(result); if (type == null) { type = TokenType.Identifier; } if (type == TokenType.Identifier && result.startsWith("__")) { throw new LexerException(start_line, start_col, "Unknown reserved identifier"); } return factory.create(type, start_index, start_line, start_col, in_stream.getPosition(), in_stream.getLine(), in_stream.getCol(), result); } LexIdentifier(TokenFactory factory, LexerStream in_stream); Token read(); }
LexIdentifier { public Token read() throws IOException, LexerException { int start_index = in_stream.getPosition(); int start_line = in_stream.getLine(); int start_col = in_stream.getCol(); int next = in_stream.peek(); if (next == -1) { throw new LexerException(start_line, start_col, "Unexpected end of input stream when parsing identifier"); } if (!Character.isLetter(next) && next != '_') { throw new LexerException(start_line, start_col, "Invalid character for start of identifier: "+(char)next); } StringBuilder sb = new StringBuilder(); while (valid_ident_char(next)) { sb.append(Character.toChars(in_stream.read())); next = in_stream.peek(); } String result = sb.toString(); TokenType type = TokenType.forValue(result); if (type == null) { type = TokenType.Identifier; } if (type == TokenType.Identifier && result.startsWith("__")) { throw new LexerException(start_line, start_col, "Unknown reserved identifier"); } return factory.create(type, start_index, start_line, start_col, in_stream.getPosition(), in_stream.getLine(), in_stream.getCol(), result); } LexIdentifier(TokenFactory factory, LexerStream in_stream); Token read(); }
@Test public void lex() throws Exception { String code = "" + " "import std.stdio;\n" + "\n" + "void main() {\n" + " ulong lines = 0;\n" + " double sumLength=0;\n" + " foreach (line;stdin.byLine()){\n" + " ++lines;\n" + " sumLength += line.length;\n" + " }\n" + " writeln(\"Average line length: \",\n" + " lines ? sumLength/lines : 0);\n" + " }\n" + ""; List<TokenType> expected = new ArrayList<TokenType>(); expected.add(TokenType.LineComment); expected.add(TokenType.Whitespace); expected.add(TokenType.Import); expected.add(TokenType.Whitespace); expected.add(TokenType.Identifier); expected.add(TokenType.Dot); expected.add(TokenType.Identifier); expected.add(TokenType.Semicolon); expected.add(TokenType.Whitespace); expected.add(TokenType.Void); expected.add(TokenType.Whitespace); expected.add(TokenType.Identifier); expected.add(TokenType.OpenParenthesis); expected.add(TokenType.CloseParenthesis); expected.add(TokenType.Whitespace); expected.add(TokenType.OpenCurlyBrace); expected.add(TokenType.Whitespace); expected.add(TokenType.Ulong); expected.add(TokenType.Whitespace); expected.add(TokenType.Identifier); expected.add(TokenType.Whitespace); expected.add(TokenType.Assign); expected.add(TokenType.Whitespace); expected.add(TokenType.Literal); expected.add(TokenType.Semicolon); expected.add(TokenType.Whitespace); expected.add(TokenType.Double); expected.add(TokenType.Whitespace); expected.add(TokenType.Identifier); expected.add(TokenType.Assign); expected.add(TokenType.Literal); expected.add(TokenType.Semicolon); expected.add(TokenType.Whitespace); expected.add(TokenType.Foreach); expected.add(TokenType.Whitespace); expected.add(TokenType.OpenParenthesis); expected.add(TokenType.Identifier); expected.add(TokenType.Semicolon); expected.add(TokenType.Identifier); expected.add(TokenType.Dot); expected.add(TokenType.Identifier); expected.add(TokenType.OpenParenthesis); expected.add(TokenType.CloseParenthesis); expected.add(TokenType.CloseParenthesis); expected.add(TokenType.OpenCurlyBrace); expected.add(TokenType.Whitespace); expected.add(TokenType.Increment); expected.add(TokenType.Identifier); expected.add(TokenType.Semicolon); expected.add(TokenType.Whitespace); expected.add(TokenType.Identifier); expected.add(TokenType.Whitespace); expected.add(TokenType.PlusAssign); expected.add(TokenType.Whitespace); expected.add(TokenType.Identifier); expected.add(TokenType.Dot); expected.add(TokenType.Identifier); expected.add(TokenType.Semicolon); expected.add(TokenType.Whitespace); expected.add(TokenType.CloseCurlyBrace); expected.add(TokenType.Whitespace); expected.add(TokenType.Identifier); expected.add(TokenType.OpenParenthesis); expected.add(TokenType.LiteralUtf8); expected.add(TokenType.Comma); expected.add(TokenType.Whitespace); expected.add(TokenType.Identifier); expected.add(TokenType.Whitespace); expected.add(TokenType.Question); expected.add(TokenType.Whitespace); expected.add(TokenType.Identifier); expected.add(TokenType.Div); expected.add(TokenType.Identifier); expected.add(TokenType.Whitespace); expected.add(TokenType.Colon); expected.add(TokenType.Whitespace); expected.add(TokenType.Literal); expected.add(TokenType.CloseParenthesis); expected.add(TokenType.Semicolon); expected.add(TokenType.Whitespace); expected.add(TokenType.CloseCurlyBrace); expected.add(TokenType.Whitespace); expected.add(TokenType.EOF); Lexer lexer = new Lexer(new BaseTokenFactory(), new StringReader(code)); List<Token> tokens = new ArrayList<Token>(); Token tok; do { tok = lexer.next(); tokens.add(tok); } while (tok != null && tok.getType() != TokenType.EOF); assertNull(lexer.next()); for (int i = 0; i < expected.size(); i++) { assertEquals(expected.get(i), tokens.get(i).getType()); } assertEquals("std", tokens.get(4).getValue()); assertEquals("stdio", tokens.get(6).getValue()); assertEquals("main", tokens.get(11).getValue()); assertEquals("lines", tokens.get(19).getValue()); assertEquals(0L, tokens.get(23).getValue()); assertEquals("sumLength", tokens.get(28).getValue()); assertEquals(0L, tokens.get(30).getValue()); assertEquals("line", tokens.get(36).getValue()); assertEquals("stdin", tokens.get(38).getValue()); assertEquals("byLine", tokens.get(40).getValue()); assertEquals("lines", tokens.get(47).getValue()); assertEquals("sumLength", tokens.get(50).getValue()); assertEquals("line", tokens.get(54).getValue()); assertEquals("length", tokens.get(56).getValue()); assertEquals("writeln", tokens.get(61).getValue()); assertEquals("Average line length: ", tokens.get(63).getValue()); assertEquals("lines", tokens.get(66).getValue()); assertEquals("sumLength", tokens.get(70).getValue()); assertEquals("lines", tokens.get(72).getValue()); assertEquals(0L, tokens.get(76).getValue()); }
public Token next() throws IOException { Token token; try { token = _next(); } catch (LexerException e) { token = factory.create(TokenType.Unknown, in_stream.getPosition(), in_stream.getLine(), in_stream.getCol()); in_stream.read(); } return token; }
Lexer { public Token next() throws IOException { Token token; try { token = _next(); } catch (LexerException e) { token = factory.create(TokenType.Unknown, in_stream.getPosition(), in_stream.getLine(), in_stream.getCol()); in_stream.read(); } return token; } }
Lexer { public Token next() throws IOException { Token token; try { token = _next(); } catch (LexerException e) { token = factory.create(TokenType.Unknown, in_stream.getPosition(), in_stream.getLine(), in_stream.getCol()); in_stream.read(); } return token; } Lexer(TokenFactory tokenFactory, Reader inStream); private Lexer(TokenFactory tokenFactory, LexerStream inStream); }
Lexer { public Token next() throws IOException { Token token; try { token = _next(); } catch (LexerException e) { token = factory.create(TokenType.Unknown, in_stream.getPosition(), in_stream.getLine(), in_stream.getCol()); in_stream.read(); } return token; } Lexer(TokenFactory tokenFactory, Reader inStream); private Lexer(TokenFactory tokenFactory, LexerStream inStream); int position(); Token next(); Token _next(); }
Lexer { public Token next() throws IOException { Token token; try { token = _next(); } catch (LexerException e) { token = factory.create(TokenType.Unknown, in_stream.getPosition(), in_stream.getLine(), in_stream.getCol()); in_stream.read(); } return token; } Lexer(TokenFactory tokenFactory, Reader inStream); private Lexer(TokenFactory tokenFactory, LexerStream inStream); int position(); Token next(); Token _next(); }
@Test public void error() throws Exception { String code = "" + "\n" + "q\"/abc/def/\"\n" + ""; Lexer lexer = new Lexer(new BaseTokenFactory(), new StringReader(code)); assertEquals(TokenType.BlockComment, lexer.next().getType()); assertEquals(TokenType.Whitespace, lexer.next().getType()); assertEquals(TokenType.Unknown, lexer.next().getType()); Token tok = lexer.next(); assertEquals(TokenType.Identifier, tok.getType()); assertEquals("def", tok.getValue()); assertEquals(TokenType.Div, lexer.next().getType()); assertEquals(TokenType.Unknown, lexer.next().getType()); assertEquals(TokenType.EOF, lexer.next().getType()); assertNull(lexer.next()); }
public Token next() throws IOException { Token token; try { token = _next(); } catch (LexerException e) { token = factory.create(TokenType.Unknown, in_stream.getPosition(), in_stream.getLine(), in_stream.getCol()); in_stream.read(); } return token; }
Lexer { public Token next() throws IOException { Token token; try { token = _next(); } catch (LexerException e) { token = factory.create(TokenType.Unknown, in_stream.getPosition(), in_stream.getLine(), in_stream.getCol()); in_stream.read(); } return token; } }
Lexer { public Token next() throws IOException { Token token; try { token = _next(); } catch (LexerException e) { token = factory.create(TokenType.Unknown, in_stream.getPosition(), in_stream.getLine(), in_stream.getCol()); in_stream.read(); } return token; } Lexer(TokenFactory tokenFactory, Reader inStream); private Lexer(TokenFactory tokenFactory, LexerStream inStream); }
Lexer { public Token next() throws IOException { Token token; try { token = _next(); } catch (LexerException e) { token = factory.create(TokenType.Unknown, in_stream.getPosition(), in_stream.getLine(), in_stream.getCol()); in_stream.read(); } return token; } Lexer(TokenFactory tokenFactory, Reader inStream); private Lexer(TokenFactory tokenFactory, LexerStream inStream); int position(); Token next(); Token _next(); }
Lexer { public Token next() throws IOException { Token token; try { token = _next(); } catch (LexerException e) { token = factory.create(TokenType.Unknown, in_stream.getPosition(), in_stream.getLine(), in_stream.getCol()); in_stream.read(); } return token; } Lexer(TokenFactory tokenFactory, Reader inStream); private Lexer(TokenFactory tokenFactory, LexerStream inStream); int position(); Token next(); Token _next(); }