Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/jedwards1211/segment-java
keeps track of where text is located in a file during parsing and prints it underlined in context
https://github.com/jedwards1211/segment-java
parsing
Last synced: about 15 hours ago
JSON representation
keeps track of where text is located in a file during parsing and prints it underlined in context
- Host: GitHub
- URL: https://github.com/jedwards1211/segment-java
- Owner: jedwards1211
- Created: 2016-10-19T23:49:49.000Z (about 8 years ago)
- Default Branch: master
- Last Pushed: 2019-02-28T05:02:34.000Z (almost 6 years ago)
- Last Synced: 2024-11-09T06:40:55.105Z (about 2 months ago)
- Topics: parsing
- Language: Java
- Size: 27.3 KB
- Stars: 0
- Watchers: 3
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# segment-java
Displaying good parse error messages was hard, until I created this!
## Usage
It's very simple: you wrap a `String` and add the source file, start line, and start column, like so:
```java
Segment segment = new Segment(text, "somefile.txt", startLine, startCol);
```
`Segment` implements `CharSequence` and has almost the exact same API as `String` so you can use it just like you would
use a `String` in parsing. And here's why you want to: as you `split` it or make `substring`s, etc., it returns new `Segments`
that know where they are located in the file:```java
for (Segment line : segment.split("\r\n|\r|\n") {
System.out.println(line.startLine);
}
```
prints:
```
0
1
2
...etc
```
It also provides an `underlineInContext` method that's useful for showing where syntax errors are located:
```java
Segment segment = new Segment("there is an error here", "somefile.txt", 0, 0);
System.out.println(segment.split("\\s+")[4].underlineInContext());
```
prints:
```
there is an error here
^^^^
```This package also provides `SegmentMatcher` since it would be impossible to call `Pattern.matcher(segment)`:
```java
SegmentMatcher matcher = new SegmentMatcher(new Segment("hello world"), "somefile.txt", 0, 0), Pattern.compile("world"));
matcher.find();
Segment match = matcher.group();
System.out.println(match.underlineInContext());
```
prints:
```
hello world
^^^^^
```