Urn.g4 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. /*
  2. * This grammar encodes the URN syntax.
  3. *
  4. * RFC 2141.
  5. * https.//tools.ietf.org/html/rfc2141
  6. */
  7. grammar Urn;
  8. @parser::members {
  9. func isIdentifier(s string) bool {
  10. for i, r := range s {
  11. // !unicode.IsLetter(r) etc. when (if) we'll need unicode
  12. if (r < 'a' || r > 'z') && (r < 'A' || r > 'Z') && (r < '0' || r > '9') && (r != '-' || (i == 0 && r == '-')) {
  13. return false
  14. }
  15. }
  16. return true
  17. }
  18. }
  19. urn
  20. : Urn Colon iD Colon sS EOF
  21. ;
  22. iD
  23. : Part
  24. {len($ctx.GetText()) <= 32}?<fail={"exceed max (32) number of characters"}>
  25. {isIdentifier($ctx.GetText())}?<fail={"is not a valid identifier"}>
  26. ;
  27. sS
  28. : Part (Colon|Hyphen)* sS?
  29. | (Colon|Hyphen)+ sS?
  30. | Part sS?
  31. | Urn
  32. ;
  33. Urn
  34. : URN
  35. ;
  36. Part
  37. : IDENTIFIER
  38. | CHARS
  39. ;
  40. Colon
  41. : ':'
  42. ;
  43. Hyphen
  44. : '-'
  45. ;
  46. Whitespace
  47. : ' '
  48. ;
  49. fragment URN
  50. : [uU][rR][nN]
  51. ;
  52. fragment IDENTIFIER
  53. : ALPHA_NUMERIC (ALPHA_NUMERIC|Hyphen)+
  54. ;
  55. fragment CHARS
  56. : (TRANSLATION|HEX)+
  57. ;
  58. fragment HEX
  59. : '%' HEX_DIGIT HEX_DIGIT
  60. ;
  61. fragment NUMBER
  62. : [0-9]
  63. ;
  64. fragment LETTER
  65. : [A-Z]
  66. | [a-z]
  67. ;
  68. fragment ALPHA_NUMERIC
  69. : NUMBER
  70. | LETTER
  71. ;
  72. fragment OTHER
  73. : '('
  74. | ')'
  75. | '+'
  76. | ','
  77. | '.'
  78. | '='
  79. | '@'
  80. | ';'
  81. | '$'
  82. | '_'
  83. | '!'
  84. | '*'
  85. | '\''
  86. ;
  87. fragment HEX_DIGIT
  88. : NUMBER
  89. | [A-F]
  90. | [a-f]
  91. ;
  92. fragment RESERVED
  93. : '%'
  94. | '/'
  95. | '?'
  96. | '#'
  97. ;
  98. fragment TRANSLATION
  99. : OTHER
  100. | RESERVED
  101. | ALPHA_NUMERIC
  102. ;