Marks the start or end of a text block.
Marks the start or end of a text block. The linePredicate
and its offset
define where in the text this marker is placed.
Inheritance allows use to reuse the constructor code defined in MarkerFactory
's apply methods.
Inheritance allows use to reuse the constructor code defined in MarkerFactory
's apply methods.
Alternatively, we could have used implicit conversions to turn, for example, from("start")
into from(BlockMarker((anInt: Int, aString: String) => "start".equals"))
and thus share constructors among to
, from
, and the rest. The drawback of these conversions is that target typing is not available,
requiring the user of this library to provide types explicitly, therefore having to write from((_: String).startsWith("start"))
instead of from(_.startsWith("start"))
.
Thrown when the text couldn't be properly parsed.
A bunch of lines are a text block.
When a line within the parsed text matches the predicate
, we set a eu.matthiasbraun.sparse.Parser.BlockMarker after that line.
When a line within the parsed text matches the predicate
, we set a eu.matthiasbraun.sparse.Parser.BlockMarker after that line.
This marker may either indicate the start or end of a eu.matthiasbraun.sparse.Parser.TextBlock.
Example usage:
parse(source, after("line before start"), to("end line"))
parse(source, from("start line), after(_.startsWith("prefix of line before end")))
parse(source, after((line, lineNr) => line == "line before start" && lineNr > 10), to("end line"))
When a line within the parsed text matches the predicate
, we set a eu.matthiasbraun.sparse.Parser.BlockMarker before that line.
When a line within the parsed text matches the predicate
, we set a eu.matthiasbraun.sparse.Parser.BlockMarker before that line.
This marker indicates the start of a eu.matthiasbraun.sparse.Parser.TextBlock.
Example usage:
parse(source, before("line after start"), to("end line"))
parse(source, before(_.startsWith("prefix of line after start"), to("end line"))
parse(source, before((line, lineNr) => line == "line after start" && lineNr > 10), to("end line"))
When a line within the parsed text matches the predicate
, we set a eu.matthiasbraun.sparse.Parser.BlockMarker at that line.
When a line within the parsed text matches the predicate
, we set a eu.matthiasbraun.sparse.Parser.BlockMarker at that line.
This marker indicates the start of a eu.matthiasbraun.sparse.Parser.TextBlock.
Example usage:
parse(source, from("start line"), to("end line"))
parse(source, from(_.endsWith("start line suffix")), to(_.startsWith("end line prefix")))
parse(source, from((line, lineNr) => line == "start line" && lineNr > 10 ), to("end line))
Parses eu.matthiasbraun.sparse.Parser.TextBlocks from a source
, such as a file.
Parses eu.matthiasbraun.sparse.Parser.TextBlocks from a source
, such as a file.
The start and end of a text block are defined by the start
and end
eu.matthiasbraun.sparse.Parser.BlockMarkers.
These markers match lines within the text.
The caller can create those markers using eu.matthiasbraun.sparse.Parser.from or eu.matthiasbraun.sparse.Parser.until, for example.
Example usage:
val file = new File("parse/this/file") val blocksMaybe = parse(fromFile(file), from("start line"), to("end line")) blocksMaybe match { case Success(blocks) => blocks.foreach { println } case Failure(exception) => println(exception) }
the scala.io.Source that contains the text that we want to parse
the text blocks we want to parse begin at this eu.matthiasbraun.sparse.Parser.BlockMarker
the text blocks we want to parse stop at this eu.matthiasbraun.sparse.Parser.BlockMarker
either a list of eu.matthiasbraun.sparse.Parser.TextBlocks wrapped in a scala.util.Success or a scala.util.Failure with the first exception that occurred during parsing.
When a line within the parsed text matches the predicate
, we set a eu.matthiasbraun.sparse.Parser.BlockMarker at that line.
When a line within the parsed text matches the predicate
, we set a eu.matthiasbraun.sparse.Parser.BlockMarker at that line.
This marker indicates the end of a eu.matthiasbraun.sparse.Parser.TextBlock.
Example usage:
parse(source, from("start line"), to("end line"))
parse(source, from(_.endsWith("start line suffix")), to(_.startsWith("end line prefix")))
parse(source, from("start line), to((line, lineNr) => line == "end line" && lineNr > 10 ))
When a line within the parsed text matches the predicate
, we set a eu.matthiasbraun.sparse.Parser.BlockMarker before that line.
When a line within the parsed text matches the predicate
, we set a eu.matthiasbraun.sparse.Parser.BlockMarker before that line.
This marker indicates the end of a eu.matthiasbraun.sparse.Parser.TextBlock.
Example usage:
parse(source, from("start line"), until("line after end"))
parse(source, from("start line"), until(_.startsWith("prefix of line after end")))
parse(source, from("start line), until((line, lineNr) => line == "line after end " && lineNr > 10 ))
A parser for extracting text blocks from a source such as a file. Created by Matthias Braun on 5/7/15.