Advertisement
Guest User

Untitled

a guest
Jan 1st, 2018
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.56 KB | None | 0 0
  1. /*
  2. BSD License
  3.  
  4. Copyright (c) 2013, Tom Everett
  5. All rights reserved.
  6.  
  7. Redistribution and use in source and binary forms, with or without
  8. modification, are permitted provided that the following conditions
  9. are met:
  10.  
  11. 1. Redistributions of source code must retain the above copyright
  12. notice, this list of conditions and the following disclaimer.
  13. 2. Redistributions in binary form must reproduce the above copyright
  14. notice, this list of conditions and the following disclaimer in the
  15. documentation and/or other materials provided with the distribution.
  16. 3. Neither the name of Tom Everett nor the names of its contributors
  17. may be used to endorse or promote products derived from this software
  18. without specific prior written permission.
  19.  
  20. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  21. "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  22. LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  23. A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  24. HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  25. SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  26. LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  27. DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  28. THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  29. (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  30. OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  31.  
  32. A: Aa | b;
  33. // becomes
  34. A: bR;
  35. R: (aA)?;
  36.  
  37. A: b | Aa
  38. // becomes
  39. A: bR;
  40. R: (Aa)?;
  41.  
  42. A: AB | B
  43. A: BR;
  44. R: (BA)?;
  45.  
  46. */
  47.  
  48. grammar bash;
  49.  
  50. WS
  51. : [ \t\r\n] -> skip
  52. ;
  53.  
  54. LETTER
  55. : [a-zA-Z];
  56.  
  57. DIGIT
  58. : [0-9];
  59.  
  60. number
  61. : DIGIT | number DIGIT
  62. ;
  63.  
  64. word
  65. : LETTER | word LETTER | word '_'
  66. ;
  67.  
  68. word_list
  69. : word | word_list word
  70. ;
  71.  
  72. assignment_word
  73. : word '=' word
  74. ;
  75.  
  76. redirection
  77. : '>' word | '<' word | number '>' word | number '<' word | '>>' word | number '>>' word | '<<' word | number '<<' word | '<&' number | number '<&' number | '>&' number | number '>&' number | '<&' word | number '<&' word | '>&' word | number '>&' word | '<<-' word | number '<<-' word | '>&' '-' | number '>&' '-' | '<&' '-' | number '<&' '-' | '&>' word | number '<>' word | '<>' word | '>|' word | number '>|' word
  78. ;
  79.  
  80. simple_command_element
  81. : (word | assignment_word | redirection)
  82. ;
  83.  
  84. redirection_list :
  85. redirection redirection_list_r
  86. ;
  87.  
  88. redirection_list_r :
  89. ( redirection redirection_list)?
  90. ;
  91.  
  92. simple_command
  93. : simple_command_element simple_command_r
  94. ;
  95.  
  96. simple_command_r
  97. : (simple_command_element simple_command)?
  98. ;
  99.  
  100. command
  101. : simple_command | shell_command | (shell_command redirection_list)
  102. ;
  103.  
  104. shell_command
  105. : for_command | case_command | ('while' compound_list 'do' compound_list 'done') | ('until' compound_list 'do' compound_list 'done') | select_command | if_command | subshell | group_command | function_def
  106. ;
  107.  
  108. for_command
  109. : (('for' word newline_list 'do' compound_list 'done') | ('for' word newline_list '{' compound_list '}') | ('for' word '|' newline_list 'do' compound_list 'done') | ('for' word '|' newline_list '{' compound_list '}') | ('for' word newline_list 'in' word_list list_terminator newline_list 'do' compound_list 'done') | ('for' word newline_list 'in' word_list list_terminator newline_list '{' compound_list '}'))
  110. ;
  111.  
  112. select_command
  113. : ('select' word newline_list 'do' list 'done') | ('select' word newline_list '{' list '}') | ('select' word '|' newline_list 'do' list 'done') | ('select' word '|' newline_list '{' list '}') | ('select' word newline_list 'in' word_list list_terminator newline_list 'do' list 'done') | ('select' word newline_list 'in' word_list list_terminator newline_list '{' list '}')
  114. ;
  115.  
  116. case_command
  117. : ('case' word newline_list 'in' newline_list 'esac') | ('case' word newline_list 'in' case_clause_sequence newline_list 'esac') | ('case' word newline_list 'in' case_clause 'esac')
  118. ;
  119.  
  120. function_def
  121. : (word '(' ')' newline_list group_command) | ('function' word '(' ')' newline_list group_command) | ('function' word newline_list group_command)
  122. ;
  123.  
  124. subshell
  125. : '(' compound_list ')'
  126. ;
  127.  
  128. if_command
  129. : ('if' compound_list 'then' compound_list 'fi') | ('if' compound_list 'then' compound_list 'else' compound_list 'fi') | ('if' compound_list 'then' compound_list elif_clause 'fi')
  130. ;
  131.  
  132. group_command
  133. : '{' list '}'
  134. ;
  135.  
  136. elif_clause
  137. : ('elif' compound_list 'then' compound_list) | ('elif' compound_list 'then' compound_list 'else' compound_list) | ('elif' compound_list 'then' compound_list elif_clause)
  138. ;
  139.  
  140. case_clause
  141. : pattern_list | (case_clause_sequence pattern_list)
  142. ;
  143.  
  144. pattern_list
  145. : (newline_list pattern ')' compound_list) | (newline_list pattern ')' newline_list) | (newline_list '(' pattern ')' compound_list) | (newline_list '(' pattern ')' newline_list)
  146. ;
  147.  
  148. case_clause_sequence
  149. : pattern_list '||' | case_clause_sequence pattern_list '||'
  150. ;
  151.  
  152. /*
  153. pattern
  154. : word | (pattern '|' word)
  155. ;
  156. */
  157.  
  158. pattern
  159. : word pattern_r
  160. ;
  161.  
  162. pattern_r
  163. : ('|' word pattern)?
  164. ;
  165.  
  166. list
  167. : (newline_list list0)
  168. ;
  169.  
  170. compound_list
  171. : list | (newline_list list1)
  172. ;
  173.  
  174. list0
  175. : (list1 '\n' newline_list) | (list1 '&' newline_list) | (list1 '|' newline_list)
  176. ;
  177.  
  178. /*
  179. A: Aa | b;
  180. // becomes
  181. A: bR;
  182. R: (aA)?;
  183.  
  184. A: Aza | b;
  185. */
  186.  
  187. list1
  188. : (list1 '&&' newline_list list1) | (list1 '||' newline_list list1) | (list1 '&' newline_list list1) | (list1 '|' newline_list list1) | (list1 '\n' newline_list list1) | (pipeline_command)
  189. ;
  190.  
  191. list_terminator
  192. : '\n' | '|'
  193. ;
  194.  
  195. newline_list
  196. : '\n' | newline_list '\n'
  197. ;
  198.  
  199. /*
  200. A: Aa | b;
  201. // becomes
  202. A: bR;
  203. R: (aA)?;
  204. */
  205.  
  206. simple_list
  207. : simple_list1 | (simple_list1 '&') | (simple_list1 '|')
  208. ;
  209.  
  210. simple_list1
  211. : (simple_list1 '&&' newline_list simple_list1) | (simple_list1 '||' newline_list simple_list1) | (simple_list1 '&' simple_list1) | (simple_list1 '|' simple_list1) | (pipeline_command)
  212. ;
  213.  
  214. pipeline_command
  215. : pipeline | ('!' pipeline) | (timespec pipeline) | (timespec '!' pipeline) | ('!' timespec pipeline)
  216. ;
  217.  
  218. pipeline
  219. : pipeline '|' (newline_list pipeline) | command
  220. ;
  221.  
  222. time_opt
  223. : '-p'
  224. ;
  225.  
  226. timespec
  227. : 'time' | 'time' time_opt
  228. ;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement