You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

220 lines
8.6 KiB

  1. #!/bin/sh
  2. #
  3. # This file runs the highlighter on a specified file
  4. # i.e. parses the file with the highlighter. Outputs
  5. # running time (stderr) and resulting region_highlight
  6. # (file parse.out, or $2 if given).
  7. #
  8. # Can be also run in line-wise mode on own input (-o
  9. # option in $1), no region_highlight file then.
  10. #
  11. [[ -z "$ZSH_VERSION" ]] && exec /usr/bin/env /usr/local/bin/zsh-5.5.1 -f -c "source \"$0\" \"$1\" \"$2\" \"$3\""
  12. ZERO="${(%):-%N}"
  13. if [[ -e "${ZERO}/../fast-highlight" ]]; then
  14. source "${ZERO}/../fast-highlight"
  15. source "${ZERO}/../fast-string-highlight"
  16. fpath+=( "${ZERO}/.." )
  17. elif [[ -e "../fast-highlight" ]]; then
  18. source "../fast-highlight"
  19. source "../fast-string-highlight"
  20. fpath+=( "$PWD/.." )
  21. elif [[ -e "${ZERO}/fast-highlight" ]]; then
  22. source "${ZERO}/fast-highlight"
  23. source "${ZERO}/fast-string-highlight"
  24. fpath+=( "${ZERO}" )
  25. elif [[ -e "./fast-highlight" ]]; then
  26. source "./fast-highlight"
  27. source "./fast-string-highlight"
  28. fpath+=( "./" )
  29. else
  30. print -u2 "Could not find fast-highlight, aborting"
  31. exit 1
  32. fi
  33. zmodload zsh/zprof
  34. autoload is-at-least chroma/-git.ch
  35. setopt interactive_comments extendedglob
  36. # Own input?
  37. if [[ "$1" = "-o" || "$1" = "-oo" || "$1" = "-ooo" || "$1" = "-git" || "$1" = "-hue" || "$1" = "-hol" ]]; then
  38. typeset -a input
  39. input=()
  40. if [[ "$1" = "-o" ]]; then
  41. input+=( "./parse.zsh ../fast-highlight parse2.out" )
  42. input+=( "rm -f parse*.out" )
  43. input+=( "./mh-parse.zsh ../fast-highlight > out" )
  44. input+=( "if [[ -o multibyte ]]; then echo multibyte is set; fi" )
  45. input+=( "[[ \"a\" = *[[:alpha:]_-][[:alpha:]]# ]] && echo yes" )
  46. input+=( 'git tag -a v0.98 -m "Syntax highlighting of the history entries"' )
  47. input+=( 'func() { echo "a" >! phist2.db; echo "b" >>! phist2.db; fc -Rap "phist2.db"; list=( ${history[@]} ); echo "${history[1]}"; }' )
  48. (( ${+ZSH_EXECUTION_STRING} == 0 )) && { print -zr "${(F)input}"; return 0; }
  49. elif [[ "$1" = "-oo" ]]; then
  50. input+=( 'typeset -a list\n() {\necho "a" >! phist2.db\necho "b" >>! phist2.db\nfc -Rap "phist2.db"\nlist=( ${history[@]} )\necho "${history[2]}"\necho "${history[1]}"\necho "${#history}";\ninteger size="${#history}"\nsize+=1\necho "$size" / "${history[$size]}"\nlist=( "${history[$size]}" ${history[@]} )\n}' )
  51. input+=( 'typeset -a list\n() {\necho "a" >! phist2.db\necho "b" >>! phist2.db\nfc -Rap "phist2.db"\nlist=( ${history[@]} )\necho "${history[2]}"\necho "${history[1]}"\necho "${#history}";\ninteger size="${#history}"\nsize+=1\necho "$size" / "${history[$size]}"\nlist=( "${history[$size]}" ${history[@]} )\n}' )
  52. input+=( 'typeset -a list\n() {\necho "a" >! phist2.db\necho "b" >>! phist2.db\nfc -Rap "phist2.db"\nlist=( ${history[@]} )\necho "${history[2]}"\necho "${history[1]}"\necho "${#history}";\ninteger size="${#history}"\nsize+=1\necho "$size" / "${history[$size]}"\nlist=( "${history[$size]}" ${history[@]} )\n}' )
  53. input+=( 'typeset -a list\n() {\necho "a" >! phist2.db\necho "b" >>! phist2.db\nfc -Rap "phist2.db"\nlist=( ${history[@]} )\necho "${history[2]}"\necho "${history[1]}"\necho "${#history}";\ninteger size="${#history}"\nsize+=1\necho "$size" / "${history[$size]}"\nlist=( "${history[$size]}" ${history[@]} )\n}' )
  54. input+=( 'typeset -a list\n() {\necho "a" >! phist2.db\necho "b" >>! phist2.db\nfc -Rap "phist2.db"\nlist=( ${history[@]} )\necho "${history[2]}"\necho "${history[1]}"\necho "${#history}";\ninteger size="${#history}"\nsize+=1\necho "$size" / "${history[$size]}"\nlist=( "${history[$size]}" ${history[@]} )\n}' )
  55. input+=( 'typeset -a list\n() {\necho "a" >! phist2.db\necho "b" >>! phist2.db\nfc -Rap "phist2.db"\nlist=( ${history[@]} )\necho "${history[2]}"\necho "${history[1]}"\necho "${#history}";\ninteger size="${#history}"\nsize+=1\necho "$size" / "${history[$size]}"\nlist=( "${history[$size]}" ${history[@]} )\n}' )
  56. input+=( 'typeset -a list\n() {\necho "a" >! phist2.db\necho "b" >>! phist2.db\nfc -Rap "phist2.db"\nlist=( ${history[@]} )\necho "${history[2]}"\necho "${history[1]}"\necho "${#history}";\ninteger size="${#history}"\nsize+=1\necho "$size" / "${history[$size]}"\nlist=( "${history[$size]}" ${history[@]} )\n}' )
  57. (( ${+ZSH_EXECUTION_STRING} == 0 )) && { print -zr "${(F)input}"; return 0; }
  58. elif [[ "$1" = "-ooo" ]]; then
  59. local in='
  60. # This is an example code that is diverse and allows to test a theme
  61. text="An example quite long string $with variable in it"
  62. local param1="text $variable" param2='"'"'other $variable'"'"'
  63. math=$(( 10 + HISTSIZ + HISTSIZE + $SAVEHIST )) size=$(( 0 ))
  64. for (( ii = 1; ii <= size; ++ ii )); do
  65. if [[ "${cmds[ii]} string" = "| string" ]]
  66. then
  67. sidx=${buffer[(in:ii:)\$\(?#[^\\\\]\)]} # find opening $(
  68. (( sidx <= len )) && {
  69. eidx=${buffer[(b:sidx:ii)[^\\\\]\)]} # find closing )
  70. }
  71. fi
  72. done'
  73. (( ${+ZSH_EXECUTION_STRING} == 0 )) && { print -zr "$in"; return 0; }
  74. input+=( "$in" )
  75. input+=( "$in" )
  76. elif [[ "$1" = "-git" ]]; then
  77. local in="git lp
  78. git push origin master
  79. git commit
  80. git add safari.ini zdharma.ini
  81. git st .
  82. git diff --cached
  83. git commit --allow-empty
  84. git checkout themes/zdharma.ini
  85. git commit --amend
  86. git commit -m \"Example commit message\"
  87. git tag -a 'v1.18' -m 'Here-string is highlighted, descriptor-variables passed to exec are correctly highlighted'
  88. git tag -l -n9
  89. git checkout cb66b11
  90. "
  91. (( ${+ZSH_EXECUTION_STRING} == 0 )) && { print -zr "$in"; return 0; }
  92. input+=( "$in" )
  93. input+=( "$in" )
  94. elif [[ "$1" = "-hue" ]]; then
  95. local in="var=\$other; local var=\$other
  96. () { eval \"\$var\"; }
  97. case \$other in
  98. \$var)
  99. ( echo OK; )
  100. ;;
  101. esac
  102. sudo -i -s ls -1 /var/log
  103. () { ( eval \"command ls -1\" ); } argument"
  104. (( ${+ZSH_EXECUTION_STRING} == 0 )) && { print -zr "$in"; return 0; }
  105. input+=( "$in" "$in" )
  106. elif [[ "$1" = "-hol" ]]; then
  107. local in="var=\$( other )
  108. local var2=\$(( other + 1 ))
  109. () { eval \"\$var\"; }
  110. sudo -i -s ls -1 >/tmp/ls-log.txt /var/log
  111. IFS=\$'\\n' print -rl -- \$(command ls -1 | tee -a /tmp/ls-1.txt)
  112. var3=\$(( HISTSIZE + 10 + \$var ))
  113. local var4=\$( other command )
  114. touch \$(( HISTSIZE + \$SAVEHIST + 10 ))
  115. case \$other in
  116. \$var)
  117. ( echo OK; )
  118. ;;
  119. \$var3)
  120. ( if { true } { noglob echo yes } )
  121. esac
  122. ( builtin cd /var/log; ls -1; noglob cd \"/var/log\" 'log' )
  123. noglob cat <<<\"\$PATH\" | tr : \"\\n\"
  124. if [[ \"\$var\" -gt 10 ]]; then
  125. (( var = HISTSIZE + \$SAVEHIST ))
  126. fi
  127. /var/log
  128. sidx=\${buffer[(in:ii:)\\\$\\(?#[^\\\\\\\\]\\)]} # find opening cmd-subst
  129. {
  130. exec {MYFD}<&0 {MYFD2}>&1
  131. ( read <&\$MYFD line; echo \$line >&\$MYFD2 && { builtin print \${match[1]}Written. } )
  132. } always {
  133. (( MYFD > 0 )) && { print -rl -- -myfd:\$MYFD >&\$MYFD2 && print \"Sent.\" '(to filedescriptor)'; }
  134. }
  135. command sleep \"\$(( \$a + b + \${cde} + \${(s::)fgh[ijk]} + \\\$l + \\m + \\\" ))\"
  136. for (( i = 0; i <= 2; ++ i )) { print \$i; }
  137. "
  138. (( ${+ZSH_EXECUTION_STRING} == 0 )) && { print -zr "$in"; return 0; }
  139. input+=( "$in" )
  140. fi
  141. typeset -a long_input
  142. integer i
  143. for (( i=1; i<= 50; i ++ )); do
  144. long_input+=( "${input[@]}" )
  145. done
  146. typeset -F SECONDS
  147. SECONDS=0
  148. local right_brace_is_recognised_everywhere
  149. integer path_dirs_was_set multi_func_def ointeractive_comments
  150. -fast-highlight-fill-option-variables
  151. local BUFFER
  152. for BUFFER in "${long_input[@]}"; do
  153. reply=( )
  154. () {
  155. -fast-highlight-init
  156. -fast-highlight-process "" "$BUFFER" "0"
  157. -fast-highlight-string-process "" "$BUFFER"
  158. }
  159. done
  160. print "Running time: $SECONDS"
  161. zprof | head
  162. # File input?
  163. elif [[ -r "$1" ]]; then
  164. # Load from given file
  165. local BUFFER="$(<$1)"
  166. typeset -F SECONDS
  167. SECONDS=0
  168. reply=( )
  169. -fast-highlight-init
  170. local right_brace_is_recognised_everywhere
  171. integer path_dirs_was_set multi_func_def ointeractive_comments
  172. -fast-highlight-fill-option-variables
  173. () {
  174. -fast-highlight-process "" "$BUFFER" "0"
  175. -fast-highlight-string-process "" "$BUFFER"
  176. }
  177. print "Running time: $SECONDS"
  178. zprof | head
  179. # This output can be diffed to detect changes in operation
  180. if [[ -z "$2" ]]; then
  181. print -rl -- "${reply[@]}" >! out.parse
  182. else
  183. print -rl -- "${reply[@]}" >! "$2"
  184. fi
  185. else
  186. if [[ -z "$1" ]]; then
  187. print -u2 "Usage: ./parse.zsh {to-parse file} [region_highlight output file]"
  188. exit 2
  189. else
  190. print -u2 "Unreadable to-parse file \`$1', aborting"
  191. exit 3
  192. fi
  193. fi
  194. exit 0
  195. # vim:ft=zsh