-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Make it possible to build a parser by tokens (#30)
Make it possible to build custom parser by tokens
- Loading branch information
Showing
27 changed files
with
986 additions
and
64 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
60 changes: 60 additions & 0 deletions
60
src/main/java/com/ethlo/time/internal/token/DigitsToken.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
package com.ethlo.time.internal.token; | ||
|
||
/*- | ||
* #%L | ||
* Internet Time Utility | ||
* %% | ||
* Copyright (C) 2017 - 2024 Morten Haraldsen (ethlo) | ||
* %% | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
* #L% | ||
*/ | ||
|
||
import java.text.ParsePosition; | ||
|
||
import com.ethlo.time.Field; | ||
import com.ethlo.time.internal.util.LimitedCharArrayIntegerUtil; | ||
import com.ethlo.time.token.DateTimeToken; | ||
|
||
public class DigitsToken implements DateTimeToken | ||
{ | ||
private final Field field; | ||
private final int length; | ||
|
||
public DigitsToken(Field field, int length) | ||
{ | ||
this.field = field; | ||
this.length = length; | ||
} | ||
|
||
@Override | ||
public int read(String text, ParsePosition parsePosition) | ||
{ | ||
final int offset = parsePosition.getIndex(); | ||
final int end = offset + length; | ||
final int value = LimitedCharArrayIntegerUtil.parsePositiveInt(text, offset, end); | ||
parsePosition.setIndex(end); | ||
return value; | ||
} | ||
|
||
public Field getField() | ||
{ | ||
return field; | ||
} | ||
|
||
@Override | ||
public String toString() | ||
{ | ||
return "digits: " + field + "(" + length + ")"; | ||
} | ||
} |
61 changes: 61 additions & 0 deletions
61
src/main/java/com/ethlo/time/internal/token/FractionsToken.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
package com.ethlo.time.internal.token; | ||
|
||
/*- | ||
* #%L | ||
* Internet Time Utility | ||
* %% | ||
* Copyright (C) 2017 - 2024 Morten Haraldsen (ethlo) | ||
* %% | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
* #L% | ||
*/ | ||
|
||
import static com.ethlo.time.internal.util.LimitedCharArrayIntegerUtil.DIGIT_9; | ||
import static com.ethlo.time.internal.util.LimitedCharArrayIntegerUtil.ZERO; | ||
|
||
import java.text.ParsePosition; | ||
|
||
import com.ethlo.time.Field; | ||
import com.ethlo.time.token.DateTimeToken; | ||
|
||
public class FractionsToken implements DateTimeToken | ||
{ | ||
@Override | ||
public int read(final String text, final ParsePosition parsePosition) | ||
{ | ||
int idx = parsePosition.getIndex(); | ||
final int length = text.length(); | ||
int value = 0; | ||
while (idx < length) | ||
{ | ||
final char c = text.charAt(idx); | ||
if (c < ZERO || c > DIGIT_9) | ||
{ | ||
break; | ||
} | ||
else | ||
{ | ||
value = value * 10 + (c - ZERO); | ||
idx++; | ||
} | ||
} | ||
parsePosition.setIndex(idx); | ||
return value; | ||
} | ||
|
||
@Override | ||
public Field getField() | ||
{ | ||
return Field.NANO; | ||
} | ||
} |
Oops, something went wrong.