25 abstract class Line_Context |
25 abstract class Line_Context |
26 case object Finished extends Line_Context |
26 case object Finished extends Line_Context |
27 case class Quoted(quote: String) extends Line_Context |
27 case class Quoted(quote: String) extends Line_Context |
28 case object Verbatim extends Line_Context |
28 case object Verbatim extends Line_Context |
29 case class Cartouche(depth: Int) extends Line_Context |
29 case class Cartouche(depth: Int) extends Line_Context |
30 case object Comment_Prefix extends Line_Context |
30 case class Comment_Prefix(symbol: Symbol.Symbol) extends Line_Context |
31 case class Cartouche_Comment(depth: Int) extends Line_Context |
31 case class Cartouche_Comment(depth: Int) extends Line_Context |
32 case class Comment(depth: Int) extends Line_Context |
32 case class Comment(depth: Int) extends Line_Context |
33 |
33 |
34 |
34 |
35 |
35 |
172 "{*" ~ verbatim_body ^^ { case x ~ y => x + y } |
172 "{*" ~ verbatim_body ^^ { case x ~ y => x + y } |
173 |
173 |
174 |
174 |
175 /* nested text cartouches */ |
175 /* nested text cartouches */ |
176 |
176 |
177 private def cartouche_depth(depth: Int): Parser[(String, Int)] = new Parser[(String, Int)] |
177 def cartouche_depth(depth: Int): Parser[(String, Int)] = new Parser[(String, Int)] |
178 { |
178 { |
179 require(depth >= 0) |
179 require(depth >= 0) |
180 |
180 |
181 def apply(in: Input) = |
181 def apply(in: Input) = |
182 { |
182 { |
226 val source1 = |
226 val source1 = |
227 Library.try_unprefix(Symbol.open_decoded, source) orElse |
227 Library.try_unprefix(Symbol.open_decoded, source) orElse |
228 Library.try_unprefix(Symbol.open, source) getOrElse err() |
228 Library.try_unprefix(Symbol.open, source) getOrElse err() |
229 Library.try_unsuffix(Symbol.close_decoded, source1) orElse |
229 Library.try_unsuffix(Symbol.close_decoded, source1) orElse |
230 Library.try_unsuffix(Symbol.close, source1) getOrElse err() |
230 Library.try_unsuffix(Symbol.close, source1) getOrElse err() |
231 } |
|
232 |
|
233 def comment_prefix: Parser[String] = |
|
234 (Symbol.comment | Symbol.comment_decoded) ~ many(Symbol.is_blank) ^^ |
|
235 { case a ~ b => a + b.mkString } |
|
236 |
|
237 def comment_cartouche: Parser[String] = |
|
238 comment_prefix ~ cartouche ^^ { case a ~ b => a + b } |
|
239 |
|
240 def comment_cartouche_line(ctxt: Line_Context): Parser[(String, Line_Context)] = |
|
241 { |
|
242 def cartouche_context(d: Int): Line_Context = |
|
243 if (d == 0) Finished else Cartouche_Comment(d) |
|
244 |
|
245 ctxt match { |
|
246 case Finished => |
|
247 comment_prefix ~ opt(cartouche_depth(0)) ^^ { |
|
248 case a ~ None => (a, Comment_Prefix) |
|
249 case a ~ Some((c, d)) => (a + c, cartouche_context(d)) } |
|
250 case Comment_Prefix => |
|
251 many1(Symbol.is_blank) ~ opt(cartouche_depth(0)) ^^ { |
|
252 case b ~ None => (b.mkString, Comment_Prefix) |
|
253 case b ~ Some((c, d)) => (b.mkString + c, cartouche_context(d)) } | |
|
254 cartouche_depth(0) ^^ { case (c, d) => (c, cartouche_context(d)) } |
|
255 case Cartouche_Comment(depth) => |
|
256 cartouche_depth(depth) ^^ { case (c, d) => (c, cartouche_context(d)) } |
|
257 case _ => failure("") |
|
258 } |
|
259 } |
231 } |
260 |
232 |
261 |
233 |
262 /* nested comments */ |
234 /* nested comments */ |
263 |
235 |