PageRenderTime 25ms CodeModel.GetById 22ms RepoModel.GetById 1ms app.codeStats 0ms

/vendor/bundle/ruby/1.8/gems/coderay-1.0.5/lib/coderay/scanners/yaml.rb

https://bitbucket.org/pekopeko1/redhotchiliproject-3.0
Ruby | 140 lines | 123 code | 14 blank | 3 comment | 23 complexity | 8f334b3a53a43b4d7cfae5fd4c3d0c18 MD5 | raw file
Possible License(s): GPL-2.0, ISC, LGPL-2.1, MIT, Apache-2.0, BSD-3-Clause
  1. module CodeRay
  2. module Scanners
  3. # Scanner for YAML.
  4. #
  5. # Based on the YAML scanner from Syntax by Jamis Buck.
  6. class YAML < Scanner
  7. register_for :yaml
  8. file_extension 'yml'
  9. KINDS_NOT_LOC = :all
  10. protected
  11. def scan_tokens encoder, options
  12. state = :initial
  13. key_indent = string_indent = 0
  14. until eos?
  15. key_indent = nil if bol?
  16. if match = scan(/ +[\t ]*/)
  17. encoder.text_token match, :space
  18. elsif match = scan(/\n+/)
  19. encoder.text_token match, :space
  20. state = :initial if match.index(?\n)
  21. elsif match = scan(/#.*/)
  22. encoder.text_token match, :comment
  23. elsif bol? and case
  24. when match = scan(/---|\.\.\./)
  25. encoder.begin_group :head
  26. encoder.text_token match, :head
  27. encoder.end_group :head
  28. next
  29. when match = scan(/%.*/)
  30. encoder.text_token match, :doctype
  31. next
  32. end
  33. elsif state == :value and case
  34. when !check(/(?:"[^"]*")(?=: |:$)/) && match = scan(/"/)
  35. encoder.begin_group :string
  36. encoder.text_token match, :delimiter
  37. encoder.text_token match, :content if match = scan(/ [^"\\]* (?: \\. [^"\\]* )* /mx)
  38. encoder.text_token match, :delimiter if match = scan(/"/)
  39. encoder.end_group :string
  40. next
  41. when match = scan(/[|>][-+]?/)
  42. encoder.begin_group :string
  43. encoder.text_token match, :delimiter
  44. string_indent = key_indent || column(pos - match.size) - 1
  45. encoder.text_token matched, :content if scan(/(?:\n+ {#{string_indent + 1}}.*)+/)
  46. encoder.end_group :string
  47. next
  48. when match = scan(/(?![!"*&]).+?(?=$|\s+#)/)
  49. encoder.begin_group :string
  50. encoder.text_token match, :content
  51. string_indent = key_indent || column(pos - match.size) - 1
  52. encoder.text_token matched, :content if scan(/(?:\n+ {#{string_indent + 1}}.*)+/)
  53. encoder.end_group :string
  54. next
  55. end
  56. elsif case
  57. when match = scan(/[-:](?= |$)/)
  58. state = :value if state == :colon && (match == ':' || match == '-')
  59. state = :value if state == :initial && match == '-'
  60. encoder.text_token match, :operator
  61. next
  62. when match = scan(/[,{}\[\]]/)
  63. encoder.text_token match, :operator
  64. next
  65. when state == :initial && match = scan(/[-\w.()\/ ]*\S(?= *:(?: |$))/)
  66. encoder.text_token match, :key
  67. key_indent = column(pos - match.size) - 1
  68. state = :colon
  69. next
  70. when match = scan(/(?:"[^"\n]*"|'[^'\n]*')(?= *:(?: |$))/)
  71. encoder.begin_group :key
  72. encoder.text_token match[0,1], :delimiter
  73. encoder.text_token match[1..-2], :content
  74. encoder.text_token match[-1,1], :delimiter
  75. encoder.end_group :key
  76. key_indent = column(pos - match.size) - 1
  77. state = :colon
  78. next
  79. when match = scan(/(![\w\/]+)(:([\w:]+))?/)
  80. encoder.text_token self[1], :type
  81. if self[2]
  82. encoder.text_token ':', :operator
  83. encoder.text_token self[3], :class
  84. end
  85. next
  86. when match = scan(/&\S+/)
  87. encoder.text_token match, :variable
  88. next
  89. when match = scan(/\*\w+/)
  90. encoder.text_token match, :global_variable
  91. next
  92. when match = scan(/<</)
  93. encoder.text_token match, :class_variable
  94. next
  95. when match = scan(/\d\d:\d\d:\d\d/)
  96. encoder.text_token match, :octal
  97. next
  98. when match = scan(/\d\d\d\d-\d\d-\d\d\s\d\d:\d\d:\d\d(\.\d+)? [-+]\d\d:\d\d/)
  99. encoder.text_token match, :octal
  100. next
  101. when match = scan(/:\w+/)
  102. encoder.text_token match, :symbol
  103. next
  104. when match = scan(/[^:\s]+(:(?! |$)[^:\s]*)* .*/)
  105. encoder.text_token match, :error
  106. next
  107. when match = scan(/[^:\s]+(:(?! |$)[^:\s]*)*/)
  108. encoder.text_token match, :error
  109. next
  110. end
  111. else
  112. raise if eos?
  113. encoder.text_token getch, :error
  114. end
  115. end
  116. encoder
  117. end
  118. end
  119. end
  120. end