Skip to content
This repository has been archived by the owner on Sep 6, 2018. It is now read-only.

Parser protocol #39

Closed
djbe opened this issue May 22, 2017 · 4 comments
Closed

Parser protocol #39

djbe opened this issue May 22, 2017 · 4 comments
Milestone

Comments

@djbe
Copy link
Member

djbe commented May 22, 2017

We should unify the interface of the parsers to one common protocol, that they should all support. This will be needed when we eventually want to support a plugin system.

I propose something like the following:

public protocol Parser {
  init(options: [String: Any])

  func parseFile(at path: Path) throws
  func parseFiles(at paths: [Path]) throws
  func stencilContext() -> [String: Any]
  /// This callback will be called when a Parser want to emit a diagnostics message
  /// You can set this on the usage-site to a closure that prints the diagnostics in any way you see fit
  /// Arguments are (message, file, line)
  var warningHandler: (String, String, UInt) -> Void { get set }
}

public extension Parser {
  func parseFiles(at paths: [Path]) throws {
    for path in paths {
      try parseFile(at: path)
    }
  }
  var waningHandler: (String, String, UInt) -> Void = { (_,_,_) in }
}

Missing from this is a way to pass parameters (not template parameters) to the parser implementations. Maybe an options dictionary?

@AliSoftware
Copy link
Contributor

Edited to add the warningHandler we talked about in #40 (comment)

@djbe
Copy link
Member Author

djbe commented May 30, 2017

Hmmm, shouldn't that be the other way around? The Parser receives a warningHandler from the main app, and calls it when needed? (and thus move the warningHandler to the init)

@AliSoftware
Copy link
Contributor

How is that the other way around? Sure I didn't add it to the init (because I feel that handler should be optional, thus the default implementation for it) but the way I've written it it's stiff the main app which provides the warningHandler to the parser as you describe, right? (sorry, I edited the comment once again I forgot to add { set } for that property)

class FooParser: Parser {  }

let p = FooParser(options: [])
p.warningHandler = { (message, file, line) in print("\(file):\(line): warning: \(message)" }
p.parseFile(at: somePath)
let ctx = p.stencilContext()

@djbe
Copy link
Member Author

djbe commented Jun 7, 2017

A reason to pass the warningHandler in init, is because it might be needed there. For example with the colors parser(s), should any of those register a duplicate extension, that'll happen during init:
https://github.com/SwiftGen/SwiftGenKit/blob/master/Sources/Parsers/ColorsFileParser.swift#L49

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

2 participants