Skip to content

Commit

Permalink
Don't need the T: Node<R> generic on Parser.
Browse files Browse the repository at this point in the history
It didn't offer anything as `T` was always `Node<R>` since all the rules generated random nodes.
We we also never used it besides  just returning T
  • Loading branch information
lytefast committed Oct 25, 2020
1 parent f76c57c commit 0ae88af
Show file tree
Hide file tree
Showing 7 changed files with 26 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ object CustomMarkdownRules {
return if (state.isInQuote) { null } else { super.match(inspectionSource, lastCapture, state) }
}

override fun parse(matcher: Matcher, parser: Parser<RC, in BlockQuoteNode<RC>, S>, state: S): ParseSpec<RC, BlockQuoteNode<RC>, S> {
override fun parse(matcher: Matcher, parser: Parser<RC, in BlockQuoteNode<RC>, S>, state: S): ParseSpec<RC, S> {
val groupIndex = if (matcher.group(1) != null) { 1 } else { 2 }
val newState = state.newBlockQuoteState(isInQuote = true)
return ParseSpec.createNonterminal(BlockQuoteNode(), newState, matcher.start(groupIndex), matcher.end(groupIndex))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ class MainActivity : AppCompatActivity() {

@Suppress("unused")
class FooRule<S> : Rule<Any?, Node<Any?>, S>(Pattern.compile("^<Foo>")) {
override fun parse(matcher: Matcher, parser: Parser<Any?, in Node<Any?>, S>, state: S): ParseSpec<Any?, Node<Any?>, S> {
override fun parse(matcher: Matcher, parser: Parser<Any?, in Node<Any?>, S>, state: S): ParseSpec<Any?, S> {
return ParseSpec.createTerminal(TextNode("Bar"), state)
}
}
Expand All @@ -98,7 +98,7 @@ class MainActivity : AppCompatActivity() {
}

class UserMentionRule<S> : Rule<RenderContext, UserNode, S>(Pattern.compile("^<(\\d+)>")) {
override fun parse(matcher: Matcher, parser: Parser<RenderContext, in UserNode, S>, state: S): ParseSpec<RenderContext, UserNode, S> {
override fun parse(matcher: Matcher, parser: Parser<RenderContext, in UserNode, S>, state: S): ParseSpec<RenderContext, S> {
return ParseSpec.createTerminal(UserNode(matcher.group(1).toInt()), state)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,22 +19,22 @@ import com.discord.simpleast.core.node.Node
* @param S The type of state that child nodes will use. This is mainly used to just pass through
* the state back to the parser.
*/
class ParseSpec<R, T : Node<R>, S> {
val root: T?
class ParseSpec<R, S> {
val root: Node<R>
val isTerminal: Boolean
val state: S
var startIndex: Int = 0
var endIndex: Int = 0

constructor(root: T?, state: S, startIndex: Int, endIndex: Int) {
constructor(root: Node<R>, state: S, startIndex: Int, endIndex: Int) {
this.root = root
this.state = state
this.isTerminal = false
this.startIndex = startIndex
this.endIndex = endIndex
}

constructor(root: T?, state: S) {
constructor(root: Node<R>, state: S) {
this.root = root
this.state = state
this.isTerminal = true
Expand All @@ -48,12 +48,12 @@ class ParseSpec<R, T : Node<R>, S> {
companion object {

@JvmStatic
fun <R, T : Node<R>, S> createNonterminal(node: T?, state: S, startIndex: Int, endIndex: Int): ParseSpec<R, T, S> {
fun <R, S> createNonterminal(node: Node<R>, state: S, startIndex: Int, endIndex: Int): ParseSpec<R, S> {
return ParseSpec(node, state, startIndex, endIndex)
}

@JvmStatic
fun <R, T : Node<R>, S> createTerminal(node: T?, state: S): ParseSpec<R, T, S> {
fun <R, S> createTerminal(node: Node<R>, state: S): ParseSpec<R, S> {
return ParseSpec(node, state)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,14 @@ open class Parser<R, T : Node<R>, S> @JvmOverloads constructor(private val enabl
* @throws ParseException for certain specific error flows.
*/
@JvmOverloads
fun parse(source: CharSequence, initialState: S, rules: List<Rule<R, out T, S>> = this.rules): MutableList<T> {
val remainingParses = Stack<ParseSpec<R, out T, S>>()
val topLevelNodes = ArrayList<T>()
fun parse(source: CharSequence, initialState: S, rules: List<Rule<R, out T, S>> = this.rules): MutableList<Node<R>> {
val remainingParses = Stack<ParseSpec<R, S>>()
val topLevelRootNode = Node<R>()

var lastCapture: String? = null

if (source.isNotEmpty()) {
remainingParses.add(ParseSpec(null, initialState, 0, source.length))
remainingParses.add(ParseSpec(topLevelRootNode, initialState, 0, source.length))
}

while (!remainingParses.isEmpty()) {
Expand Down Expand Up @@ -68,11 +68,9 @@ open class Parser<R, T : Node<R>, S> @JvmOverloads constructor(private val enabl

val matcherSourceEnd = matcher.end() + offset
val newBuilder = rule.parse(matcher, this, builder.state)
val parent = builder.root

newBuilder.root?.let {
parent?.addChild(it) ?: topLevelNodes.add(it)
}
val parent = builder.root
parent.addChild(newBuilder.root)

// In case the last match didn't consume the rest of the source for this subtree,
// make sure the rest of the source is consumed.
Expand All @@ -95,7 +93,7 @@ open class Parser<R, T : Node<R>, S> @JvmOverloads constructor(private val enabl
}
}

return topLevelNodes
return topLevelRootNode.getChildren()?.toMutableList()?: arrayListOf()
}

private fun <R, T: Node<R>, S> logMatch(rule: Rule<R, T, S>, source: CharSequence) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ abstract class Rule<R, T : Node<R>, S>(val matcher: Matcher) {
return if (matcher.find()) matcher else null
}

abstract fun parse(matcher: Matcher, parser: Parser<R, in T, S>, state: S): ParseSpec<R, T, S>
abstract fun parse(matcher: Matcher, parser: Parser<R, in T, S>, state: S): ParseSpec<R, S>

/**
* A [Rule] that ensures that the [matcher] is only executed if the preceding capture was a newline.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,15 +51,15 @@ object SimpleMarkdownRules {

fun <R, S> createTextRule(): Rule<R, Node<R>, S> {
return object : Rule<R, Node<R>, S>(PATTERN_TEXT) {
override fun parse(matcher: Matcher, parser: Parser<R, in Node<R>, S>, state: S): ParseSpec<R, Node<R>, S> {
override fun parse(matcher: Matcher, parser: Parser<R, in Node<R>, S>, state: S): ParseSpec<R, S> {
val node = TextNode<R>(matcher.group())
return ParseSpec.createTerminal(node, state)
}
}
}
fun <R, S> createNewlineRule(): Rule<R, Node<R>, S> {
return object : Rule.BlockRule<R, Node<R>, S>(PATTERN_NEWLINE) {
override fun parse(matcher: Matcher, parser: Parser<R, in Node<R>, S>, state: S): ParseSpec<R, Node<R>, S> {
override fun parse(matcher: Matcher, parser: Parser<R, in Node<R>, S>, state: S): ParseSpec<R, S> {
val node = TextNode<R>("\n")
return ParseSpec.createTerminal(node, state)
}
Expand All @@ -68,15 +68,15 @@ object SimpleMarkdownRules {

fun <R, S> createEscapeRule(): Rule<R, Node<R>, S> {
return object : Rule<R, Node<R>, S>(PATTERN_ESCAPE) {
override fun parse(matcher: Matcher, parser: Parser<R, in Node<R>, S>, state: S): ParseSpec<R, Node<R>, S> {
override fun parse(matcher: Matcher, parser: Parser<R, in Node<R>, S>, state: S): ParseSpec<R, S> {
return ParseSpec.createTerminal(TextNode(matcher.group(1)), state)
}
}
}

fun <R, S> createItalicsRule(): Rule<R, Node<R>, S> {
return object : Rule<R, Node<R>, S>(PATTERN_ITALICS) {
override fun parse(matcher: Matcher, parser: Parser<R, in Node<R>, S>, state: S): ParseSpec<R, Node<R>, S> {
override fun parse(matcher: Matcher, parser: Parser<R, in Node<R>, S>, state: S): ParseSpec<R, S> {
val startIndex: Int
val endIndex: Int
val asteriskMatch = matcher.group(2)
Expand Down Expand Up @@ -115,7 +115,7 @@ object SimpleMarkdownRules {
@JvmStatic
fun <R, S> createSimpleStyleRule(pattern: Pattern, styleFactory: () -> List<CharacterStyle>) =
object : Rule<R, Node<R>, S>(pattern) {
override fun parse(matcher: Matcher, parser: Parser<R, in Node<R>, S>, state: S): ParseSpec<R, Node<R>, S> {
override fun parse(matcher: Matcher, parser: Parser<R, in Node<R>, S>, state: S): ParseSpec<R, S> {
val node = StyleNode<R, CharacterStyle>(styleFactory())
return ParseSpec.createNonterminal(node, state, matcher.start(1), matcher.end(1))
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ object MarkdownRules {
Rule.BlockRule<R, Node<R>, S>(PATTERN_LIST_ITEM) {

override fun parse(matcher: Matcher, parser: Parser<R, in Node<R>, S>, state: S)
: ParseSpec<R, Node<R>, S> {
: ParseSpec<R, S> {
val node = MarkdownListItemNode<R>(bulletSpanProvider)
return ParseSpec.createNonterminal(node, state, matcher.start(1), matcher.end(1))
}
Expand All @@ -87,7 +87,7 @@ object MarkdownRules {
return StyleNode(listOf(styleSpanProvider(numHeaderIndicators)))
}

override fun parse(matcher: Matcher, parser: Parser<R, in Node<R>, S>, state: S): ParseSpec<R, Node<R>, S> =
override fun parse(matcher: Matcher, parser: Parser<R, in Node<R>, S>, state: S): ParseSpec<R, S> =
ParseSpec.createNonterminal(
createHeaderStyleNode(matcher.group(1)),
state, matcher.start(2), matcher.end(2))
Expand All @@ -97,7 +97,7 @@ object MarkdownRules {
HeaderRule<R, S>(pattern, styleSpanProvider) {

override fun parse(matcher: Matcher, parser: Parser<R, in Node<R>, S>, state: S)
: ParseSpec<R, Node<R>, S> = ParseSpec.createNonterminal(
: ParseSpec<R, S> = ParseSpec.createNonterminal(
createHeaderStyleNode(matcher.group(2)), state, matcher.start(1), matcher.end(1))

override fun createHeaderStyleNode(headerStyleGroup: String): StyleNode<R, CharacterStyle> {
Expand Down Expand Up @@ -136,7 +136,7 @@ object MarkdownRules {
SimpleMarkdownRules.createSimpleMarkdownRules<RC, S>(false)
+ SimpleMarkdownRules.createTextRule())

override fun parse(matcher: Matcher, parser: Parser<RC, in Node<RC>, S>, state: S): ParseSpec<RC, Node<RC>, S> {
override fun parse(matcher: Matcher, parser: Parser<RC, in Node<RC>, S>, state: S): ParseSpec<RC, S> {
val defaultStyleNode = createHeaderStyleNode(matcher.group(4))
val headerBody = matcher.group(1) ?: matcher.group(3)
val children = parser.parse(headerBody, state, innerRules)
Expand Down

0 comments on commit 0ae88af

Please sign in to comment.