logo

oasis-root

Compiled tree of Oasis Linux based on own branch at <https://hacktivis.me/git/oasis/> git clone https://anongit.hacktivis.me/git/oasis-root.git

vcard.lua (2614B)


  1. -- Copyright (c) 2015-2024 Piotr Orzechowski [drzewo.org]. See LICENSE.
  2. -- vCard 2.1, 3.0 and 4.0 LPeg lexer.
  3. local lexer = require('lexer')
  4. local token, word_match = lexer.token, lexer.word_match
  5. local P, S = lpeg.P, lpeg.S
  6. local lex = lexer.new('vcard')
  7. -- Whitespace.
  8. lex:add_rule('whitespace', token(lexer.WHITESPACE, lexer.space^1))
  9. -- Begin vCard, end vCard.
  10. lex:add_rule('begin_sequence', token(lexer.KEYWORD, 'BEGIN') * token(lexer.OPERATOR, ':') *
  11. token(lexer.COMMENT, 'VCARD'))
  12. lex:add_rule('end_sequence', token(lexer.KEYWORD, 'END') * token(lexer.OPERATOR, ':') *
  13. token(lexer.COMMENT, 'VCARD'))
  14. -- vCard version (in v3.0 and v4.0 must appear immediately after BEGIN:VCARD).
  15. lex:add_rule('version_sequence', token(lexer.KEYWORD, 'VERSION') * token(lexer.OPERATOR, ':') *
  16. token(lexer.CONSTANT, lexer.digit^1 * ('.' * lexer.digit^1)^-1))
  17. -- Required properties.
  18. local required_property = token(lexer.KEYWORD, word_match({
  19. 'BEGIN', 'END', 'FN', 'VERSION', --
  20. 'N' -- Not required in v4.0.
  21. }, true)) * #P(':')
  22. lex:add_rule('required_property', required_property)
  23. -- Supported properties.
  24. local supported_property = token(lexer.TYPE, word_match({
  25. 'ADR', 'BDAY', 'CATEGORIES', 'EMAIL', 'END', 'GEO', 'KEY', 'LOGO', 'NOTE', 'ORG', 'PHOTO', 'REV',
  26. 'ROLE', 'SOUND', 'SOURCE', 'TEL', 'TITLE', 'TZ', 'UID', 'URL',
  27. -- Supported in v4.0 only.
  28. 'ANNIVERSARY', 'CALADRURI', 'CALURI', 'CLIENTPIDMAP', 'FBURL', 'GENDER', 'KIND', 'LANG', 'MEMBER',
  29. 'RELATED', 'XML',
  30. -- Not supported in v4.0.
  31. 'AGENT', 'LABEL', 'MAILER', 'PROFILE', 'SORT-STRING',
  32. -- Supported in v3.0 only.
  33. 'CLASS', 'NAME',
  34. -- Not supported in v2.1.
  35. 'IMPP', 'NICKNAME', 'PRODID'
  36. }, true)) * #S(':;')
  37. lex:add_rule('supported_property', supported_property)
  38. -- Group and property.
  39. local identifier = lexer.alpha^1 * lexer.digit^0 * ('-' * lexer.alnum^1)^0
  40. local property = required_property + supported_property +
  41. lexer.token(lexer.TYPE, S('xX') * '-' * identifier) * #S(':;')
  42. lex:add_rule('group_sequence', token(lexer.CONSTANT, lexer.starts_line(identifier)) *
  43. token(lexer.OPERATOR, '.') * property)
  44. -- Extension.
  45. lex:add_rule('extension',
  46. token(lexer.TYPE, lexer.starts_line(S('xX') * '-' * identifier * #S(':;'))))
  47. -- Parameter.
  48. local parameter = (token(lexer.IDENTIFIER, lexer.starts_line(identifier)) +
  49. token(lexer.STRING, identifier)) * #S(':=')
  50. lex:add_rule('parameter', parameter)
  51. -- Operators.
  52. lex:add_rule('operator', token(lexer.OPERATOR, S('.:;=')))
  53. -- Data.
  54. lex:add_rule('data', token(lexer.IDENTIFIER, lexer.any))
  55. -- Fold points.
  56. lex:add_fold_point(lexer.KEYWORD, 'BEGIN', 'END')
  57. return lex