/*
 * Copyright 2014, Steve Rowe
 * SPDX-License-Identifier: BSD-3-Clause
 */

%{
  private static final int maxCodePoint = 0x10FFFF;
  private final String[] propertyValues = new String[maxCodePoint + 1];

  private void printOutput() {
    String prevPropertyValue = propertyValues[0];
    int begCodePoint = 0;
    for (int codePoint = 1 ; codePoint <= maxCodePoint ; ++codePoint ) {
      if (codePoint == 0xD800) { // Skip the surrogate blocks
        printBlock(begCodePoint, codePoint - 1, prevPropertyValue);
        begCodePoint = codePoint = 0xE000;
        prevPropertyValue = propertyValues[codePoint];
        continue;
      }
      String propertyValue = propertyValues[codePoint];
      if (null == propertyValue || ! propertyValue.equals(prevPropertyValue)) {
        printBlock(begCodePoint, codePoint - 1, prevPropertyValue);
        prevPropertyValue = propertyValue;
        begCodePoint = codePoint;
      }
    }
    printBlock(begCodePoint, maxCodePoint, prevPropertyValue);
  }

  private void printBlock(int begCodePoint, int endCodePoint, String propertyValue) {
    System.out.format("%04X..%04X; %s%n", begCodePoint, endCodePoint, propertyValue);
  }

  private void setCurCharPropertyValue(String propertyValue) {
    int index = 0;
    while (index < yylength()) {
      int codePoint = yytext().codePointAt(index);
      propertyValues[codePoint] = propertyValue;
      index += Character.charCount(codePoint);
    }
  }
%}
