1 /* Title: Pure/Isar/outer_parse.scala
4 Generic parsers for Isabelle/Isar outer syntax.
9 import scala.util.parsing.combinator.Parsers
16 trait Parser extends Parsers
20 def filter_proper = true
22 private def proper(in: Input): Input =
23 if (in.atEnd || !in.first.is_ignored || !filter_proper) in
26 def token(s: String, pred: Elem => Boolean): Parser[Elem] = new Parser[Elem]
28 def apply(raw_input: Input) =
30 val in = proper(raw_input)
31 if (in.atEnd) Failure(s + " expected (past end-of-file!)", in)
34 if (pred(token)) Success(token, proper(in.rest))
38 Failure(s + " expected,\nbut " + txt + " was found", in)
40 Failure(s + " expected,\nbut " + txt1 + " was found:\n" + txt2, in)
46 def not_eof: Parser[Elem] = token("input token", _ => true)
47 def eof: Parser[Unit] = not(not_eof)
49 def atom(s: String, pred: Elem => Boolean): Parser[String] =
50 token(s, pred) ^^ (_.content)
52 def keyword(name: String): Parser[String] =
53 atom(Token.Kind.KEYWORD.toString + " \"" + name + "\"",
54 tok => tok.kind == Token.Kind.KEYWORD && tok.content == name)
56 def name: Parser[String] = atom("name declaration", _.is_name)
57 def xname: Parser[String] = atom("name reference", _.is_xname)
58 def text: Parser[String] = atom("text", _.is_text)
59 def ML_source: Parser[String] = atom("ML source", _.is_text)
60 def doc_source: Parser[String] = atom("document source", _.is_text)
61 def path: Parser[String] = atom("file name/path specification", _.is_name)
63 private def tag_name: Parser[String] =
64 atom("tag name", tok =>
65 tok.kind == Token.Kind.IDENT ||
66 tok.kind == Token.Kind.STRING)
68 def tags: Parser[List[String]] = rep(keyword("%") ~> tag_name)
73 def parse[T](p: Parser[T], in: Token.Reader): ParseResult[T] = p(in)
74 def parse_all[T](p: Parser[T], in: Token.Reader): ParseResult[T] = parse(phrase(p), in)