src/Pure/Tools/bibtex.scala
author wenzelm
Fri, 03 Oct 2014 20:19:42 +0200
changeset 58527 4b190c763097
parent 58526 f05ccce3eca2
child 58528 7d6b8f8893e8
permissions -rw-r--r--
strict spaces for item_start: despite actual bibtex syntax, but in accordance to bibtex modes in Emacs and jEdit;
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
58523
937c479e62fe some support for bibtex files;
wenzelm
parents:
diff changeset
     1
/*  Title:      Pure/Tools/bibtex.scala
937c479e62fe some support for bibtex files;
wenzelm
parents:
diff changeset
     2
    Author:     Makarius
937c479e62fe some support for bibtex files;
wenzelm
parents:
diff changeset
     3
937c479e62fe some support for bibtex files;
wenzelm
parents:
diff changeset
     4
Some support for bibtex files.
937c479e62fe some support for bibtex files;
wenzelm
parents:
diff changeset
     5
*/
937c479e62fe some support for bibtex files;
wenzelm
parents:
diff changeset
     6
937c479e62fe some support for bibtex files;
wenzelm
parents:
diff changeset
     7
package isabelle
937c479e62fe some support for bibtex files;
wenzelm
parents:
diff changeset
     8
937c479e62fe some support for bibtex files;
wenzelm
parents:
diff changeset
     9
937c479e62fe some support for bibtex files;
wenzelm
parents:
diff changeset
    10
import scala.util.parsing.input.{Reader, CharSequenceReader}
937c479e62fe some support for bibtex files;
wenzelm
parents:
diff changeset
    11
import scala.util.parsing.combinator.RegexParsers
937c479e62fe some support for bibtex files;
wenzelm
parents:
diff changeset
    12
937c479e62fe some support for bibtex files;
wenzelm
parents:
diff changeset
    13
937c479e62fe some support for bibtex files;
wenzelm
parents:
diff changeset
    14
