zafarkhaja / jsemver

Java implementation of the SemVer Specification
MIT License
429 stars 82 forks source link

LexerException constructed with null input #65

Closed haydin505 closed 6 months ago

haydin505 commented 9 months ago

I've inspected the code and couldn't figure out how the exception thrown with null input value.

Stack Trace

com.github.zafarkhaja.semver.expr.LexerException: null
    at com.github.zafarkhaja.semver.expr.Lexer.tokenize(Lexer.java:218)
    at c.github.zafarkhaja.semver.expr.ExpressionParser.parse(ExpressionParser.java:86)
    at c.github.zafarkhaja.semver.expr.ExpressionParser.parse(ExpressionParser.java:43)
    at com.github.zafarkhaja.semver.Version.satisfies(Version.java:325)

Corresponding method: com.github.zafarkhaja.semver.expr.Lexer#tokenize

  Stream<Token> tokenize(String input) {
      List<Token> tokens = new ArrayList<>();
      int tokenPos = 0;
      while (!input.isEmpty()) {
          boolean matched = false;
          for (Token.Type tokenType : Token.Type.values()) {
              Matcher matcher = tokenType.pattern.matcher(input);
              if (matcher.find()) {
                  matched = true;
                  input = matcher.replaceFirst("");
                  if (tokenType != Token.Type.WHITESPACE) {
                      tokens.add(new Token(
                          tokenType,
                          matcher.group(),
                          tokenPos
                      ));
                  }
                  tokenPos += matcher.end();
                  break;
              }
          }
          if (!matched) {
              throw new LexerException(input);
          }
      }
      tokens.add(new Token(Token.Type.EOI, null, tokenPos));
      return new Stream<>(tokens.toArray(new Token[0]));
  }
zafarkhaja commented 6 months ago

This is a duplicate of #38. Already fixed for 0.10.0.