forked from alienrobotwizard/emacs-starter-kit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pig-mode.el
148 lines (131 loc) · 5.84 KB
/
pig-mode.el
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
; -*- Mode: Emacs-Lisp -*-
;;; pig-mode.el -- Major mode for Pig files
;; Software License Agreement (BSD License)
;;
;; Copyright (c) 2009 Sergei Matusevich <[email protected]>
;; All rights reserved.
;;
;; Redistribution and use in source and binary forms, with or without
;; modification, are permitted provided that the following conditions
;; are met:
;; 1. Redistributions of source code must retain the above copyright
;; notice, this list of conditions and the following disclaimer.
;; 2. Redistributions in binary form must reproduce the above copyright
;; notice, this list of conditions and the following disclaimer in the
;; documentation and/or other materials provided with the distribution.
;; 3. The name of the author may not be used to endorse or promote products
;; derived from this software without specific prior written permission.
;;
;; THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
;; IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
;; OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
;; IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
;; INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
;; NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
;; DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
;; THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
;; (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
;; THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
;; Put this file into your Emacs lisp path (eg. site-lisp)
;; and add the following line to your ~/.emacs file:
;;
;; (require 'pig-mode)
(require 'font-lock)
;; the command to comment/uncomment text
(defun pig-comment-dwim (arg)
(interactive "*P")
(require 'newcomment)
(let ((deactivate-mark nil) (comment-start "--") (comment-end ""))
(comment-dwim arg)))
(defvar pig-mode-hook nil)
(defvar pig-mode-map
(let ((keymap (make-sparse-keymap)))
(define-key keymap (kbd "RET") 'newline-and-indent)
keymap)
"Keymap for pig major mode")
;;(add-to-list 'auto-mode-alist '("\\.pig\\'" . pig-mode))
(defconst pig-font-lock-keywords
`((,(regexp-opt
'("LOAD" "STORE" "FILTER" "FOREACH" "ORDER" "ARRANGE"
"DISTINCT" "COGROUP" "JOIN" "CROSS" "UNION" "SPLIT" "INTO"
"IF" "ALL" "ANY" "AS" "BY" "USING" "INNER" "OUTER" "PARALLEL"
"GROUP" "CONTINUOUSLY" "WINDOW" "TUPLES" "GENERATE" "EVAL"
"DEFINE" "INPUT" "OUTPUT" "SHIP" "CACHE" "STREAM" "THROUGH"
"SECONDS" "MINUTES" "HOURS" "ASC" "DESC"
"NULL" "AND" "OR" "NOT" "EQ" "NEQ" "GT" "LT" "GTE" "LTE"
"MATCHES" "IS" "DUMP" "ILLUSTRATE" "DESCRIBE")
'words)
(1 font-lock-keyword-face))
("^ *\\(REGISTER\\) *\\([^;]+\\)"
(1 font-lock-keyword-face)
(2 font-lock-string-face))
(,(concat
(regexp-opt
'("FLATTEN" "SUM" "COUNT" "MIN" "MAX" "AVG" "ARITY" "TOKENIZE"
"DIFF" "SIZE" "CONCAT"
"BinStorage" "PigStorage" "TextLoader" "PigDump" "IsEmpty")
'words)
"(")
(1 font-lock-function-name-face))
("\\<\\([0-9]+[lL]\\|\\([0-9]+\\.?[0-9]*\\|\\.[0-9]+\\)\\([eE][-+]?[0-9]+\\)?[fF]?\\)\\>"
. font-lock-constant-face)
("\\<$[0-9]+\\>" . font-lock-variable-name-face)
(,(regexp-opt
'("chararray" "bytearray" "int" "long" "float" "double" "tuple"
"bag" "map")
'words)
(1 font-lock-type-face)))
"regexps to highlight in pig mode")
(defvar pig-mode-syntax-table
(let ((st (make-syntax-table)))
(modify-syntax-entry ?_ "w" st)
(modify-syntax-entry ?- ". 56" st)
(modify-syntax-entry ?- ". 12b" st)
(modify-syntax-entry ?/ ". 1456" st)
(modify-syntax-entry ?* ". 23" st)
(modify-syntax-entry ?\n "> b" st)
(modify-syntax-entry ?\" "\"" st)
(modify-syntax-entry ?\' "\"" st)
(modify-syntax-entry ?\` "\"" st)
st)
"Syntax table for pig mode")
(defun pig-indent-line ()
"Indent current line as Pig code"
(interactive)
(indent-line-to (save-excursion
(beginning-of-line)
(if (looking-at ".*}[ \t]*;[ \t]*$")
(pig-statement-indentation)
(forward-line -1)
(while (and (not (bobp)) (looking-at "^[ \t]*$"))
(forward-line -1) )
(cond
((bobp) 0)
((looking-at "^[ \t]*--") (current-indentation))
((looking-at ".*;[ \t]*$") (pig-statement-indentation))
(t (+ (pig-statement-indentation) default-tab-width)) ) ) )) )
(defun pig-statement-indentation ()
(save-excursion
(beginning-of-line)
(cond
((bobp) 0)
((looking-at ".*\\(}[ \t]*;\\|)\\)[ \t]*$")
(end-of-line)
(backward-list)
(pig-statement-indentation) )
((search-backward-regexp "[{;][ \t]*$" nil t)
(forward-line 1)
(beginning-of-line)
(while (and (looking-at "^[ \t]*\\(--.*\\)?$")
(save-excursion (end-of-line) (not (eobp))) )
(forward-line 1) )
(current-indentation) )
(t 0) ) ) )
(define-derived-mode pig-mode fundamental-mode "pig"
"Major mode for editing Yahoo! .pig files"
:syntax-table pig-mode-syntax-table
(define-key pig-mode-map [remap comment-dwim] 'pig-comment-dwim)
(set (make-local-variable 'font-lock-defaults) '(pig-font-lock-keywords nil t))
(set (make-local-variable 'indent-line-function) 'pig-indent-line) )
(provide 'pig-mode)
;;; end of pig-mode.el