Пример #1
0
 char readChar() throws IOException {
   char result = (char) input.read();
   if (result == 0xFFFF) {
     throw new IOException("LexerInput EOF");
   }
   return result;
 }
 char readChar() throws IOException {
   int result = input.read();
   if (result == LexerInput.EOF) {
     throw new IOException("LexerInput EOF");
   }
   return (char) result;
 }
Пример #3
0
  private int read() {
    int result = input.read();
    if (result == LexerInput.EOF) {
      result = CharStream.EOF;
    }

    return result;
  }
Пример #4
0
  public void consume() {
    int c = input.read();
    index++;
    charPositionInLine++;

    if (c == '\n') {
      line++;
      charPositionInLine = 0;
    }
  }
Пример #5
0
 void backup(int i) {
   input.backup(i);
 }
Пример #6
0
 public char[] GetSuffix(int len) {
   if (len > input.readLength()) throw new IllegalArgumentException();
   return input.readText(input.readLength() - len, input.readLength()).toString().toCharArray();
 }
Пример #7
0
 String GetImage() {
   return input.readText().toString();
 }
  //    public boolean isInputComment() {
  //        for (int i = 0; i < 2; i++) {
  //            input.read();
  //        }
  //        String nextTwo = input.readText(input.readLength() - 2, input.readLength()).toString();
  //        input.backup(2);
  //        return nextTwo.equals("--");
  //    }
  public SSTemplateTokenId findNextToken() {

    int c = input.read();
    int d = c;
    if (c == LexerInput.EOF) {
      return null;
    }

    Matcher matcher;

    while (c != LexerInput.EOF) {

      CharSequence text = input.readText();
      d = c;
      Debugger.oneLine("text='" + text + "', state.main='" + state.main + "'");

      switch (state.main) {
        case INIT:
          if (CharSequenceUtilities.startsWith(text, COMMENT_START)) {
            state.main = SSTemplateLexerState.Main.COMMENT;
            Debugger.oneLine("state.main='" + state.main + "'");
          } else if (CharSequenceUtilities.startsWith(text, INSTRUCTION_START)) {
            state.main = SSTemplateLexerState.Main.INSTRUCTION;
            state.sub = SSTemplateLexerState.Sub.INIT;
            SSTemplateTokenId result = SSTemplateTokenId.T_SSTEMPLATE_INSTRUCTION;
            // Since a comment(<%--) and an inscruction (<%) start the same, We need
            // to double check the instruction is not actually a comment by peeking
            // at the next two characters
            String nextTwoChars = SSTemplateLexerInputHelper.peek(input, 2);
            if (CharSequenceUtilities.startsWith(text + nextTwoChars, COMMENT_START)) {
              result = SSTemplateTokenId.T_SSTEMPLATE_COMMENT;
              state.main = SSTemplateLexerState.Main.COMMENT;
            }
            return result;
          } else if (CharSequenceUtilities.startsWith(text, VARIABLE_ESCAPED_START)) {
            state.main = SSTemplateLexerState.Main.VARIABLE;
            state.sub = SSTemplateLexerState.Sub.INIT;
            Debugger.oneLine(
                "state.main='"
                    + state.main
                    + "', state.sub='"
                    + state.sub
                    + "' return "
                    + SSTemplateTokenId.T_SSTEMPLATE_VARIABLE);
            return SSTemplateTokenId.T_SSTEMPLATE_VARIABLE;
          } else if (VALID_VARIABLE.matcher(text).find()) {
            state.main = SSTemplateLexerState.Main.VARIABLE;
            state.sub = SSTemplateLexerState.Sub.INIT;
          }
          break;

        case COMMENT:
          break;

        case VARIABLE:

        case INSTRUCTION:
          /* End markups */
          if (state.main == SSTemplateLexerState.Main.VARIABLE) {
            if (c == '}' || Character.isDigit(c) || Character.isAlphabetic(c)) {
              Debugger.oneLine(
                  "text='"
                      + text
                      + "', state.main='"
                      + state.main
                      + "' return "
                      + SSTemplateTokenId.T_SSTEMPLATE_VARIABLE);
              return SSTemplateTokenId.T_SSTEMPLATE_VARIABLE;
            } else if (c == '.' || c == '(' || c == ')') {
              return SSTemplateTokenId.T_SSTEMPLATE_PUNCTUATION;
            } else {
              return SSTemplateTokenId.T_SSTEMPLATE_OTHER;
            }
          }

          /* Whitespaces */
          if (Character.isWhitespace(c)) {

            do {
              c = input.read();
            } while (c != LexerInput.EOF && Character.isWhitespace(c));

            if (c != LexerInput.EOF) {
              input.backup(1);
            }
            Debugger.oneLine(
                "text='"
                    + text
                    + "', state.main='"
                    + state.main
                    + "' return "
                    + SSTemplateTokenId.T_SSTEMPLATE_WHITESPACE);
            return SSTemplateTokenId.T_SSTEMPLATE_WHITESPACE;
          }

          /* End markups */
          if (c == '%' || c == '>') {

            d = input.read();

            if (d == LexerInput.EOF) {
              Debugger.oneLine(
                  "text='"
                      + text
                      + "', state.main='"
                      + state.main
                      + "' return "
                      + SSTemplateTokenId.T_SSTEMPLATE_OTHER);
              return SSTemplateTokenId.T_SSTEMPLATE_OTHER;
            }

            int e = input.read();

            if (d == '>' && e == LexerInput.EOF) {

              if (state.main == SSTemplateLexerState.Main.INSTRUCTION && c == '%') {
                Debugger.oneLine(
                    "text='"
                        + text
                        + "', state.main='"
                        + state.main
                        + "' return "
                        + SSTemplateTokenId.T_SSTEMPLATE_INSTRUCTION);
                return SSTemplateTokenId.T_SSTEMPLATE_INSTRUCTION;
              }

              if (state.main == SSTemplateLexerState.Main.VARIABLE && c == '}') {
                Debugger.oneLine(
                    "text='"
                        + text
                        + "', state.main='"
                        + state.main
                        + "' return "
                        + SSTemplateTokenId.T_SSTEMPLATE_VARIABLE);
                return SSTemplateTokenId.T_SSTEMPLATE_VARIABLE;
              }
            }

            input.backup(2);
          }

          /* Operators */
          if (!(state.main == SSTemplateLexerState.Main.INSTRUCTION
              && state.sub == SSTemplateLexerState.Sub.INIT)) {

            d = c;

            int characters = 0;
            while (c != LexerInput.EOF && input.readLength() < OPERATOR_LENGTH) {
              c = input.read();
              characters++;
            }

            matcher = REGEX_OPERATOR.matcher(input.readText());
            if (matcher.find()) {

              String operator = matcher.group();
              matcher = REGEX_WHITESPACE_END.matcher(operator);

              if (matcher.find()) {

                input.backup(characters - matcher.start());
                return SSTemplateTokenId.T_SSTEMPLATE_OPERATOR;

              } else {

                input.backup(characters - operator.length() + 1);
                Debugger.oneLine(
                    "text='"
                        + text
                        + "', state.main='"
                        + state.main
                        + "' return "
                        + SSTemplateTokenId.T_SSTEMPLATE_OPERATOR);
                return SSTemplateTokenId.T_SSTEMPLATE_OPERATOR;
              }
            }

            input.backup(characters);
            c = d;

          } else if (c == '-') {
              /* Trim operator */

            Debugger.oneLine(
                "text='"
                    + text
                    + "', state.main='"
                    + state.main
                    + "' return "
                    + SSTemplateTokenId.T_SSTEMPLATE_OPERATOR);

            return SSTemplateTokenId.T_SSTEMPLATE_OPERATOR;
          }

          /* Names */
          if (Character.isLetter(c) || c == '_') {

            do {
              c = input.read();
            } while (c != LexerInput.EOF
                && (Character.isLetter(c) || Character.isDigit(c) || c == '_'));

            if (c != LexerInput.EOF) {
              input.backup(1);
            }

            if (state.main == SSTemplateLexerState.Main.INSTRUCTION
                && state.sub == SSTemplateLexerState.Sub.INIT) {
              state.sub = SSTemplateLexerState.Sub.NONE;
              Debugger.oneLine(
                  "text='"
                      + text
                      + "', state.main='"
                      + state.main
                      + "' return "
                      + SSTemplateTokenId.T_SSTEMPLATE_FUNCTION);
              return SSTemplateTokenId.T_SSTEMPLATE_FUNCTION;
            } else {
              Debugger.oneLine(
                  "text='"
                      + text
                      + "', state.main='"
                      + state.main
                      + "' return "
                      + SSTemplateTokenId.T_SSTEMPLATE_NAME);
              return SSTemplateTokenId.T_SSTEMPLATE_NAME;
            }
          }

          /* Numbers */
          if (Character.isDigit(c)) {

            boolean dotFound = false;

            do {
              if (c == '.') {
                dotFound = true;
              }
              c = input.read();
            } while (c != LexerInput.EOF && (Character.isDigit(c) || (!dotFound && c == '.')));

            if (c != LexerInput.EOF) {
              input.backup(1);
            }
            Debugger.oneLine(
                "text='"
                    + text
                    + "', state.main='"
                    + state.main
                    + "' return "
                    + SSTemplateTokenId.T_SSTEMPLATE_NUMBER);
            return SSTemplateTokenId.T_SSTEMPLATE_NUMBER;
          }

          /* Double quoted strings */
          if (c == '"') {

            boolean escaped = false;

            do {
              if (c == '\\' && !escaped) {
                escaped = true;
              } else {
                escaped = false;
              }
              c = input.read();
            } while (c != LexerInput.EOF && (escaped || c != '"'));
            Debugger.oneLine(
                "text='"
                    + text
                    + "', state.main='"
                    + state.main
                    + "' return "
                    + SSTemplateTokenId.T_SSTEMPLATE_STRING);

            return SSTemplateTokenId.T_SSTEMPLATE_STRING;
          }

          /* Single quoted strings */
          if (c == '\'') {

            boolean escaped = false;

            do {
              if (c == '\\' && !escaped) {
                escaped = true;
              } else {
                escaped = false;
              }
              c = input.read();
            } while (c != LexerInput.EOF && (escaped || c != '\''));
            Debugger.oneLine(
                "text='"
                    + text
                    + "', state.main='"
                    + state.main
                    + "' return "
                    + SSTemplateTokenId.T_SSTEMPLATE_STRING);

            return SSTemplateTokenId.T_SSTEMPLATE_STRING;
          }

          /* PUNCTUATION */
          if (PUNCTUATION.indexOf(c) >= 0) {
            Debugger.oneLine(
                "text='"
                    + text
                    + "', state.main='"
                    + state.main
                    + "' return "
                    + SSTemplateTokenId.T_SSTEMPLATE_PUNCTUATION);
            return SSTemplateTokenId.T_SSTEMPLATE_PUNCTUATION;
          }
          Debugger.oneLine(
              "text='"
                  + text
                  + "', state.main='"
                  + state.main
                  + "' return "
                  + SSTemplateTokenId.T_SSTEMPLATE_OTHER);

          return SSTemplateTokenId.T_SSTEMPLATE_OTHER;
      }

      c = input.read();
    }

    if (state.main == SSTemplateLexerState.Main.COMMENT) {
      return SSTemplateTokenId.T_SSTEMPLATE_COMMENT;
    }
    return SSTemplateTokenId.T_SSTEMPLATE_OTHER;
  }
Пример #9
0
 private void backup(int count) {
   input.backup(count);
 }
Пример #10
0
 public String substring(int start, int stop) {
   return input.readText(start, stop).toString();
 }