मुझे लगता है कि लॉगिंग फ़ंक्शन टाइप करने के लिए बेहद अजीब है। मुझे log(parser)("string")
क्यों करना है? parser.log("string")
के रूप में कुछ आसान क्यों नहीं है? वैसे भी, पर काबू पाने के लिए है कि, मैं इस बजाय बनाया:
trait Logging { self: Parsers =>
// Used to turn logging on or off
val debug: Boolean
// Much easier than having to wrap a parser with a log function and type a message
// i.e. log(someParser)("Message") vs someParser.log("Message")
implicit class Logged[+A](parser: Parser[A]) {
def log(msg: String): Parser[A] =
if (debug) self.log(parser)(msg) else parser
}
}
अब आप अपने पार्सर में, आप मिश्रण-इन कर सकते इस विशेषता तो जैसे:
import scala.util.parsing.combinator.Parsers
import scala.util.parsing.input.CharSequenceReader
object CombinatorParserTest extends App with Parsers with Logging {
type Elem = Char
override val debug: Boolean = true
def notComma: Parser[Char] = elem("not comma", _ != ',')
def notEndLine: Parser[Char] = elem("not end line", x => x != '\r' && x != '\n')
def text: Parser[List[Char]] = rep(notComma.log("notComma") | notEndLine.log("notEndLine"))
val r = text(new CharSequenceReader(","))
println(r)
}
तुम भी बंद करने के लिए debug
क्षेत्र ओवरराइड कर सकते हैं अगर वांछित लॉगिंग।
चल रहा है यह भी पता चलता दूसरा पार्सर सही ढंग से अल्पविराम पार्स: सही कह रही है कि यह बार-बार में यह बार-बार प्रदान की लगता पार्स किया गया है जब का अनुरोध में
trying notComma at [email protected]
notComma --> [1.1] failure: not comma expected
,
^
trying notEndLine at [email protected]
notEndLine --> [1.2] parsed: ,
trying notComma at [email protected]
notComma --> [1.2] failure: end of input
,
^
trying notEndLine at [email protected]
notEndLine --> [1.2] failure: end of input
,
^
The result is List(,)
Process finished with exit code 0
मुझे लगता है कि EOF कृत्रिम रूप से शुरू की है लगता है, लेकिन आप कर रहे हैं एक अतिरिक्त चरित्र जब इनपुट अनुक्रम के अंत में पहले से ही है। – huynhjl