object Bibtex
937c479e62fe some support for bibtex files;
wenzelm
parents:
diff changeset
    15
{
937c479e62fe some support for bibtex files;
wenzelm
parents:
diff changeset
    16
  /** content **/
937c479e62fe some support for bibtex files;
wenzelm
parents:
diff changeset
    17
937c479e62fe some support for bibtex files;
wenzelm
parents:
diff changeset
    18
  val months = List(
937c479e62fe some support for bibtex files;
wenzelm
parents:
diff changeset
    19
    "jan",
937c479e62fe some support for bibtex files;
wenzelm
parents:
diff changeset
    20
    "feb",
937c479e62fe some support for bibtex files;
wenzelm
parents:
diff changeset
    21
    "mar",
937c479e62fe some support for bibtex files;
wenzelm
parents:
diff changeset
    22
    "apr",
937c479e62fe some support for bibtex files;
wenzelm
parents:
diff changeset
    23
    "may",
937c479e62fe some support for bibtex files;
wenzelm
parents:
diff changeset
    24
    "jun",
937c479e62fe some support for bibtex files;
wenzelm
parents:
diff changeset
    25
    "jul",
937c479e62fe some support for bibtex files;
wenzelm
parents:
diff changeset
    26
    "aug",
937c479e62fe some support for bibtex files;
wenzelm
parents:
diff changeset
    27
    "sep",
937c479e62fe some support for bibtex files;
wenzelm
parents:
diff changeset
    28
    "oct",
937c479e62fe some support for bibtex files;
wenzelm
parents:
diff changeset
    29
    "nov",
937c479e62fe some support for bibtex files;
wenzelm
parents:
diff changeset
    30
    "dec")
937c479e62fe some support for bibtex files;
wenzelm
parents:
diff changeset
    31
937c479e62fe some support for bibtex files;
wenzelm
parents:
diff changeset
    32
  val commands = List("preamble", "string")
937c479e62fe some support for bibtex files;
wenzelm
parents:
diff changeset
    33
58524
f805b366a497 context menu for bibtex entries;
wenzelm
parents: 58523
diff changeset
    34
  sealed case class Entry(
58526
f05ccce3eca2 SideKick parser for bibtex entries;
wenzelm
parents: 58524
diff changeset
    35
    kind: String,
58523
937c479e62fe some support for bibtex files;
wenzelm
parents:
diff changeset
    36
    required: List[String],
937c479e62fe some support for bibtex files;
wenzelm
parents:
diff changeset
    37
    optional_crossref: List[String],
937c479e62fe some support for bibtex files;
wenzelm
parents:
diff changeset
    38
    optional: List[String])
58524
f805b366a497 context menu for bibtex entries;
wenzelm
parents: 58523
diff changeset
    39
  {
f805b366a497 context menu for bibtex entries;
wenzelm
parents: 58523
diff changeset
    40
    def fields: List[String] = required ::: optional_crossref ::: optional
f805b366a497 context menu for bibtex entries;
wenzelm
parents: 58523
diff changeset
    41
    def template: String =
58526
f05ccce3eca2 SideKick parser for bibtex entries;
wenzelm
parents: 58524
diff changeset
    42
      "@" + kind + "{,\n" + fields.map(x => "  " + x + " = {},\n").mkString + "}\n"
58524
f805b366a497 context menu for bibtex entries;
wenzelm
parents: 58523
diff changeset
    43
  }
58523
937c479e62fe some support for bibtex files;
wenzelm
parents:
diff changeset
    44
58524
f805b366a497 context menu for bibtex entries;
wenzelm
parents: 58523
diff changeset
    45
  val entries: List[Entry] =
f805b366a497 context menu for bibtex entries;
wenzelm
parents: 58523
diff changeset
    46
    List(
f805b366a497 context menu for bibtex entries;
wenzelm
parents: 58523
diff changeset
    47
      Entry("Article",
f805b366a497 context menu for bibtex entries;
wenzelm
parents: 58523
diff changeset
    48
        List("author", "title"),
f805b366a497 context menu for bibtex entries;
wenzelm
parents: 58523
diff changeset
    49
        List("journal", "year"),
f805b366a497 context menu for bibtex entries;
wenzelm
parents: 58523
diff changeset
    50
        List("volume", "number", "pages", "month", "note")),
f805b366a497 context menu for bibtex entries;
wenzelm
parents: 58523
diff changeset
    51
      Entry("InProceedings",
f805b366a497 context menu for bibtex entries;
wenzelm
parents: 58523
diff changeset
    52
        List("author", "title"),
f805b366a497 context menu for bibtex entries;
wenzelm
parents: 58523
diff changeset
    53
        List("booktitle", "year"),
f805b366a497 context menu for bibtex entries;
wenzelm
parents: 58523
diff changeset
    54
        List("editor", "volume", "number", "series", "pages", "month", "address",
f805b366a497 context menu for bibtex entries;
wenzelm
parents: 58523
diff changeset
    55
          "organization", "publisher", "note")),
f805b366a497 context menu for bibtex entries;
wenzelm
parents: 58523
diff changeset
    56
      Entry("InCollection",
f805b366a497 context menu for bibtex entries;
wenzelm
parents: 58523
diff changeset
    57
        List("author", "title", "booktitle"),
f805b366a497 context menu for bibtex entries;
wenzelm
parents: 58523
diff changeset
    58
        List("publisher", "year"),
f805b366a497 context menu for bibtex entries;
wenzelm
parents: 58523
diff changeset
    59
        List("editor", "volume", "number", "series", "type", "chapter", "pages",
f805b366a497 context menu for bibtex entries;
wenzelm
parents: 58523
diff changeset
    60
          "edition", "month", "address", "note")),
f805b366a497 context menu for bibtex entries;
wenzelm
parents: 58523
diff changeset
    61
      Entry("InBook",
f805b366a497 context menu for bibtex entries;
wenzelm
parents: 58523
diff changeset
    62
        List("author", "editor", "title", "chapter"),
f805b366a497 context menu for bibtex entries;
wenzelm
parents: 58523
diff changeset
    63
        List("publisher", "year"),
f805b366a497 context menu for bibtex entries;
wenzelm
parents: 58523
diff changeset
    64
        List("volume", "number", "series", "type", "address", "edition", "month", "pages", "note")),
f805b366a497 context menu for bibtex entries;
wenzelm
parents: 58523
diff changeset
    65
      Entry("Proceedings",
f805b366a497 context menu for bibtex entries;
wenzelm
parents: 58523
diff changeset
    66
        List("title", "year"),
f805b366a497 context menu for bibtex entries;
wenzelm
parents: 58523
diff changeset
    67
        List(),
f805b366a497 context menu for bibtex entries;
wenzelm
parents: 58523
diff changeset
    68
        List("booktitle", "editor", "volume", "number", "series", "address", "month",
f805b366a497 context menu for bibtex entries;
wenzelm
parents: 58523
diff changeset
    69
          "organization", "publisher", "note")),
f805b366a497 context menu for bibtex entries;
wenzelm
parents: 58523
diff changeset
    70
      Entry("Book",
f805b366a497 context menu for bibtex entries;
wenzelm
parents: 58523
diff changeset
    71
        List("author", "editor", "title"),
f805b366a497 context menu for bibtex entries;
wenzelm
parents: 58523
diff changeset
    72
        List("publisher", "year"),
f805b366a497 context menu for bibtex entries;
wenzelm
parents: 58523
diff changeset
    73
        List("volume", "number", "series", "address", "edition", "month", "note")),
f805b366a497 context menu for bibtex entries;
wenzelm
parents: 58523
diff changeset
    74
      Entry("Booklet",
f805b366a497 context menu for bibtex entries;
wenzelm
parents: 58523
diff changeset
    75
        List("title"),
f805b366a497 context menu for bibtex entries;
wenzelm
parents: 58523
diff changeset
    76
        List(),
f805b366a497 context menu for bibtex entries;
wenzelm
parents: 58523
diff changeset
    77
        List("author", "howpublished", "address", "month", "year", "note")),
f805b366a497 context menu for bibtex entries;
wenzelm
parents: 58523
diff changeset
    78
      Entry("PhdThesis",
f805b366a497 context menu for bibtex entries;
wenzelm
parents: 58523
diff changeset
    79
        List("author", "title", "school", "year"),
f805b366a497 context menu for bibtex entries;
wenzelm
parents: 58523
diff changeset
    80
        List(),
f805b366a497 context menu for bibtex entries;
wenzelm
parents: 58523
diff changeset
    81
        List("type", "address", "month", "note")),
f805b366a497 context menu for bibtex entries;
wenzelm
parents: 58523
diff changeset
    82
      Entry("MastersThesis",
f805b366a497 context menu for bibtex entries;
wenzelm
parents: 58523
diff changeset
    83
        List("author", "title", "school", "year"),
f805b366a497 context menu for bibtex entries;
wenzelm
parents: 58523
diff changeset
    84
        List(),
f805b366a497 context menu for bibtex entries;
wenzelm
parents: 58523
diff changeset
    85
        List("type", "address", "month", "note")),
f805b366a497 context menu for bibtex entries;
wenzelm
parents: 58523
diff changeset
    86
      Entry("TechReport",
f805b366a497 context menu for bibtex entries;
wenzelm
parents: 58523
diff changeset
    87
        List("author", "title", "institution", "year"),
f805b366a497 context menu for bibtex entries;
wenzelm
parents: 58523
diff changeset
    88
        List(),
f805b366a497 context menu for bibtex entries;
wenzelm
parents: 58523
diff changeset
    89
        List("type", "number", "address", "month", "note")),
f805b366a497 context menu for bibtex entries;
wenzelm
parents: 58523
diff changeset
    90
      Entry("Manual",
f805b366a497 context menu for bibtex entries;
wenzelm
parents: 58523
diff changeset
    91
        List("title"),
f805b366a497 context menu for bibtex entries;
wenzelm
parents: 58523
diff changeset
    92
        List(),
f805b366a497 context menu for bibtex entries;
wenzelm
parents: 58523
diff changeset
    93
        List("author", "organization", "address", "edition", "month", "year", "note")),
f805b366a497 context menu for bibtex entries;
wenzelm
parents: 58523
diff changeset
    94
      Entry("Unpublished",
f805b366a497 context menu for bibtex entries;
wenzelm
parents: 58523
diff changeset
    95
        List("author", "title", "note"),
f805b366a497 context menu for bibtex entries;
wenzelm
parents: 58523
diff changeset
    96
        List(),
f805b366a497 context menu for bibtex entries;
wenzelm
parents: 58523
diff changeset
    97
        List("month", "year")),
f805b366a497 context menu for bibtex entries;
wenzelm
parents: 58523
diff changeset
    98
      Entry("Misc",
f805b366a497 context menu for bibtex entries;
wenzelm
parents: 58523
diff changeset
    99
        List(),
f805b366a497 context menu for bibtex entries;
wenzelm
parents: 58523
diff changeset
   100
        List(),
f805b366a497 context menu for bibtex entries;
wenzelm
parents: 58523
diff changeset
   101
        List("author", "title", "howpublished", "month", "year", "note")))
58523
937c479e62fe some support for bibtex files;
wenzelm
parents:
diff changeset
   102
937c479e62fe some support for bibtex files;
wenzelm
parents:
diff changeset
   103
937c479e62fe some support for bibtex files;
wenzelm
parents:
diff changeset
   104
937c479e62fe some support for bibtex files;
wenzelm
parents:
diff changeset
   105
  /** tokens and chunks **/
937c479e62fe some support for bibtex files;
wenzelm
parents:
diff changeset
   106
937c479e62fe some support for bibtex files;
wenzelm
parents:
diff changeset
   107
  object Token
937c479e62fe some support for bibtex files;
wenzelm
parents:
diff changeset
   108
  {
937c479e62fe some support for bibtex files;
wenzelm
parents:
diff changeset
   109
    object Kind extends Enumeration
937c479e62fe some support for bibtex files;
wenzelm
parents:
diff changeset
   110
    {
937c479e62fe some support for bibtex files;
wenzelm
parents:
diff changeset
   111
      val KEYWORD = Value("keyword")
937c479e62fe some support for bibtex files;
wenzelm
parents:
diff changeset
   112
      val NAT = Value("natural number")
937c479e62fe some support for bibtex files;
wenzelm
parents:
diff changeset
   113
      val IDENT = Value("identifier")
937c479e62fe some support for bibtex files;
wenzelm
parents:
diff changeset
   114
      val STRING = Value("string")
937c479e62fe some support for bibtex files;
wenzelm
parents:
diff changeset
   115
      val SPACE = Value("white space")
937c479e62fe some support for bibtex files;
wenzelm
parents:
diff changeset
   116
      val ERROR = Value("bad input")
937c479e62fe some support for bibtex files;
wenzelm
parents:
diff changeset
   117
    }
937c479e62fe some support for bibtex files;
wenzelm
parents:
diff changeset
   118
  }
937c479e62fe some support for bibtex files;
wenzelm
parents:
diff changeset
   119
937c479e62fe some support for bibtex files;
wenzelm
parents:
diff changeset
   120
  sealed case class Token(kind: Token.Kind.Value, val source: String)
937c479e62fe some support for bibtex files;
wenzelm
parents:
diff changeset
   121
  {
937c479e62fe some support for bibtex files;
wenzelm
parents:
diff changeset
   122
    def is_space: Boolean = kind == Token.Kind.SPACE
937c479e62fe some support for bibtex files;
wenzelm
parents:
diff changeset
   123
    def is_error: Boolean = kind == Token.Kind.ERROR
937c479e62fe some support for bibtex files;
wenzelm
parents:
diff changeset
   124
  }
937c479e62fe some support for bibtex files;
wenzelm
parents:
diff changeset
   125
58526
f05ccce3eca2 SideKick parser for bibtex entries;
wenzelm
parents: 58524
diff changeset
   126
  abstract class Chunk { def size: Int; def kind: String = "" }
f05ccce3eca2 SideKick parser for bibtex entries;
wenzelm
parents: 58524
diff changeset
   127
  case class Ignored(source: String) extends Chunk { def size: Int = source.size }
f05ccce3eca2 SideKick parser for bibtex entries;
wenzelm
parents: 58524
diff changeset
   128
  case class Malformed(source: String) extends Chunk { def size: Int = source.size }
58523
937c479e62fe some support for bibtex files;
wenzelm
parents:
diff changeset
   129
  case class Item(tokens: List[Token]) extends Chunk
937c479e62fe some support for bibtex files;
wenzelm
parents:
diff changeset
   130
  {
58526
f05ccce3eca2 SideKick parser for bibtex entries;
wenzelm
parents: 58524
diff changeset
   131
    def size: Int = (0 /: tokens)({ case (n, token) => n + token.source.size })
f05ccce3eca2 SideKick parser for bibtex entries;
wenzelm
parents: 58524
diff changeset
   132
f05ccce3eca2 SideKick parser for bibtex entries;
wenzelm
parents: 58524
diff changeset
   133
    private val content: (String, List[Token]) =
58523
937c479e62fe some support for bibtex files;
wenzelm
parents:
diff changeset
   134
      tokens match {
937c479e62fe some support for bibtex files;
wenzelm
parents:
diff changeset
   135
        case Token(Token.Kind.KEYWORD, "@") :: body
937c479e62fe some support for bibtex files;
wenzelm
parents:
diff changeset
   136
        if !body.isEmpty && !body.exists(_.is_error) =>
58526
f05ccce3eca2 SideKick parser for bibtex entries;
wenzelm
parents: 58524
diff changeset
   137
          (body.init.filterNot(_.is_space), body.last) match {
f05ccce3eca2 SideKick parser for bibtex entries;
wenzelm
parents: 58524
diff changeset
   138
            case (Token(Token.Kind.IDENT, id) :: Token(Token.Kind.KEYWORD, "{") :: toks,
f05ccce3eca2 SideKick parser for bibtex entries;
wenzelm
parents: 58524
diff changeset
   139
                  Token(Token.Kind.KEYWORD, "}")) => (id, toks)
f05ccce3eca2 SideKick parser for bibtex entries;
wenzelm
parents: 58524
diff changeset
   140
            case (Token(Token.Kind.IDENT, id) :: Token(Token.Kind.KEYWORD, "(") :: toks,
f05ccce3eca2 SideKick parser for bibtex entries;
wenzelm
parents: 58524
diff changeset
   141
                  Token(Token.Kind.KEYWORD, ")")) => (id, toks)
f05ccce3eca2 SideKick parser for bibtex entries;
wenzelm
parents: 58524
diff changeset
   142
            case _ => ("", Nil)
58523
937c479e62fe some support for bibtex files;
wenzelm
parents:
diff changeset
   143
          }
58526
f05ccce3eca2 SideKick parser for bibtex entries;
wenzelm
parents: 58524
diff changeset
   144
        case _ => ("", Nil)
f05ccce3eca2 SideKick parser for bibtex entries;
wenzelm
parents: 58524
diff changeset
   145
      }
f05ccce3eca2 SideKick parser for bibtex entries;
wenzelm
parents: 58524
diff changeset
   146
    override def kind: String = content._1
f05ccce3eca2 SideKick parser for bibtex entries;
wenzelm
parents: 58524
diff changeset
   147
    def content_tokens: List[Token] = content._2
f05ccce3eca2 SideKick parser for bibtex entries;
wenzelm
parents: 58524
diff changeset
   148
f05ccce3eca2 SideKick parser for bibtex entries;
wenzelm
parents: 58524
diff changeset
   149
    def is_wellformed: Boolean = kind != ""
f05ccce3eca2 SideKick parser for bibtex entries;
wenzelm
parents: 58524
diff changeset
   150
f05ccce3eca2 SideKick parser for bibtex entries;
wenzelm
parents: 58524
diff changeset
   151
    def name: String =
f05ccce3eca2 SideKick parser for bibtex entries;
wenzelm
parents: 58524
diff changeset
   152
      content_tokens match {
f05ccce3eca2 SideKick parser for bibtex entries;
wenzelm
parents: 58524
diff changeset
   153
        case Token(Token.Kind.IDENT, name) :: _ if is_wellformed => name
58523
937c479e62fe some support for bibtex files;
wenzelm
parents:
diff changeset
   154
        case _ => ""
937c479e62fe some support for bibtex files;
wenzelm
parents:
diff changeset
   155
      }
937c479e62fe some support for bibtex files;
wenzelm
parents:
diff changeset
   156
  }
937c479e62fe some support for bibtex files;
wenzelm
parents:
diff changeset
   157
937c479e62fe some support for bibtex files;
wenzelm
parents:
diff changeset
   158
937c479e62fe some support for bibtex files;
wenzelm
parents:
diff changeset
   159
937c479e62fe some support for bibtex files;
wenzelm
parents:
diff changeset
   160
  /** parsing **/
937c479e62fe some support for bibtex files;
wenzelm
parents:
diff changeset
   161
937c479e62fe some support for bibtex files;
wenzelm
parents:
diff changeset
   162
  // context of partial line-oriented scans
937c479e62fe some support for bibtex files;
wenzelm
parents:
diff changeset
   163
  abstract class Line_Context
937c479e62fe some support for bibtex files;
wenzelm
parents:
diff changeset
   164
  case class Delimited(quoted: Boolean, depth: Int) extends Line_Context
937c479e62fe some support for bibtex files;
wenzelm
parents:
diff changeset
   165
  val Finished = Delimited(false, 0)
937c479e62fe some support for bibtex files;
wenzelm
parents:
diff changeset
   166
937c479e62fe some support for bibtex files;
wenzelm
parents:
diff changeset
   167
  private def token(kind: Token.Kind.Value)(source: String): Token = Token(kind, source)
937c479e62fe some support for bibtex files;
wenzelm
parents:
diff changeset
   168
  private def keyword(source: String): Token = Token(Token.Kind.KEYWORD, source)
937c479e62fe some support for bibtex files;
wenzelm
parents:
diff changeset
   169
937c479e62fe some support for bibtex files;
wenzelm
parents:
diff changeset
   170
937c479e62fe some support for bibtex files;
wenzelm
parents:
diff changeset
   171
  // See also http://ctan.org/tex-archive/biblio/bibtex/base/bibtex.web
937c479e62fe some support for bibtex files;
wenzelm
parents:
diff changeset
   172
  // module @<Scan for and process a \.{.bib} command or database entry@>.
937c479e62fe some support for bibtex files;
wenzelm
parents:
diff changeset
   173
937c479e62fe some support for bibtex files;
wenzelm
parents:
diff changeset
   174
  object Parsers extends RegexParsers
937c479e62fe some support for bibtex files;
wenzelm
parents:
diff changeset
   175
  {
937c479e62fe some support for bibtex files;
wenzelm
parents:
diff changeset
   176
    /* white space and comments */
937c479e62fe some support for bibtex files;
wenzelm
parents:
diff changeset
   177
937c479e62fe some support for bibtex files;
wenzelm
parents:
diff changeset
   178
    override val whiteSpace = "".r
937c479e62fe some support for bibtex files;
wenzelm
parents:
diff changeset
   179
937c479e62fe some support for bibtex files;
wenzelm
parents:
diff changeset
   180
    private val space = """[ \t\n\r]+""".r ^^ token(Token.Kind.SPACE)
58527
4b190c763097 strict spaces for item_start: despite actual bibtex syntax, but in accordance to bibtex modes in Emacs and jEdit;
wenzelm
parents: 58526
diff changeset
   181
    private val strict_space = """[ \t]+""".r ^^ token(Token.Kind.SPACE)
58523
937c479e62fe some support for bibtex files;
wenzelm
parents:
diff changeset
   182
937c479e62fe some support for bibtex files;
wenzelm
parents:
diff changeset
   183
    private val ignored =
937c479e62fe some support for bibtex files;
wenzelm
parents:
diff changeset
   184
      rep1("""(?mi)([^@]+|@[ \t\n\r]*comment)""".r) ^^ { case ss => Ignored(ss.mkString) }
937c479e62fe some support for bibtex files;
wenzelm
parents:
diff changeset
   185
937c479e62fe some support for bibtex files;
wenzelm
parents:
diff changeset
   186
937c479e62fe some support for bibtex files;
wenzelm
parents:
diff changeset
   187
    /* delimited string: outermost "..." or {...} and body with balanced {...} */
937c479e62fe some support for bibtex files;
wenzelm
parents:
diff changeset
   188
937c479e62fe some support for bibtex files;
wenzelm
parents:
diff changeset
   189
    private def delimited_depth(delim: Delimited): Parser[(String, Delimited)] =
937c479e62fe some support for bibtex files;
wenzelm
parents:
diff changeset
   190
      new Parser[(String, Delimited)]
937c479e62fe some support for bibtex files;
wenzelm
parents:
diff changeset
   191
      {
937c479e62fe some support for bibtex files;
wenzelm
parents:
diff changeset
   192
        require(if (delim.quoted) delim.depth > 0 else delim.depth >= 0)
937c479e62fe some support for bibtex files;
wenzelm
parents:
diff changeset
   193
937c479e62fe some support for bibtex files;
wenzelm
parents:
diff changeset
   194
        def apply(in: Input) =
937c479e62fe some support for bibtex files;
wenzelm
parents:
diff changeset
   195
        {
937c479e62fe some support for bibtex files;
wenzelm
parents:
diff changeset
   196
          val start = in.offset
937c479e62fe some support for bibtex files;
wenzelm
parents:
diff changeset
   197
          val end = in.source.length
937c479e62fe some support for bibtex files;
wenzelm
parents:
diff changeset
   198
937c479e62fe some support for bibtex files;
wenzelm
parents:
diff changeset
   199
          var i = start
937c479e62fe some support for bibtex files;
wenzelm
parents:
diff changeset
   200
          var q = delim.quoted
937c479e62fe some support for bibtex files;
wenzelm
parents:
diff changeset
   201
          var d = delim.depth
937c479e62fe some support for bibtex files;
wenzelm
parents:
diff changeset
   202
          var finished = false
937c479e62fe some support for bibtex files;
wenzelm
parents:
diff changeset
   203
          while (!finished && i < end) {
937c479e62fe some support for bibtex files;
wenzelm
parents:
diff changeset
   204
            val c = in.source.charAt(i)
937c479e62fe some support for bibtex files;
wenzelm
parents:
diff changeset
   205
            if (c == '"' && d == 0) { i += 1; d = 1; q = true }
937c479e62fe some support for bibtex files;
wenzelm
parents:
diff changeset
   206
            else if (c == '"' && d == 1) { i += 1; d = 0; q = false; finished = true }
937c479e62fe some support for bibtex files;
wenzelm
parents:
diff changeset
   207
            else if (c == '{') { i += 1; d += 1 }
937c479e62fe some support for bibtex files;
wenzelm
parents:
diff changeset
   208
            else if (c == '}' && d > 0) { i += 1; d -= 1; if (d == 0) finished = true }
937c479e62fe some support for bibtex files;
wenzelm
parents:
diff changeset
   209
            else if (d > 0) i += 1
937c479e62fe some support for bibtex files;
wenzelm
parents:
diff changeset
   210
            else finished = true
937c479e62fe some support for bibtex files;
wenzelm
parents:
diff changeset
   211
          }
937c479e62fe some support for bibtex files;
wenzelm
parents:
diff changeset
   212
          if (i == start) Failure("bad input", in)
937c479e62fe some support for bibtex files;
wenzelm
parents:
diff changeset
   213
          else
937c479e62fe some support for bibtex files;
wenzelm
parents:
diff changeset
   214
            Success((in.source.subSequence(start, i).toString,
937c479e62fe some support for bibtex files;
wenzelm
parents:
diff changeset
   215
              Delimited(q, d)), in.drop(i - start))
937c479e62fe some support for bibtex files;
wenzelm
parents:
diff changeset
   216
        }
937c479e62fe some support for bibtex files;
wenzelm
parents:
diff changeset
   217
      }.named("delimited_depth")
937c479e62fe some support for bibtex files;
wenzelm
parents:
diff changeset
   218
937c479e62fe some support for bibtex files;
wenzelm
parents:
diff changeset
   219
    private def delimited: Parser[String] =
937c479e62fe some support for bibtex files;
wenzelm
parents:
diff changeset
   220
      delimited_depth(Finished) ^? { case (x, delim) if delim == Finished => x }
937c479e62fe some support for bibtex files;
wenzelm
parents:
diff changeset
   221
937c479e62fe some support for bibtex files;
wenzelm
parents:
diff changeset
   222
    private def delimited_line(ctxt: Line_Context): Parser[(String, Line_Context)] =
937c479e62fe some support for bibtex files;
wenzelm
parents:
diff changeset
   223
    {
937c479e62fe some support for bibtex files;
wenzelm
parents:
diff changeset
   224
      ctxt match {
937c479e62fe some support for bibtex files;
wenzelm
parents:
diff changeset
   225
        case delim: Delimited => delimited_depth(delim)
937c479e62fe some support for bibtex files;
wenzelm
parents:
diff changeset
   226
        case _ => failure("")
937c479e62fe some support for bibtex files;
wenzelm
parents:
diff changeset
   227
      }
937c479e62fe some support for bibtex files;
wenzelm
parents:
diff changeset
   228
    }
937c479e62fe some support for bibtex files;
wenzelm
parents:
diff changeset
   229
937c479e62fe some support for bibtex files;
wenzelm
parents:
diff changeset
   230
    private val recover_delimited: Parser[String] =
937c479e62fe some support for bibtex files;
wenzelm
parents:
diff changeset
   231
      delimited_depth(Finished) ^^ (_._1)
937c479e62fe some support for bibtex files;
wenzelm
parents:
diff changeset
   232
937c479e62fe some support for bibtex files;
wenzelm
parents:
diff changeset
   233
    private val delimited_token =
937c479e62fe some support for bibtex files;
wenzelm
parents:
diff changeset
   234
      delimited ^^ token(Token.Kind.STRING) |
937c479e62fe some support for bibtex files;
wenzelm
parents:
diff changeset
   235
      recover_delimited ^^ token(Token.Kind.ERROR)
937c479e62fe some support for bibtex files;
wenzelm
parents:
diff changeset
   236
937c479e62fe some support for bibtex files;
wenzelm
parents:
diff changeset
   237
937c479e62fe some support for bibtex files;
wenzelm
parents:
diff changeset
   238
    /* other tokens */
937c479e62fe some support for bibtex files;
wenzelm
parents:
diff changeset
   239
937c479e62fe some support for bibtex files;
wenzelm
parents:
diff changeset
   240
    private val at = "@" ^^ keyword
937c479e62fe some support for bibtex files;
wenzelm
parents:
diff changeset
   241
    private val left_brace = "{" ^^ keyword
937c479e62fe some support for bibtex files;
wenzelm
parents:
diff changeset
   242
    private val right_brace = "}" ^^ keyword
937c479e62fe some support for bibtex files;
wenzelm
parents:
diff changeset
   243
    private val left_paren = "(" ^^ keyword
937c479e62fe some support for bibtex files;
wenzelm
parents:
diff changeset
   244
    private val right_paren = ")" ^^ keyword
937c479e62fe some support for bibtex files;
wenzelm
parents:
diff changeset
   245
937c479e62fe some support for bibtex files;
wenzelm
parents:
diff changeset
   246
    private val nat = "[0-9]+".r ^^ token(Token.Kind.NAT)
937c479e62fe some support for bibtex files;
wenzelm
parents:
diff changeset
   247
937c479e62fe some support for bibtex files;
wenzelm
parents:
diff changeset
   248
    private val ident =
937c479e62fe some support for bibtex files;
wenzelm
parents:
diff changeset
   249
      """[\x21-\x7f&&[^"#%'(),={}0-9]][\x21-\x7f&&[^"#%'(),={}]]*""".r ^^ token(Token.Kind.IDENT)
937c479e62fe some support for bibtex files;
wenzelm
parents:
diff changeset
   250
937c479e62fe some support for bibtex files;
wenzelm
parents:
diff changeset
   251
937c479e62fe some support for bibtex files;
wenzelm
parents:
diff changeset
   252
    /* chunks */
937c479e62fe some support for bibtex files;
wenzelm
parents:
diff changeset
   253
937c479e62fe some support for bibtex files;
wenzelm
parents:
diff changeset
   254
    private val item_start =
58527
4b190c763097 strict spaces for item_start: despite actual bibtex syntax, but in accordance to bibtex modes in Emacs and jEdit;
wenzelm
parents: 58526
diff changeset
   255
      at ~ rep(strict_space) ~ ident ~ rep(strict_space) ^^
58523
937c479e62fe some support for bibtex files;
wenzelm
parents:
diff changeset
   256
        { case a ~ b ~ c ~ d => List(a) ::: b ::: List(c) ::: d }
937c479e62fe some support for bibtex files;
wenzelm
parents:
diff changeset
   257
937c479e62fe some support for bibtex files;
wenzelm
parents:
diff changeset
   258
    private val body_token = delimited_token | ("[=#,]".r ^^ keyword | (nat | (ident | space)))
937c479e62fe some support for bibtex files;
wenzelm
parents:
diff changeset
   259
937c479e62fe some support for bibtex files;
wenzelm
parents:
diff changeset
   260
    private val item =
937c479e62fe some support for bibtex files;
wenzelm
parents:
diff changeset
   261
      (item_start ~ left_brace ~ rep(body_token) ~ opt(right_brace) |
937c479e62fe some support for bibtex files;
wenzelm
parents:
diff changeset
   262
       item_start ~ left_paren ~ rep(body_token) ~ opt(right_paren)) ^^
937c479e62fe some support for bibtex files;
wenzelm
parents:
diff changeset
   263
        { case a ~ b ~ c ~ d => Item(a ::: List(b) ::: c ::: d.toList) }
937c479e62fe some support for bibtex files;
wenzelm
parents:
diff changeset
   264
937c479e62fe some support for bibtex files;
wenzelm
parents:
diff changeset
   265
    private val recover_item = "(?m)@[^@]+".r ^^ (s => Malformed(s))
937c479e62fe some support for bibtex files;
wenzelm
parents:
diff changeset
   266
937c479e62fe some support for bibtex files;
wenzelm
parents:
diff changeset
   267
    val chunks: Parser[List[Chunk]] = rep(ignored | (item | recover_item))
937c479e62fe some support for bibtex files;
wenzelm
parents:
diff changeset
   268
  }
937c479e62fe some support for bibtex files;
wenzelm
parents:
diff changeset
   269
937c479e62fe some support for bibtex files;
wenzelm
parents:
diff changeset
   270
  def parse(input: CharSequence): List[Chunk] =
937c479e62fe some support for bibtex files;
wenzelm
parents:
diff changeset
   271
  {
937c479e62fe some support for bibtex files;
wenzelm
parents:
diff changeset
   272
    val in: Reader[Char] = new CharSequenceReader(input)
937c479e62fe some support for bibtex files;
wenzelm
parents:
diff changeset
   273
    Parsers.parseAll(Parsers.chunks, in) match {
937c479e62fe some support for bibtex files;
wenzelm
parents:
diff changeset
   274
      case Parsers.Success(result, _) => result
58526
f05ccce3eca2 SideKick parser for bibtex entries;
wenzelm
parents: 58524
diff changeset
   275
      case _ => error("Unexpected failure to parse input:\n" + input.toString)
58523
937c479e62fe some support for bibtex files;
wenzelm
parents:
diff changeset
   276
    }
937c479e62fe some support for bibtex files;
wenzelm
parents:
diff changeset
   277
  }
937c479e62fe some support for bibtex files;
wenzelm
parents:
diff changeset
   278
}
937c479e62fe some support for bibtex files;
wenzelm
parents:
diff changeset
   279