From ccc71ea2976c87d6fd59b63de8ac9a64a0831199 Mon Sep 17 00:00:00 2001 From: Juerg Lang Date: Tue, 26 Jul 2022 13:25:23 +0200 Subject: [PATCH] code refactoring --- ChangeLog.md | 2 +- .../jlangch/venice/impl/reader/Reader.java | 104 +++++++++--------- 2 files changed, 56 insertions(+), 50 deletions(-) diff --git a/ChangeLog.md b/ChangeLog.md index 24f3d6867..3f5888fa2 100644 --- a/ChangeLog.md +++ b/ChangeLog.md @@ -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 diff --git a/src/main/java/com/github/jlangch/venice/impl/reader/Reader.java b/src/main/java/com/github/jlangch/venice/impl/reader/Reader.java index 997c2b48e..d19487884 100644 --- a/src/main/java/com/github/jlangch/venice/impl/reader/Reader.java +++ b/src/main/java/com/github/jlangch/venice/impl/reader/Reader.java @@ -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(), '(' , ')'); @@ -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()) {