src/Pure/Tools/bibtex.scala
author wenzelm
Fri, 03 Oct 2014 23:33:47 +0200
changeset 58528 7d6b8f8893e8
parent 58527 4b190c763097
child 58529 cd4439d8799c
permissions -rw-r--r--
more explicit item kind; clarified recover_delimited: skip to next @; support for line context parsing; tuned signature;
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
58528
7d6b8f8893e8 more explicit item kind;
wenzelm
parents: 58527
diff changeset
    10
import scala.collection.mutable
58523
937c479e62fe some support for bibtex files;
wenzelm
parents:
diff changeset
    11
import scala.util.parsing.input.{Reader, CharSequenceReader}
937c479e62fe some support for bibtex files;
wenzelm
parents:
diff changeset
    12
import scala.util.parsing.combinator.RegexParsers
937c479e62fe some support for bibtex files;
wenzelm
parents:
diff changeset
    13
937c479e62fe some support for bibtex files;
wenzelm
parents:
diff changeset
    14
937c479e62fe some support for bibtex files;
wenzelm
parents:
diff changeset
    15
object Bibtex
937c479e62fe some support for bibtex files;
wenzelm
parents:
diff changeset
    16
{
937c479e62fe some support for bibtex files;
wenzelm
parents:
diff changeset
    17
  /** content **/
937c479e62fe some support for bibtex files;
wenzelm
parents:
diff changeset
    18
937c479e62fe some support for bibtex files;
wenzelm
parents:
diff changeset
    19
  val months = List(
937c479e62fe some support for bibtex files;
wenzelm
parents:
diff changeset
    20
    "jan",
937c479e62fe some support for bibtex files;
wenzelm
parents:
diff changeset
    21
    "feb",
937c479e62fe some support for bibtex files;
wenzelm
parents:
diff changeset
    22
    "mar",
937c479e62fe some support for bibtex files;
wenzelm
parents:
diff changeset
    23
    "apr",
937c479e62fe some support for bibtex files;
wenzelm
parents:
diff changeset
    24
    "may",
937c479e62fe some support for bibtex files;
wenzelm
parents:
diff changeset
    25
    "jun",
937c479e62fe some support for bibtex files;
wenzelm
parents:
diff changeset
    26
    "jul",
937c479e62fe some support for bibtex files;
wenzelm
parents:
diff changeset
    27
    "aug",
937c479e62fe some support for bibtex files;
wenzelm
parents:
diff changeset
    28
    "sep",
937c479e62fe some support for bibtex files;
wenzelm
parents:
diff changeset
    29
    "oct",
937c479e62fe some support for bibtex files;
wenzelm
parents:
diff changeset
    30
    "nov",
937c479e62fe some support for bibtex files;
wenzelm
parents:
diff changeset
    31
    "dec")
937c479e62fe some support for bibtex files;
wenzelm
parents:
diff changeset
    32
937c479e62fe some support for bibtex files;
wenzelm
parents:
diff changeset
    33
  val commands = List("preamble", "string")
937c479e62fe some support for bibtex files;
wenzelm
parents:
diff changeset
    34
58524
f805b366a497 context menu for bibtex entries;
wenzelm
parents: 58523
diff changeset
    35
  sealed case class Entry(
58526
f05ccce3eca2 SideKick parser for bibtex entries;
wenzelm
parents: 58524
diff changeset
    36
    kind: String,
58523
937c479e62fe some support for bibtex files;
wenzelm
parents:
diff changeset
    37
    required: List[String],
937c479e62fe some support for bibtex files;
wenzelm
parents:
diff changeset
    38
    optional_crossref: List[String],
937c479e62fe some support for bibtex files;
wenzelm
parents:
diff changeset
    39
    optional: List[String])
58524
f805b366a497 context menu for bibtex entries;
wenzelm
parents: 58523
diff changeset
    40
  {
f805b366a497 context menu for bibtex entries;
wenzelm
parents: 58523
diff changeset
    41
    def fields: List[String] = required ::: optional_crossref ::: optional
f805b366a497 context menu for bibtex entries;
wenzelm
parents: 58523
diff changeset
    42
    def template: String =
58526
f05ccce3eca2 SideKick parser for bibtex entries;
wenzelm
parents: 58524
diff changeset
    43
      "@" + kind + "{,\n" + fields.map(x => "  " + x + " = {},\n").mkString + "}\n"
58524
f805b366a497 context menu for bibtex entries;
wenzelm
parents: 58523
diff changeset
    44
  }
58523
937c479e62fe some support for bibtex files;
wenzelm
parents:
diff changeset
    45
58524
f805b366a497 context menu for bibtex entries;
wenzelm
parents: 58523
diff changeset
    46
  val entries: List[Entry] =
f805b366a497 context menu for bibtex entries;
wenzelm
parents: 58523
diff changeset
    47
    List(
f805b366a497 context menu for bibtex entries;
wenzelm
parents: 58523
diff changeset
    48
      Entry("Article",
f805b366a497 context menu for bibtex entries;
wenzelm
parents: 58523
diff changeset
    49
        List("author", "title"),
f805b366a497 context menu for bibtex entries;
wenzelm
parents: 58523
diff changeset
    50
        List("journal", "year"),
f805b366a497 context menu for bibtex entries;
wenzelm
parents: 58523
diff changeset
    51
        List("volume", "number", "pages", "month", "note")),
f805b366a497 context menu for bibtex entries;
wenzelm
parents: 58523
diff changeset
    52
      Entry("InProceedings",
f805b366a497 context menu for bibtex entries;
wenzelm
parents: 58523
diff changeset
    53
        List("author", "title"),
f805b366a497 context menu for bibtex entries;
wenzelm
parents: 58523
diff changeset
    54
        List("booktitle", "year"),
f805b366a497 context menu for bibtex entries;
wenzelm
parents: 58523
diff changeset
    55
        List("editor", "volume", "number", "series", "pages", "month", "address",
f805b366a497 context menu for bibtex entries;
wenzelm
parents: 58523
diff changeset
    56
          "organization", "publisher", "note")),
f805b366a497 context menu for bibtex entries;
wenzelm
parents: 58523
diff changeset
    57
      Entry("InCollection",
f805b366a497 context menu for bibtex entries;
wenzelm
parents: 58523
diff changeset
    58
        List("author", "title", "booktitle"),
f805b366a497 context menu for bibtex entries;
wenzelm
parents: 58523
diff changeset
    59
        List("publisher", "year"),
f805b366a497 context menu for bibtex entries;
wenzelm
parents: 58523
diff changeset
    60
        List("editor", "volume", "number", "series", "type", "chapter", "pages",
f805b366a497 context menu for bibtex entries;
wenzelm
parents: 58523
diff changeset
    61
          "edition", "month", "address", "note")),
f805b366a497 context menu for bibtex entries;
wenzelm
parents: 58523
diff changeset
    62
      Entry("InBook",
f805b366a497 context menu for bibtex entries;
wenzelm
parents: 58523
diff changeset
    63
        List("author", "editor", "title", "chapter"),
f805b366a497 context menu for bibtex entries;
wenzelm
parents: 58523
diff changeset
    64
        List("publisher", "year"),
f805b366a497 context menu for bibtex entries;
wenzelm
parents: 58523
diff changeset
    65
        List("volume", "number", "series", "type", "address", "edition", "month", "pages", "note")),
f805b366a497 context menu for bibtex entries;
wenzelm
parents: 58523
diff changeset
    66
      Entry("Proceedings",
f805b366a497 context menu for bibtex entries;
wenzelm
parents: 58523
diff changeset
    67
        List("title", "year"),
f805b366a497 context menu for bibtex entries;
wenzelm
parents: 58523
diff changeset
    68
        List(),
f805b366a497 context menu for bibtex entries;
wenzelm
parents: 58523
diff changeset
    69
        List("booktitle", "editor", "volume", "number", "series", "address", "month",
f805b366a497 context menu for bibtex entries;
wenzelm
parents: 58523
diff changeset
    70
          "organization", "publisher", "note")),
f805b366a497 context menu for bibtex entries;
wenzelm
parents: 58523
diff changeset
    71
      Entry("Book",
f805b366a497 context menu for bibtex entries;
wenzelm
parents: 58523
diff changeset
    72
        List("author", "editor", "title"),
f805b366a497 context menu for bibtex entries;
wenzelm
parents: 58523
diff changeset
    73
        List("publisher", "year"),
f805b366a497 context menu for bibtex entries;
wenzelm
parents: 58523
diff changeset
    74
        List("volume", "number", "series", "address", "edition", "month", "note")),
f805b366a497 context menu for bibtex entries;
wenzelm
parents: 58523
diff changeset
    75
      Entry("Booklet",
f805b366a497 context menu for bibtex entries;
wenzelm
parents: 58523
diff changeset
    76
        List("title"),
f805b366a497 context menu for bibtex entries;
wenzelm
parents: 58523
diff changeset
    77
        List(),
f805b366a497 context menu for bibtex entries;
wenzelm
parents: 58523
diff changeset
    78
        List("author", "howpublished", "address", "month", "year", "note")),
f805b366a497 context menu for bibtex entries;
wenzelm
parents: 58523
diff changeset
    79
      Entry("PhdThesis",
f805b366a497 context menu for bibtex entries;
wenzelm
parents: 58523
diff changeset
    80
        List("author", "title", "school", "year"),
f805b366a497 context menu for bibtex entries;
wenzelm
parents: 58523
diff changeset
    81
        List(),
f805b366a497 context menu for bibtex entries;
wenzelm
parents: 58523
diff changeset
    82
        List("type", "address", "month", "note")),
f805b366a497 context menu for bibtex entries;
wenzelm
parents: 58523
diff changeset
    83
      Entry("MastersThesis",
f805b366a497 context menu for bibtex entries;
wenzelm
parents: 58523
diff changeset
    84
        List("author", "title", "school", "year"),
f805b366a497 context menu for bibtex entries;
wenzelm
parents: 58523
diff changeset
    85
        List(),
f805b366a497 context menu for bibtex entries;
wenzelm
parents: 58523
diff changeset
    86
        List("type", "address", "month", "note")),
f805b366a497 context menu for bibtex entries;
wenzelm
parents: 58523
diff changeset
    87
      Entry("TechReport",
f805b366a497 context menu for bibtex entries;
wenzelm
parents: 58523
diff changeset
    88
        List("author", "title", "institution", "year"),
f805b366a497 context menu for bibtex entries;
wenzelm
parents: 58523
diff changeset
    89
        List(),
f805b366a497 context menu for bibtex entries;
wenzelm
parents: 58523
diff changeset
    90
        List("type", "number", "address", "month", "note")),
f805b366a497 context menu for bibtex entries;
wenzelm
parents: 58523
diff changeset
    91
      Entry("Manual",
f805b366a497 context menu for bibtex entries;
wenzelm
parents: 58523
diff changeset
    92
        List("title"),
f805b366a497 context menu for bibtex entries;
wenzelm
parents: 58523
diff changeset
    93
        List(),
f805b366a497 context menu for bibtex entries;
wenzelm
parents: 58523
diff changeset
    94
        List("author", "organization", "address", "edition", "month", "year", "note")),
f805b366a497 context menu for bibtex entries;
wenzelm
parents: 58523
diff changeset
    95
      Entry("Unpublished",
f805b366a497 context menu for bibtex entries;
wenzelm
parents: 58523
diff changeset
    96
        List("author", "title", "note"),
f805b366a497 context menu for bibtex entries;
wenzelm
parents: 58523
diff changeset
    97
        List(),
f805b366a497 context menu for bibtex entries;
wenzelm
parents: 58523
diff changeset
    98
        List("month", "year")),
f805b366a497 context menu for bibtex entries;
wenzelm
parents: 58523
diff changeset
    99
      Entry("Misc",
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(),
f805b366a497 context menu for bibtex entries;
wenzelm
parents: 58523
diff changeset
   102
        List("author", "title", "howpublished", "month", "year", "note")))
58523
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
937c479e62fe some support for bibtex files;
wenzelm
parents:
diff changeset
   106
  /** tokens and chunks **/
937c479e62fe some support for bibtex files;
wenzelm
parents:
diff changeset
   107
937c479e62fe some support for bibtex files;
wenzelm
parents:
diff changeset
   108
  object Token
937c479e62fe some support for bibtex files;
wenzelm
parents:
diff changeset
   109
  {
937c479e62fe some support for bibtex files;
wenzelm
parents:
diff changeset
   110
    object Kind extends Enumeration
937c479e62fe some support for bibtex files;
wenzelm
parents:
diff changeset
   111
    {
937c479e62fe some support for bibtex files;
wenzelm
parents:
diff changeset
   112
      val KEYWORD = Value("keyword")
937c479e62fe some support for bibtex files;
wenzelm
parents:
diff changeset
   113
      val NAT = Value("natural number")
937c479e62fe some support for bibtex files;
wenzelm
parents:
diff changeset
   114
      val IDENT = Value("identifier")
937c479e62fe some support for bibtex files;
wenzelm
parents:
diff changeset
   115
      val STRING = Value("string")
937c479e62fe some support for bibtex files;
wenzelm
parents:
diff changeset
   116
      val SPACE = Value("white space")
937c479e62fe some support for bibtex files;
wenzelm
parents:
diff changeset
   117
      val ERROR = Value("bad input")
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
937c479e62fe some support for bibtex files;
wenzelm
parents:
diff changeset
   121
  sealed case class Token(kind: Token.Kind.Value, val source: String)
937c479e62fe some support for bibtex files;
wenzelm
parents:
diff changeset
   122
  {
937c479e62fe some support for bibtex files;
wenzelm
parents:
diff changeset
   123
    def is_space: Boolean = kind == Token.Kind.SPACE
937c479e62fe some support for bibtex files;
wenzelm
parents:
diff changeset
   124
    def is_error: Boolean = kind == Token.Kind.ERROR
937c479e62fe some support for bibtex files;
wenzelm
parents:
diff changeset
   125
  }
937c479e62fe some support for bibtex files;
wenzelm
parents:
diff changeset
   126
58528
7d6b8f8893e8 more explicit item kind;
wenzelm
parents: 58527
diff changeset
   127
  abstract class Chunk
7d6b8f8893e8 more explicit item kind;
wenzelm
parents: 58527
diff changeset
   128
  {
7d6b8f8893e8 more explicit item kind;
wenzelm
parents: 58527
diff changeset
   129
    def size: Int
7d6b8f8893e8 more explicit item kind;
wenzelm
parents: 58527
diff changeset
   130
    def kind: String
7d6b8f8893e8 more explicit item kind;
wenzelm
parents: 58527
diff changeset
   131
  }
7d6b8f8893e8 more explicit item kind;
wenzelm
parents: 58527
diff changeset
   132
7d6b8f8893e8 more explicit item kind;
wenzelm
parents: 58527
diff changeset
   133
  case class Ignored(source: String) extends Chunk
7d6b8f8893e8 more explicit item kind;
wenzelm
parents: 58527
diff changeset
   134
  {
7d6b8f8893e8 more explicit item kind;
wenzelm
parents: 58527
diff changeset
   135
    def size: Int = source.size
7d6b8f8893e8 more explicit item kind;
wenzelm
parents: 58527
diff changeset
   136
    def kind: String = ""
7d6b8f8893e8 more explicit item kind;
wenzelm
parents: 58527
diff changeset
   137
  }
7d6b8f8893e8 more explicit item kind;
wenzelm
parents: 58527
diff changeset
   138
7d6b8f8893e8 more explicit item kind;
wenzelm
parents: 58527
diff changeset
   139
  case class Item(kind: String, tokens: List[Token]) extends Chunk
58523
937c479e62fe some support for bibtex files;
wenzelm
parents:
diff changeset
   140
  {
58526
f05ccce3eca2 SideKick parser for bibtex entries;
wenzelm
parents: 58524
diff changeset
   141
    def size: Int = (0 /: tokens)({ case (n, token) => n + token.source.size })
f05ccce3eca2 SideKick parser for bibtex entries;
wenzelm
parents: 58524
diff changeset
   142
58528
7d6b8f8893e8 more explicit item kind;
wenzelm
parents: 58527
diff changeset
   143
    private val wellformed_content: Option[List[Token]] =
58523
937c479e62fe some support for bibtex files;
wenzelm
parents:
diff changeset
   144
      tokens match {
937c479e62fe some support for bibtex files;
wenzelm
parents:
diff changeset
   145
        case Token(Token.Kind.KEYWORD, "@") :: body
937c479e62fe some support for bibtex files;
wenzelm
parents:
diff changeset
   146
        if !body.isEmpty && !body.exists(_.is_error) =>
58526
f05ccce3eca2 SideKick parser for bibtex entries;
wenzelm
parents: 58524
diff changeset
   147
          (body.init.filterNot(_.is_space), body.last) match {
58528
7d6b8f8893e8 more explicit item kind;
wenzelm
parents: 58527
diff changeset
   148
            case (Token(Token.Kind.IDENT, _) :: Token(Token.Kind.KEYWORD, "{") :: toks,
7d6b8f8893e8 more explicit item kind;
wenzelm
parents: 58527
diff changeset
   149
                  Token(Token.Kind.KEYWORD, "}")) => Some(toks)
7d6b8f8893e8 more explicit item kind;
wenzelm
parents: 58527
diff changeset
   150
            case (Token(Token.Kind.IDENT, _) :: Token(Token.Kind.KEYWORD, "(") :: toks,
7d6b8f8893e8 more explicit item kind;
wenzelm
parents: 58527
diff changeset
   151
                  Token(Token.Kind.KEYWORD, ")")) => Some(toks)
7d6b8f8893e8 more explicit item kind;
wenzelm
parents: 58527
diff changeset
   152
            case _ => None
58523
937c479e62fe some support for bibtex files;
wenzelm
parents:
diff changeset
   153
          }
58528
7d6b8f8893e8 more explicit item kind;
wenzelm
parents: 58527
diff changeset
   154
        case _ => None
58526
f05ccce3eca2 SideKick parser for bibtex entries;
wenzelm
parents: 58524
diff changeset
   155
      }
58528
7d6b8f8893e8 more explicit item kind;
wenzelm
parents: 58527
diff changeset
   156
    def is_wellformed: Boolean = kind != "" && wellformed_content.isDefined
7d6b8f8893e8 more explicit item kind;
wenzelm
parents: 58527
diff changeset
   157
    def content_tokens: List[Token] = wellformed_content getOrElse Nil
58526
f05ccce3eca2 SideKick parser for bibtex entries;
wenzelm
parents: 58524
diff changeset
   158
f05ccce3eca2 SideKick parser for bibtex entries;
wenzelm
parents: 58524
diff changeset
   159
    def name: String =
f05ccce3eca2 SideKick parser for bibtex entries;
wenzelm
parents: 58524
diff changeset
   160
      content_tokens match {
58528
7d6b8f8893e8 more explicit item kind;
wenzelm
parents: 58527
diff changeset
   161
        case Token(Token.Kind.IDENT, id) :: _ if is_wellformed => id
58523
937c479e62fe some support for bibtex files;
wenzelm
parents:
diff changeset
   162
        case _ => ""
937c479e62fe some support for bibtex files;
wenzelm
parents:
diff changeset
   163
      }
937c479e62fe some support for bibtex files;
wenzelm
parents:
diff changeset
   164
  }
937c479e62fe some support for bibtex files;
wenzelm
parents:
diff changeset
   165
937c479e62fe some support for bibtex files;
wenzelm
parents:
diff changeset
   166
937c479e62fe some support for bibtex files;
wenzelm
parents:
diff changeset
   167
937c479e62fe some support for bibtex files;
wenzelm
parents:
diff changeset
   168
  /** parsing **/
937c479e62fe some support for bibtex files;
wenzelm
parents:
diff changeset
   169
937c479e62fe some support for bibtex files;
wenzelm
parents:
diff changeset
   170
  // context of partial line-oriented scans
937c479e62fe some support for bibtex files;
wenzelm
parents:
diff changeset
   171
  abstract class Line_Context
58528
7d6b8f8893e8 more explicit item kind;
wenzelm
parents: 58527
diff changeset
   172
  case object Ignored_Context extends Line_Context
7d6b8f8893e8 more explicit item kind;
wenzelm
parents: 58527
diff changeset
   173
  case class Item_Context(kind: String, delim: Delimited, right: String) extends Line_Context
7d6b8f8893e8 more explicit item kind;
wenzelm
parents: 58527
diff changeset
   174
  case class Delimited(quoted: Boolean, depth: Int)
7d6b8f8893e8 more explicit item kind;
wenzelm
parents: 58527
diff changeset
   175
  val Closed = Delimited(false, 0)
58523
937c479e62fe some support for bibtex files;
wenzelm
parents:
diff changeset
   176
937c479e62fe some support for bibtex files;
wenzelm
parents:
diff changeset
   177
  private def token(kind: Token.Kind.Value)(source: String): Token = Token(kind, source)
937c479e62fe some support for bibtex files;
wenzelm
parents:
diff changeset
   178
  private def keyword(source: String): Token = Token(Token.Kind.KEYWORD, source)
937c479e62fe some support for bibtex files;
wenzelm
parents:
diff changeset
   179
937c479e62fe some support for bibtex files;
wenzelm
parents:
diff changeset
   180
937c479e62fe some support for bibtex files;
wenzelm
parents:
diff changeset
   181
  // See also http://ctan.org/tex-archive/biblio/bibtex/base/bibtex.web
937c479e62fe some support for bibtex files;
wenzelm
parents:
diff changeset
   182
  // module @<Scan for and process a \.{.bib} command or database entry@>.
937c479e62fe some support for bibtex files;
wenzelm
parents:
diff changeset
   183
937c479e62fe some support for bibtex files;
wenzelm
parents:
diff changeset
   184
  object Parsers extends RegexParsers
937c479e62fe some support for bibtex files;
wenzelm
parents:
diff changeset
   185
  {
937c479e62fe some support for bibtex files;
wenzelm
parents:
diff changeset
   186
    /* white space and comments */
937c479e62fe some support for bibtex files;
wenzelm
parents:
diff changeset
   187
937c479e62fe some support for bibtex files;
wenzelm
parents:
diff changeset
   188
    override val whiteSpace = "".r
937c479e62fe some support for bibtex files;
wenzelm
parents:
diff changeset
   189
937c479e62fe some support for bibtex files;
wenzelm
parents:
diff changeset
   190
    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
   191
    private val strict_space = """[ \t]+""".r ^^ token(Token.Kind.SPACE)
58523
937c479e62fe some support for bibtex files;
wenzelm
parents:
diff changeset
   192
58528
7d6b8f8893e8 more explicit item kind;
wenzelm
parents: 58527
diff changeset
   193
7d6b8f8893e8 more explicit item kind;
wenzelm
parents: 58527
diff changeset
   194
    /* ignored material outside items */
7d6b8f8893e8 more explicit item kind;
wenzelm
parents: 58527
diff changeset
   195
7d6b8f8893e8 more explicit item kind;
wenzelm
parents: 58527
diff changeset
   196
    private val ignored: Parser[Chunk] =
7d6b8f8893e8 more explicit item kind;
wenzelm
parents: 58527
diff changeset
   197
      rep1("""(?mi)([^@]+|@[ \t]*comment)""".r) ^^ { case ss => Ignored(ss.mkString) }
7d6b8f8893e8 more explicit item kind;
wenzelm
parents: 58527
diff changeset
   198
7d6b8f8893e8 more explicit item kind;
wenzelm
parents: 58527
diff changeset
   199
    private def ignored_line(ctxt: Line_Context): Parser[(Chunk, Line_Context)] =
7d6b8f8893e8 more explicit item kind;
wenzelm
parents: 58527
diff changeset
   200
      ctxt match {
7d6b8f8893e8 more explicit item kind;
wenzelm
parents: 58527
diff changeset
   201
        case Ignored_Context => ignored ^^ { case a => (a, ctxt) }
7d6b8f8893e8 more explicit item kind;
wenzelm
parents: 58527
diff changeset
   202
        case _ => failure("")
7d6b8f8893e8 more explicit item kind;
wenzelm
parents: 58527
diff changeset
   203
      }
58523
937c479e62fe some support for bibtex files;
wenzelm
parents:
diff changeset
   204
937c479e62fe some support for bibtex files;
wenzelm
parents:
diff changeset
   205
937c479e62fe some support for bibtex files;
wenzelm
parents:
diff changeset
   206
    /* delimited string: outermost "..." or {...} and body with balanced {...} */
937c479e62fe some support for bibtex files;
wenzelm
parents:
diff changeset
   207
937c479e62fe some support for bibtex files;
wenzelm
parents:
diff changeset
   208
    private def delimited_depth(delim: Delimited): Parser[(String, Delimited)] =
937c479e62fe some support for bibtex files;
wenzelm
parents:
diff changeset
   209
      new Parser[(String, Delimited)]
937c479e62fe some support for bibtex files;
wenzelm
parents:
diff changeset
   210
      {
937c479e62fe some support for bibtex files;
wenzelm
parents:
diff changeset
   211
        require(if (delim.quoted) delim.depth > 0 else delim.depth >= 0)
937c479e62fe some support for bibtex files;
wenzelm
parents:
diff changeset
   212
937c479e62fe some support for bibtex files;
wenzelm
parents:
diff changeset
   213
        def apply(in: Input) =
937c479e62fe some support for bibtex files;
wenzelm
parents:
diff changeset
   214
        {
937c479e62fe some support for bibtex files;
wenzelm
parents:
diff changeset
   215
          val start = in.offset
937c479e62fe some support for bibtex files;
wenzelm
parents:
diff changeset
   216
          val end = in.source.length
937c479e62fe some support for bibtex files;
wenzelm
parents:
diff changeset
   217
937c479e62fe some support for bibtex files;
wenzelm
parents:
diff changeset
   218
          var i = start
937c479e62fe some support for bibtex files;
wenzelm
parents:
diff changeset
   219
          var q = delim.quoted
937c479e62fe some support for bibtex files;
wenzelm
parents:
diff changeset
   220
          var d = delim.depth
937c479e62fe some support for bibtex files;
wenzelm
parents:
diff changeset
   221
          var finished = false
937c479e62fe some support for bibtex files;
wenzelm
parents:
diff changeset
   222
          while (!finished && i < end) {
937c479e62fe some support for bibtex files;
wenzelm
parents:
diff changeset
   223
            val c = in.source.charAt(i)
937c479e62fe some support for bibtex files;
wenzelm
parents:
diff changeset
   224
            if (c == '"' && d == 0) { i += 1; d = 1; q = true }
937c479e62fe some support for bibtex files;
wenzelm
parents:
diff changeset
   225
            else if (c == '"' && d == 1) { i += 1; d = 0; q = false; finished = true }
937c479e62fe some support for bibtex files;
wenzelm
parents:
diff changeset
   226
            else if (c == '{') { i += 1; d += 1 }
937c479e62fe some support for bibtex files;
wenzelm
parents:
diff changeset
   227
            else if (c == '}' && d > 0) { i += 1; d -= 1; if (d == 0) finished = true }
937c479e62fe some support for bibtex files;
wenzelm
parents:
diff changeset
   228
            else if (d > 0) i += 1
937c479e62fe some support for bibtex files;
wenzelm
parents:
diff changeset
   229
            else finished = true
937c479e62fe some support for bibtex files;
wenzelm
parents:
diff changeset
   230
          }
937c479e62fe some support for bibtex files;
wenzelm
parents:
diff changeset
   231
          if (i == start) Failure("bad input", in)
58528
7d6b8f8893e8 more explicit item kind;
wenzelm
parents: 58527
diff changeset
   232
          else {
7d6b8f8893e8 more explicit item kind;
wenzelm
parents: 58527
diff changeset
   233
            val s = in.source.subSequence(start, i).toString
7d6b8f8893e8 more explicit item kind;
wenzelm
parents: 58527
diff changeset
   234
            Success((s, Delimited(q, d)), in.drop(i - start))
7d6b8f8893e8 more explicit item kind;
wenzelm
parents: 58527
diff changeset
   235
          }
58523
937c479e62fe some support for bibtex files;
wenzelm
parents:
diff changeset
   236
        }
937c479e62fe some support for bibtex files;
wenzelm
parents:
diff changeset
   237
      }.named("delimited_depth")
937c479e62fe some support for bibtex files;
wenzelm
parents:
diff changeset
   238
58528
7d6b8f8893e8 more explicit item kind;
wenzelm
parents: 58527
diff changeset
   239
    private def delimited: Parser[Token] =
7d6b8f8893e8 more explicit item kind;
wenzelm
parents: 58527
diff changeset
   240
      delimited_depth(Closed) ^?
7d6b8f8893e8 more explicit item kind;
wenzelm
parents: 58527
diff changeset
   241
        { case (s, delim) if delim == Closed => Token(Token.Kind.STRING, s) }
58523
937c479e62fe some support for bibtex files;
wenzelm
parents:
diff changeset
   242
58528
7d6b8f8893e8 more explicit item kind;
wenzelm
parents: 58527
diff changeset
   243
    private def delimited_line(ctxt: Line_Context): Parser[(Item, Line_Context)] =
58523
937c479e62fe some support for bibtex files;
wenzelm
parents:
diff changeset
   244
    {
937c479e62fe some support for bibtex files;
wenzelm
parents:
diff changeset
   245
      ctxt match {
58528
7d6b8f8893e8 more explicit item kind;
wenzelm
parents: 58527
diff changeset
   246
        case Item_Context(kind, delim, right) =>
7d6b8f8893e8 more explicit item kind;
wenzelm
parents: 58527
diff changeset
   247
          delimited_depth(delim) ^^ { case (s, delim1) =>
7d6b8f8893e8 more explicit item kind;
wenzelm
parents: 58527
diff changeset
   248
            (Item(kind, List(Token(Token.Kind.STRING, s))), Item_Context(kind, delim1, right)) }
58523
937c479e62fe some support for bibtex files;
wenzelm
parents:
diff changeset
   249
        case _ => failure("")
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
58528
7d6b8f8893e8 more explicit item kind;
wenzelm
parents: 58527
diff changeset
   253
    private def recover_delimited: Parser[Token] =
7d6b8f8893e8 more explicit item kind;
wenzelm
parents: 58527
diff changeset
   254
      """(?m)["{][^@]+""".r ^^ token(Token.Kind.ERROR)
58523
937c479e62fe some support for bibtex files;
wenzelm
parents:
diff changeset
   255
937c479e62fe some support for bibtex files;
wenzelm
parents:
diff changeset
   256
937c479e62fe some support for bibtex files;
wenzelm
parents:
diff changeset
   257
    /* other tokens */
937c479e62fe some support for bibtex files;
wenzelm
parents:
diff changeset
   258
937c479e62fe some support for bibtex files;
wenzelm
parents:
diff changeset
   259
    private val at = "@" ^^ keyword
937c479e62fe some support for bibtex files;
wenzelm
parents:
diff changeset
   260
    private val left_brace = "{" ^^ keyword
937c479e62fe some support for bibtex files;
wenzelm
parents:
diff changeset
   261
    private val right_brace = "}" ^^ keyword
937c479e62fe some support for bibtex files;
wenzelm
parents:
diff changeset
   262
    private val left_paren = "(" ^^ keyword
937c479e62fe some support for bibtex files;
wenzelm
parents:
diff changeset
   263
    private val right_paren = ")" ^^ keyword
937c479e62fe some support for bibtex files;
wenzelm
parents:
diff changeset
   264
937c479e62fe some support for bibtex files;
wenzelm
parents:
diff changeset
   265
    private val nat = "[0-9]+".r ^^ token(Token.Kind.NAT)
937c479e62fe some support for bibtex files;
wenzelm
parents:
diff changeset
   266
937c479e62fe some support for bibtex files;
wenzelm
parents:
diff changeset
   267
    private val ident =
937c479e62fe some support for bibtex files;
wenzelm
parents:
diff changeset
   268
      """[\x21-\x7f&&[^"#%'(),={}0-9]][\x21-\x7f&&[^"#%'(),={}]]*""".r ^^ token(Token.Kind.IDENT)
937c479e62fe some support for bibtex files;
wenzelm
parents:
diff changeset
   269
58528
7d6b8f8893e8 more explicit item kind;
wenzelm
parents: 58527
diff changeset
   270
    val other_token = "[=#,]".r ^^ keyword | (nat | (ident | space))
7d6b8f8893e8 more explicit item kind;
wenzelm
parents: 58527
diff changeset
   271
7d6b8f8893e8 more explicit item kind;
wenzelm
parents: 58527
diff changeset
   272
7d6b8f8893e8 more explicit item kind;
wenzelm
parents: 58527
diff changeset
   273
    /* items */
7d6b8f8893e8 more explicit item kind;
wenzelm
parents: 58527
diff changeset
   274
7d6b8f8893e8 more explicit item kind;
wenzelm
parents: 58527
diff changeset
   275
    private val item_start: Parser[(String, List[Token])] =
7d6b8f8893e8 more explicit item kind;
wenzelm
parents: 58527
diff changeset
   276
      at ~ rep(strict_space) ~ ident ~ rep(strict_space) ^^
7d6b8f8893e8 more explicit item kind;
wenzelm
parents: 58527
diff changeset
   277
        { case a ~ b ~ c ~ d => (c.source, List(a) ::: b ::: List(c) ::: d) }
7d6b8f8893e8 more explicit item kind;
wenzelm
parents: 58527
diff changeset
   278
7d6b8f8893e8 more explicit item kind;
wenzelm
parents: 58527
diff changeset
   279
    private val item_body =
7d6b8f8893e8 more explicit item kind;
wenzelm
parents: 58527
diff changeset
   280
      delimited | (recover_delimited | other_token)
7d6b8f8893e8 more explicit item kind;
wenzelm
parents: 58527
diff changeset
   281
7d6b8f8893e8 more explicit item kind;
wenzelm
parents: 58527
diff changeset
   282
    private val item: Parser[Item] =
7d6b8f8893e8 more explicit item kind;
wenzelm
parents: 58527
diff changeset
   283
      (item_start ~ left_brace ~ rep(item_body) ~ opt(right_brace) |
7d6b8f8893e8 more explicit item kind;
wenzelm
parents: 58527
diff changeset
   284
       item_start ~ left_paren ~ rep(item_body) ~ opt(right_paren)) ^^
7d6b8f8893e8 more explicit item kind;
wenzelm
parents: 58527
diff changeset
   285
        { case (kind, a) ~ b ~ c ~ d => Item(kind, a ::: List(b) ::: c ::: d.toList) }
7d6b8f8893e8 more explicit item kind;
wenzelm
parents: 58527
diff changeset
   286
7d6b8f8893e8 more explicit item kind;
wenzelm
parents: 58527
diff changeset
   287
    private val recover_item: Parser[Item] =
7d6b8f8893e8 more explicit item kind;
wenzelm
parents: 58527
diff changeset
   288
      at ~ "(?m)[^@]+".r ^^ { case a ~ b => Item("", List(a, Token(Token.Kind.ERROR, b))) }
7d6b8f8893e8 more explicit item kind;
wenzelm
parents: 58527
diff changeset
   289
7d6b8f8893e8 more explicit item kind;
wenzelm
parents: 58527
diff changeset
   290
    def item_line(ctxt: Line_Context): Parser[(Item, Line_Context)] =
7d6b8f8893e8 more explicit item kind;
wenzelm
parents: 58527
diff changeset
   291
    {
7d6b8f8893e8 more explicit item kind;
wenzelm
parents: 58527
diff changeset
   292
      ctxt match {
7d6b8f8893e8 more explicit item kind;
wenzelm
parents: 58527
diff changeset
   293
        case Ignored_Context =>
7d6b8f8893e8 more explicit item kind;
wenzelm
parents: 58527
diff changeset
   294
          item_start ~ (left_brace | left_paren) ^^
7d6b8f8893e8 more explicit item kind;
wenzelm
parents: 58527
diff changeset
   295
            { case (kind, a) ~ b =>
7d6b8f8893e8 more explicit item kind;
wenzelm
parents: 58527
diff changeset
   296
                val right = if (b.source == "{") "}" else ")"
7d6b8f8893e8 more explicit item kind;
wenzelm
parents: 58527
diff changeset
   297
                (Item(kind, a ::: List(b)), Item_Context(kind, Closed, right)) }
7d6b8f8893e8 more explicit item kind;
wenzelm
parents: 58527
diff changeset
   298
        case Item_Context(kind, delim, right) =>
7d6b8f8893e8 more explicit item kind;
wenzelm
parents: 58527
diff changeset
   299
          if (delim.depth > 0)
7d6b8f8893e8 more explicit item kind;
wenzelm
parents: 58527
diff changeset
   300
            delimited_line(ctxt)
7d6b8f8893e8 more explicit item kind;
wenzelm
parents: 58527
diff changeset
   301
          else {
7d6b8f8893e8 more explicit item kind;
wenzelm
parents: 58527
diff changeset
   302
            delimited_line(ctxt) |
7d6b8f8893e8 more explicit item kind;
wenzelm
parents: 58527
diff changeset
   303
            other_token ^^ { case a => (Item(kind, List(a)), ctxt) } |
7d6b8f8893e8 more explicit item kind;
wenzelm
parents: 58527
diff changeset
   304
            right ^^ { case a => (Item(kind, List(keyword(a))), Ignored_Context) }
7d6b8f8893e8 more explicit item kind;
wenzelm
parents: 58527
diff changeset
   305
          }
7d6b8f8893e8 more explicit item kind;
wenzelm
parents: 58527
diff changeset
   306
        case _ => failure("")
7d6b8f8893e8 more explicit item kind;
wenzelm
parents: 58527
diff changeset
   307
      }
7d6b8f8893e8 more explicit item kind;
wenzelm
parents: 58527
diff changeset
   308
    }
7d6b8f8893e8 more explicit item kind;
wenzelm
parents: 58527
diff changeset
   309
58523
937c479e62fe some support for bibtex files;
wenzelm
parents:
diff changeset
   310
937c479e62fe some support for bibtex files;
wenzelm
parents:
diff changeset
   311
    /* chunks */
937c479e62fe some support for bibtex files;
wenzelm
parents:
diff changeset
   312
58528
7d6b8f8893e8 more explicit item kind;
wenzelm
parents: 58527
diff changeset
   313
    val chunk: Parser[Chunk] = ignored | (item | recover_item)
58523
937c479e62fe some support for bibtex files;
wenzelm
parents:
diff changeset
   314
58528
7d6b8f8893e8 more explicit item kind;
wenzelm
parents: 58527
diff changeset
   315
    def chunk_line(ctxt: Line_Context): Parser[(Chunk, Line_Context)] =
7d6b8f8893e8 more explicit item kind;
wenzelm
parents: 58527
diff changeset
   316
      ignored_line(ctxt) | item_line(ctxt)
7d6b8f8893e8 more explicit item kind;
wenzelm
parents: 58527
diff changeset
   317
  }
58523
937c479e62fe some support for bibtex files;
wenzelm
parents:
diff changeset
   318
937c479e62fe some support for bibtex files;
wenzelm
parents:
diff changeset
   319
58528
7d6b8f8893e8 more explicit item kind;
wenzelm
parents: 58527
diff changeset
   320
  /* parse */
58523
937c479e62fe some support for bibtex files;
wenzelm
parents:
diff changeset
   321
937c479e62fe some support for bibtex files;
wenzelm
parents:
diff changeset
   322
  def parse(input: CharSequence): List[Chunk] =
937c479e62fe some support for bibtex files;
wenzelm
parents:
diff changeset
   323
  {
937c479e62fe some support for bibtex files;
wenzelm
parents:
diff changeset
   324
    val in: Reader[Char] = new CharSequenceReader(input)
58528
7d6b8f8893e8 more explicit item kind;
wenzelm
parents: 58527
diff changeset
   325
    Parsers.parseAll(Parsers.rep(Parsers.chunk), in) match {
58523
937c479e62fe some support for bibtex files;
wenzelm
parents:
diff changeset
   326
      case Parsers.Success(result, _) => result
58526
f05ccce3eca2 SideKick parser for bibtex entries;
wenzelm
parents: 58524
diff changeset
   327
      case _ => error("Unexpected failure to parse input:\n" + input.toString)
58523
937c479e62fe some support for bibtex files;
wenzelm
parents:
diff changeset
   328
    }
937c479e62fe some support for bibtex files;
wenzelm
parents:
diff changeset
   329
  }
58528
7d6b8f8893e8 more explicit item kind;
wenzelm
parents: 58527
diff changeset
   330
7d6b8f8893e8 more explicit item kind;
wenzelm
parents: 58527
diff changeset
   331
  def parse_line(input: CharSequence, context: Line_Context): (List[Chunk], Line_Context) =
7d6b8f8893e8 more explicit item kind;
wenzelm
parents: 58527
diff changeset
   332
  {
7d6b8f8893e8 more explicit item kind;
wenzelm
parents: 58527
diff changeset
   333
    var in: Reader[Char] = new CharSequenceReader(input)
7d6b8f8893e8 more explicit item kind;
wenzelm
parents: 58527
diff changeset
   334
    val chunks = new mutable.ListBuffer[Chunk]
7d6b8f8893e8 more explicit item kind;
wenzelm
parents: 58527
diff changeset
   335
    var ctxt = context
7d6b8f8893e8 more explicit item kind;
wenzelm
parents: 58527
diff changeset
   336
    while (!in.atEnd) {
7d6b8f8893e8 more explicit item kind;
wenzelm
parents: 58527
diff changeset
   337
      Parsers.parse(Parsers.chunk_line(ctxt), in) match {
7d6b8f8893e8 more explicit item kind;
wenzelm
parents: 58527
diff changeset
   338
        case Parsers.Success((x, c), rest) => { chunks += x; ctxt = c; in = rest }
7d6b8f8893e8 more explicit item kind;
wenzelm
parents: 58527
diff changeset
   339
        case Parsers.NoSuccess(_, rest) =>
7d6b8f8893e8 more explicit item kind;
wenzelm
parents: 58527
diff changeset
   340
          error("Unepected failure to parse input:\n" + rest.source.toString)
7d6b8f8893e8 more explicit item kind;
wenzelm
parents: 58527
diff changeset
   341
      }
7d6b8f8893e8 more explicit item kind;
wenzelm
parents: 58527
diff changeset
   342
    }
7d6b8f8893e8 more explicit item kind;
wenzelm
parents: 58527
diff changeset
   343
    (chunks.toList, ctxt)
7d6b8f8893e8 more explicit item kind;
wenzelm
parents: 58527
diff changeset
   344
  }
58523
937c479e62fe some support for bibtex files;
wenzelm
parents:
diff changeset
   345
}
937c479e62fe some support for bibtex files;
wenzelm
parents:
diff changeset
   346