private int putPattern(String name, Pattern p) { int start = _buf.position(); _put(REGEX, name); _put(p.pattern()); _put(patternFlags(p.flags())); return _buf.position() - start; }
public static void main(String[] args) throws IOException { Path path = Paths.get("../alice.txt"); String contents = new String(Files.readAllBytes(path), StandardCharsets.UTF_8); Stream<String> words = Stream.of(contents.split("[\\P{L}]+")); show("words", words); Stream<String> song = Stream.of("gently", "down", "the", "stream"); show("song", song); Stream<String> silence = Stream.empty(); silence = Stream.<String>empty(); // Explicit type specification show("silence", silence); Stream<String> echos = Stream.generate(() -> "Echo"); show("echos", echos); Stream<Double> randoms = Stream.generate(Math::random); show("randoms", randoms); Stream<BigInteger> integers = Stream.iterate(BigInteger.ONE, n -> n.add(BigInteger.ONE)); show("integers", integers); Stream<String> wordsAnotherWay = Pattern.compile("[\\P{L}]+").splitAsStream(contents); show("wordsAnotherWay", wordsAnotherWay); try (Stream<String> lines = Files.lines(path, StandardCharsets.UTF_8)) { show("lines", lines); } }
/** * Get dependencies of a source file. * * @param path The canonical path of source file. * @return Path of dependencies. */ private ArrayList<String> getDependencies(String path) { if (!dependenceMap.containsKey(path)) { ArrayList<String> dependencies = new ArrayList<String>(); Matcher m = PATTERN_REQUIRE.matcher(read(path, charset)); while (m.find()) { // Decide which root path to use. // Path wrapped in <> is related to root path. // Path wrapped in "" is related to parent folder of the source file. String root = null; if (m.group(1).equals("<")) { root = this.root; } else { root = new File(path).getParent(); } // Get path of required file. String required = m.group(2); File f = new File(root, required); if (f.exists()) { dependencies.add(canonize(f)); } else { App.exit("Cannot find required file " + required + " in " + path); } } dependenceMap.put(path, dependencies); } return dependenceMap.get(path); }
/** * Create a new SourceFile instance with specified rootPath, file encoding and file type. * * @param root Root path specified from command line. * @param charset Text encoding of source file. */ public SourceFile(String root, String charset) { // Initiation. binaryCache = new HashMap<String, byte[]>(); dependenceMap = new HashMap<String, ArrayList<String>>(); // Match "// #require <path>" or "// #require "path"" or "/* #require <path> */" or "/* #require // "path" */". PATTERN_REQUIRE = Pattern.compile( "^/[/\\*]\\s#require\\s([\"<])([\\w\\-\\./]+)[\">](?:\\s\\*/)?$", Pattern.MULTILINE); locateRoot(root); this.charset = charset; }
private void putPattern(String name, Pattern p) { _put(REGEX, name); _put(p.pattern()); _put(regexFlags(p.flags())); }