Skip to content

Commit

Permalink
code refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
jlangch committed Jul 26, 2022
1 parent 0e01b82 commit ccc71ea
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 50 deletions.
2 changes: 1 addition & 1 deletion ChangeLog.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ and this project adheres to
- performance improvements to the `grep` functions by using `pmap` instead of
`map` in its implementation.
- a `delay-queue` that is based on the Java class `java.util.concurrent.DelayQueue`
- added reader macro for regex patterns: #"[0-9]+"
- reader macro for regex patterns: #"[0-9]+"

### Fixed

Expand Down
104 changes: 55 additions & 49 deletions src/main/java/com/github/jlangch/venice/impl/reader/Reader.java
Original file line number Diff line number Diff line change
Expand Up @@ -335,55 +335,7 @@ private static VncVal read_form(final Reader rdr) {
.withMeta(MetaUtil.toMeta(token));

case '#':
// Reader macros (built-in)
final String sToken = token.getToken();
if (sToken.length() == 1) {
rdr.next();
Token t = rdr.peek();
if (t != null) {
if (t.getType() == TokenType.SPECIAL_CHAR) {
if (t.charAt(0) == '{') {
// set literal #{1 2}
return VncHashSet.ofAll(read_list(rdr, VncList.empty(), '{' , '}'));
}
else if (t.charAt(0) == '(') {
final VncVal meta = MetaUtil.toMeta(t);
// anonymous function literal #(> % 2)
if (rdr.anonymousFnArgs.isCapturing()) {
throw new ParseError(formatParseError(t, " #() forms cannot be nested"));
}
rdr.anonymousFnArgs.startCapture();
final VncVal body = read_list(rdr, VncList.empty(), '(' , ')').withMeta(meta);
final VncVector argsDef = rdr.anonymousFnArgs.buildArgDef().withMeta(meta);
final VncVal s_expr = VncList.of(new VncSymbol("fn", meta), argsDef, body);
rdr.anonymousFnArgs.stopCapture();
return s_expr;
}
}
else if (t.getType() == TokenType.STRING) {
rdr.next();
try {
String s = t.getToken();
if (s.startsWith("\"")) s = s.substring(1);
if (s.endsWith("\"")) s = s.substring(0, s.length()-1);
return new VncJavaObject(Pattern.compile(s));
}
catch (PatternSyntaxException ex) {
throw new ParseError(formatParseError(
token,
"Illegal regex pattern in reader macro: " + ex.getMessage()));
}
}
}
}
else if (sToken.charAt(1) == '\\') {
// char #\A
rdr.next();
return read_char(token);
}
throw new ParseError(formatParseError(
token,
"Expected a valid reader macro '#{..}', '#(..)' or '#\\x'"));
return parseReaderMacro(rdr, token);

case '(':
return read_list(rdr, VncList.empty(), '(' , ')');
Expand Down Expand Up @@ -411,6 +363,60 @@ else if (sToken.charAt(1) == '\\') {
}
}

private static VncVal parseReaderMacro(final Reader rdr, final Token token) {
// Reader macros (built-in)
final String sToken = token.getToken();
if (sToken.length() == 1) {
rdr.next();
Token t = rdr.peek();
if (t != null) {
if (t.getType() == TokenType.SPECIAL_CHAR) {
if (t.charAt(0) == '{') {
// set literal: #{1 2}
return VncHashSet.ofAll(read_list(rdr, VncList.empty(), '{' , '}'));
}
else if (t.charAt(0) == '(') {
final VncVal meta = MetaUtil.toMeta(t);
// anonymous function literal: #(> % 2)
if (rdr.anonymousFnArgs.isCapturing()) {
throw new ParseError(formatParseError(t, " #() forms cannot be nested"));
}
rdr.anonymousFnArgs.startCapture();
final VncVal body = read_list(rdr, VncList.empty(), '(' , ')').withMeta(meta);
final VncVector argsDef = rdr.anonymousFnArgs.buildArgDef().withMeta(meta);
final VncVal s_expr = VncList.of(new VncSymbol("fn", meta), argsDef, body);
rdr.anonymousFnArgs.stopCapture();
return s_expr;
}
}
else if (t.getType() == TokenType.STRING) {
// regex pattern: #"[0-9]+"
rdr.next();
try {
String s = t.getToken();
if (s.startsWith("\"")) s = s.substring(1);
if (s.endsWith("\"")) s = s.substring(0, s.length()-1);
return new VncJavaObject(Pattern.compile(s));
}
catch (PatternSyntaxException ex) {
throw new ParseError(formatParseError(
token,
"Illegal regex pattern in reader macro: " + ex.getMessage()));
}
}
}
}
else if (sToken.charAt(1) == '\\') {
// char literal: #\A
rdr.next();
return read_char(token);
}
throw new ParseError(formatParseError(
token,
"Expected a valid reader macro '#{..}', '#(..)' or '#\\x'"));

}

private static VncChar read_char(final Token token) {
final String s = token.getToken();
if (token.isAny()) {
Expand Down

0 comments on commit ccc71ea

Please sign in to comment.