aboutsummaryrefslogtreecommitdiff
path: root/syntax/def.peg
blob: e40610f3f4e94704cbb5324e7a73ffcb23242f49 (plain)
  1. # Subsets of Markdown syntax structure
  2. # expressed in parsing expression grammar (PEG) notation.
  3. #
  4. # Copyright 2025, Jonas Smedegaard <dr@jones.dk>
  5. # SPDX-License-Identifier: GPL-3+
  6. # Document
  7. Markdown <- ('---' NEWLINE MetaBlock* '---' NEWLINE)? Block*
  8. # Block elements
  9. Block <- Header
  10. / List
  11. / LinkDefinition
  12. / Paragraph
  13. Header <- '#'+ SPACE* !NEWLINE Words? SPACE* NEWLINE
  14. List <- ([-*]+ / [[:digit:]]+ [).]) (_ (List / Words))? NEWLINE
  15. LinkDefinition <- LinkLabel ':' _? Uri (_ LinkTitle)? NEWLINE
  16. Paragraph <- Words (HardBreak Words)* NEWLINE+
  17. # Inline elements
  18. Words <- StyledWords
  19. / LinkedWords
  20. / AnnotatedWords
  21. / PlainWords
  22. StyledWords <- '**' Words '**'
  23. / '*' Words '*'
  24. / '_' Words '_'
  25. LinkedWords <- '[' Words ']' '(' ((Uri _)? LinkTitle / Uri) ')'
  26. / '[' Words ']' LinkLabel
  27. / LinkLabel
  28. AnnotatedWords <- '[' Words ']' '{' ![{}] PlainWords '}'
  29. LinkLabel <- '[' _? ![\[\]] PlainWords _? ']'
  30. LinkTitle <- '"' _? !'"' _? PlainWords _? '"'
  31. / "'" _? !"'" PlainWords _? "'"
  32. / "(" _? ![()] PlainWords _? ")"
  33. PlainWords <- PRINTABLES (_ PRINTABLES)*
  34. Uri <- '<' ![<>] PRINTABLES? '>'
  35. / ![<>] PRINTABLES
  36. HardBreak <- SPACE SPACE+ NEWLINE
  37. _ <- SPACE+
  38. / SPACE? NEWLINE SPACE? SPACE? SPACE?
  39. # Terminals
  40. PRINTABLES <- [[:graph:]]+
  41. SPACE <- ' '
  42. NEWLINE <- '\r\n'
  43. / '\n'
  44. / '\r'
  45. # Semantic Markdown
  46. AnnotatedWordsX <- '[' Words ']'
  47. ('{' (SemWords / ![{}] PlainWords) '}')
  48. SemWords <- SEMPREFIX '<' ![<>] PRINTABLES '>'
  49. / SEMPREFIX Curie
  50. Curie <- NAME? ':' NAME?
  51. SEMPREFIX <- [.#]