Share
Embed
Indenting Erlang Code Automatically (in Vim)
The talk I gave on Budapest Erlang Factory Lite 2013.
http://www.erlang-factory.com/conference/Budapest2013/talks
Csaba Hoch
May 30, 2013
Annual Pro sale · Save $30
Have presentations of your own?
Share with private links, password protection, and more control over your work.
Transcript
120 slides
-
Csaba Hoch © 1999-2013 Erlang Solutions Ltd. Indenting Erlang Code
Automatically (in Vim) (Image source) 13 May 31, Friday -
© 1999-2013 Erlang Solutions Ltd. Overview 1. Erlang indentation in
Emacs 2. Indentation in Vim 3. Erlang indentation in Vim 2 + 13 May 31, Friday -
© 1999-2013 Erlang Solutions Ltd. Question: Which editor do you
use for Erlang development? 3 13 May 31, Friday -
© 1999-2013 Erlang Solutions Ltd. Question: Which editor do you
use for Erlang development? Survey in 2008, 209 answers: 3 Source: http://www.protest-project.eu/publications/survey.pdf 13 May 31, Friday -
© 1999-2013 Erlang Solutions Ltd. Question: Which editor do you
use for Erlang development? Survey in 2008, 209 answers: 3 Source: http://www.protest-project.eu/publications/survey.pdf Emacs 50% 13 May 31, Friday -
© 1999-2013 Erlang Solutions Ltd. Question: Which editor do you
use for Erlang development? Survey in 2008, 209 answers: 3 Source: http://www.protest-project.eu/publications/survey.pdf Vim 19% Emacs 50% 13 May 31, Friday -
© 1999-2013 Erlang Solutions Ltd. Question: Which editor do you
use for Erlang development? Survey in 2008, 209 answers: 3 Source: http://www.protest-project.eu/publications/survey.pdf Eclipse 12% Vim 19% Emacs 50% 13 May 31, Friday -
© 1999-2013 Erlang Solutions Ltd. Question: Which editor do you
use for Erlang development? Survey in 2008, 209 answers: 3 Source: http://www.protest-project.eu/publications/survey.pdf Other 19% Eclipse 12% Vim 19% Emacs 50% 13 May 31, Friday -
© 1999-2013 Erlang Solutions Ltd. Question: Which editor do you
use for Erlang development? Survey in 2008, 209 answers: 3 Source: http://www.protest-project.eu/publications/survey.pdf Other 19% Eclipse 12% Vim 19% Emacs 50% 13 May 31, Friday -
© 1999-2013 Erlang Solutions Ltd. Overview 1. Erlang indentation in
Emacs 2. Indentation in Vim 3. Erlang indentation in Vim 4 + 13 May 31, Friday -
© 1999-2013 Erlang Solutions Ltd. (defun my-indent-line () "Indent the
current line." [...implementation...]) (defun my-indent-region (begin end) "Indent each line in the region." [...implementation...]) (setq indent-line-function 'my-indent-line) (setq indent-region-function 'my-indent-region) Indentation in Emacs • Press TAB → indent line (by calling my-indent-line) • Press TAB while lines are selected → indent region (by calling my-indent-region) 5 elisp 13 May 31, Friday -
© 1999-2013 Erlang Solutions Ltd. (defun my-indent-line () "Indent the
current line." [...implementation...]) (defun my-indent-region (begin end) "Indent each line in the region." [...implementation...]) (setq indent-line-function 'my-indent-line) (setq indent-region-function 'my-indent-region) Indentation in Emacs • Press TAB → indent line (by calling my-indent-line) • Press TAB while lines are selected → indent region (by calling my-indent-region) 5 elisp 13 May 31, Friday -
© 1999-2013 Erlang Solutions Ltd. (defun my-indent-line () "Indent the
current line." [...implementation...]) (defun my-indent-region (begin end) "Indent each line in the region." [...implementation...]) (setq indent-line-function 'my-indent-line) (setq indent-region-function 'my-indent-region) Indentation in Emacs • Press TAB → indent line (by calling my-indent-line) • Press TAB while lines are selected → indent region (by calling my-indent-region) 5 elisp 13 May 31, Friday -
© 1999-2013 Erlang Solutions Ltd. (defun my-indent-line () "Indent the
current line." [...implementation...]) (defun my-indent-region (begin end) "Indent each line in the region." [...implementation...]) (setq indent-line-function 'my-indent-line) (setq indent-region-function 'my-indent-region) Indentation in Emacs • Press TAB → indent line (by calling my-indent-line) • Press TAB while lines are selected → indent region (by calling my-indent-region) 5 elisp 13 May 31, Friday -
© 1999-2013 Erlang Solutions Ltd. (defun my-indent-line () "Indent the
current line." [...implementation...]) (defun my-indent-region (begin end) "Indent each line in the region." [...implementation...]) (setq indent-line-function 'my-indent-line) (setq indent-region-function 'my-indent-region) Indentation in Emacs • Press TAB → indent line (by calling my-indent-line) • Press TAB while lines are selected → indent region (by calling my-indent-region) 5 elisp 13 May 31, Friday -
© 1999-2013 Erlang Solutions Ltd. Erlang indentation in Emacs •
erlang.el (shipped with Erlang/OTP) • Indentation algorithm: - Finds the beginning of the clause - Parses the text from the beginning of the clause and indents the lines intended to be indented - Uses a stack 6 13 May 31, Friday -
© 1999-2013 Erlang Solutions Ltd. func(X) -> case X of
1 -> ok; 2 -> error end. Erlang indentation example 7 13 May 31, Friday -
© 1999-2013 Erlang Solutions Ltd. func(X) -> case X of
1 -> ok; 2 -> error end. Erlang indentation example 1. User selects a line. 7 2 -> Line to indent 13 May 31, Friday -
© 1999-2013 Erlang Solutions Ltd. func(X) -> case X of
1 -> ok; 2 -> error end. Erlang indentation example 1. User selects a line. 7 2 -> Line to indent 2. User presses TAB. 13 May 31, Friday -
© 1999-2013 Erlang Solutions Ltd. func(X) -> case X of
1 -> ok; 2 -> error end. Erlang indentation example 1. User selects a line. 7 2 -> Line to indent 2. User presses TAB. 3. Find clause beginning. 13 May 31, Friday -
© 1999-2013 Erlang Solutions Ltd. func(X) -> case X of
1 -> ok; 2 -> error end. Erlang indentation example 1. User selects a line. 7 2 -> Line to indent 2. User presses TAB. 3. Find clause beginning. 4. Create empty stack. 13 May 31, Friday -
© 1999-2013 Erlang Solutions Ltd. func(X) -> case X of
1 -> ok; 2 -> error end. Erlang indentation example 1. User selects a line. 7 2 -> Line to indent 2. User presses TAB. 3. Find clause beginning. 4. Create empty stack. 5. Parse until the selected line. 13 May 31, Friday -
© 1999-2013 Erlang Solutions Ltd. func(X) -> case X of
1 -> ok; 2 -> error end. Erlang indentation example 1. User selects a line. 7 2 -> Line to indent 2. User presses TAB. 3. Find clause beginning. 4. Create empty stack. 5. Parse until the selected line. 13 May 31, Friday -
© 1999-2013 Erlang Solutions Ltd. func(X) -> case X of
1 -> ok; 2 -> error end. Erlang indentation example 1. User selects a line. 7 2 -> Line to indent 2. User presses TAB. 3. Find clause beginning. 4. Create empty stack. 5. Parse until the selected line. (, 4 13 May 31, Friday -
© 1999-2013 Erlang Solutions Ltd. func(X) -> case X of
1 -> ok; 2 -> error end. Erlang indentation example 1. User selects a line. 7 2 -> Line to indent 2. User presses TAB. 3. Find clause beginning. 4. Create empty stack. 5. Parse until the selected line. (, 4 13 May 31, Friday -
© 1999-2013 Erlang Solutions Ltd. func(X) -> case X of
1 -> ok; 2 -> error end. Erlang indentation example 1. User selects a line. 7 2 -> Line to indent 2. User presses TAB. 3. Find clause beginning. 4. Create empty stack. 5. Parse until the selected line. 13 May 31, Friday -
© 1999-2013 Erlang Solutions Ltd. func(X) -> case X of
1 -> ok; 2 -> error end. Erlang indentation example 1. User selects a line. 7 2 -> Line to indent 2. User presses TAB. 3. Find clause beginning. 4. Create empty stack. 5. Parse until the selected line. 13 May 31, Friday -
© 1999-2013 Erlang Solutions Ltd. func(X) -> case X of
1 -> ok; 2 -> error end. Erlang indentation example 1. User selects a line. 7 2 -> Line to indent 2. User presses TAB. 3. Find clause beginning. 4. Create empty stack. 5. Parse until the selected line. ->, 8 13 May 31, Friday -
© 1999-2013 Erlang Solutions Ltd. func(X) -> case X of
1 -> ok; 2 -> error end. Erlang indentation example 1. User selects a line. 7 2 -> Line to indent 2. User presses TAB. 3. Find clause beginning. 4. Create empty stack. 5. Parse until the selected line. ->, 8 13 May 31, Friday -
© 1999-2013 Erlang Solutions Ltd. func(X) -> case X of
1 -> ok; 2 -> error end. Erlang indentation example 1. User selects a line. 7 2 -> Line to indent 2. User presses TAB. 3. Find clause beginning. 4. Create empty stack. 5. Parse until the selected line. case, 4 ->, 8 13 May 31, Friday -
© 1999-2013 Erlang Solutions Ltd. func(X) -> case X of
1 -> ok; 2 -> error end. Erlang indentation example 1. User selects a line. 7 2 -> Line to indent 2. User presses TAB. 3. Find clause beginning. 4. Create empty stack. 5. Parse until the selected line. case, 4 ->, 8 13 May 31, Friday -
© 1999-2013 Erlang Solutions Ltd. func(X) -> case X of
1 -> ok; 2 -> error end. Erlang indentation example 1. User selects a line. 7 2 -> Line to indent 2. User presses TAB. 3. Find clause beginning. 4. Create empty stack. 5. Parse until the selected line. case, 4 ->, 8 ->, 2 13 May 31, Friday -
© 1999-2013 Erlang Solutions Ltd. func(X) -> case X of
1 -> ok; 2 -> error end. Erlang indentation example 1. User selects a line. 7 2 -> Line to indent 2. User presses TAB. 3. Find clause beginning. 4. Create empty stack. 5. Parse until the selected line. case, 4 ->, 8 ->, 2 13 May 31, Friday -
© 1999-2013 Erlang Solutions Ltd. func(X) -> case X of
1 -> ok; 2 -> error end. Erlang indentation example 1. User selects a line. 7 2 -> Line to indent 2. User presses TAB. 3. Find clause beginning. 4. Create empty stack. 5. Parse until the selected line. case, 4 ->, 8 13 May 31, Friday -
© 1999-2013 Erlang Solutions Ltd. func(X) -> case X of
1 -> ok; 2 -> error end. Erlang indentation example 1. User selects a line. 7 2 -> Line to indent 2. User presses TAB. 3. Find clause beginning. 4. Create empty stack. 5. Parse until the selected line. case, 4 ->, 8 6. Indent line: [{case, 4}|...] → 4 + 4 = 8 13 May 31, Friday -
© 1999-2013 Erlang Solutions Ltd. func(X) -> case X of
1 -> ok; 2 -> error end. Erlang indentation example 1. User selects a line. 7 2 -> Line to indent 2 -> 2. User presses TAB. 3. Find clause beginning. 4. Create empty stack. 5. Parse until the selected line. case, 4 ->, 8 6. Indent line: [{case, 4}|...] → 4 + 4 = 8 13 May 31, Friday -
© 1999-2013 Erlang Solutions Ltd. Overview 1. Erlang indentation in
Emacs 2. Indentation in Vim 3. Erlang indentation in Vim 8 + 13 May 31, Friday -
© 1999-2013 Erlang Solutions Ltd. function MyIndent() " Return the
number of spaces worth of indent " (-1 to keep current indent) [...implementation...] endfunction setlocal indentexpr=MyIndent() Indentation in Vim • Press == → indent line (MyIndent is evaluated) • Press = while lines are selected → indent selection (MyIndent is evaluated for each line) • MyIndent does not change the text, Vim does 9 Vim script Further reading: http://psy.swansea.ac.uk/staff/Carter/Vim/vim_indent.htm 13 May 31, Friday -
© 1999-2013 Erlang Solutions Ltd. function MyIndent() " Return the
number of spaces worth of indent " (-1 to keep current indent) [...implementation...] endfunction setlocal indentexpr=MyIndent() Indentation in Vim • Press == → indent line (MyIndent is evaluated) • Press = while lines are selected → indent selection (MyIndent is evaluated for each line) • MyIndent does not change the text, Vim does 9 Vim script Further reading: http://psy.swansea.ac.uk/staff/Carter/Vim/vim_indent.htm 13 May 31, Friday -
© 1999-2013 Erlang Solutions Ltd. function MyIndent() " Return the
number of spaces worth of indent " (-1 to keep current indent) [...implementation...] endfunction setlocal indentexpr=MyIndent() Indentation in Vim • Press == → indent line (MyIndent is evaluated) • Press = while lines are selected → indent selection (MyIndent is evaluated for each line) • MyIndent does not change the text, Vim does 9 Vim script Further reading: http://psy.swansea.ac.uk/staff/Carter/Vim/vim_indent.htm 13 May 31, Friday -
© 1999-2013 Erlang Solutions Ltd. function! MyIndent() let prev_lnum =
prevnonblank(v:lnum - 1) if prev_lnum == 0 | return 0 | endif let indent = indent(prev_lnum) if getline(prev_lnum) =~# '\<begin\>' let indent += &shiftwidth endif if getline(v:lnum) =~# '\<end\>' let indent -= &shiftwidth endif return indent endfunction setlocal indentexpr=MyIndent() A basic indentation example 10 13 May 31, Friday -
© 1999-2013 Erlang Solutions Ltd. function! MyIndent() let prev_lnum =
prevnonblank(v:lnum - 1) if prev_lnum == 0 | return 0 | endif let indent = indent(prev_lnum) if getline(prev_lnum) =~# '\<begin\>' let indent += &shiftwidth endif if getline(v:lnum) =~# '\<end\>' let indent -= &shiftwidth endif return indent endfunction setlocal indentexpr=MyIndent() A basic indentation example 10 13 May 31, Friday -
© 1999-2013 Erlang Solutions Ltd. function! MyIndent() let prev_lnum =
prevnonblank(v:lnum - 1) if prev_lnum == 0 | return 0 | endif let indent = indent(prev_lnum) if getline(prev_lnum) =~# '\<begin\>' let indent += &shiftwidth endif if getline(v:lnum) =~# '\<end\>' let indent -= &shiftwidth endif return indent endfunction setlocal indentexpr=MyIndent() A basic indentation example 10 13 May 31, Friday -
© 1999-2013 Erlang Solutions Ltd. function! MyIndent() let prev_lnum =
prevnonblank(v:lnum - 1) if prev_lnum == 0 | return 0 | endif let indent = indent(prev_lnum) if getline(prev_lnum) =~# '\<begin\>' let indent += &shiftwidth endif if getline(v:lnum) =~# '\<end\>' let indent -= &shiftwidth endif return indent endfunction setlocal indentexpr=MyIndent() A basic indentation example 10 13 May 31, Friday -
© 1999-2013 Erlang Solutions Ltd. function! MyIndent() let prev_lnum =
prevnonblank(v:lnum - 1) if prev_lnum == 0 | return 0 | endif let indent = indent(prev_lnum) if getline(prev_lnum) =~# '\<begin\>' let indent += &shiftwidth endif if getline(v:lnum) =~# '\<end\>' let indent -= &shiftwidth endif return indent endfunction setlocal indentexpr=MyIndent() A basic indentation example 10 13 May 31, Friday -
© 1999-2013 Erlang Solutions Ltd. function! MyIndent() let prev_lnum =
prevnonblank(v:lnum - 1) if prev_lnum == 0 | return 0 | endif let indent = indent(prev_lnum) if getline(prev_lnum) =~# '\<begin\>' let indent += &shiftwidth endif if getline(v:lnum) =~# '\<end\>' let indent -= &shiftwidth endif return indent endfunction setlocal indentexpr=MyIndent() A basic indentation example 10 13 May 31, Friday -
© 1999-2013 Erlang Solutions Ltd. function! MyIndent() let prev_lnum =
prevnonblank(v:lnum - 1) if prev_lnum == 0 | return 0 | endif let indent = indent(prev_lnum) if getline(prev_lnum) =~# '\<begin\>' let indent += &shiftwidth endif if getline(v:lnum) =~# '\<end\>' let indent -= &shiftwidth endif return indent endfunction setlocal indentexpr=MyIndent() A basic indentation example 10 13 May 31, Friday -
© 1999-2013 Erlang Solutions Ltd. Problems • Cannot handle multiple
begins/ends in the same line → we could count them 11 13 May 31, Friday -
© 1999-2013 Erlang Solutions Ltd. Problems • Cannot handle multiple
begins/ends in the same line → we could count them 11 my_function(Parameter1, [...], ParameterN) • How to indent parameter lists? 13 May 31, Friday -
© 1999-2013 Erlang Solutions Ltd. Problems • Cannot handle multiple
begins/ends in the same line → we could count them 11 my_function(Parameter1, [...], ParameterN) • Vim’s secret weapon: the searchpair function • How to indent parameter lists? 13 May 31, Friday -
© 1999-2013 Erlang Solutions Ltd. searchpair('\<receive\>', '', '\<end\>', 'bW') searchpair
12 start 13 May 31, Friday -
© 1999-2013 Erlang Solutions Ltd. searchpair('\<receive\>', '', '\<end\>', 'bW') searchpair
12 start (middle) 13 May 31, Friday -
© 1999-2013 Erlang Solutions Ltd. searchpair('\<receive\>', '', '\<end\>', 'bW') searchpair
12 start (middle) end 13 May 31, Friday -
© 1999-2013 Erlang Solutions Ltd. searchpair('\<receive\>', '', '\<end\>', 'bW') searchpair
12 b = backwards W = stop at the beginning of the file start (middle) end 13 May 31, Friday -
© 1999-2013 Erlang Solutions Ltd. searchpair('\<receive\>', '', '\<end\>', 'bW') searchpair
12 receive {_, ok} -> receive {_, ok} -> ok end after Timeout -> timeout end b = backwards W = stop at the beginning of the file start (middle) end 13 May 31, Friday -
© 1999-2013 Erlang Solutions Ltd. searchpair('\<receive\>', '', '\<end\>', 'bW') searchpair
12 receive {_, ok} -> receive {_, ok} -> ok end after Timeout -> timeout end b = backwards W = stop at the beginning of the file start (middle) end 13 May 31, Friday -
© 1999-2013 Erlang Solutions Ltd. searchpair('\<receive\>', '', '\<end\>', 'bW') searchpair
12 receive {_, ok} -> receive {_, ok} -> ok end after Timeout -> timeout end b = backwards W = stop at the beginning of the file start (middle) end 13 May 31, Friday -
© 1999-2013 Erlang Solutions Ltd. searchpair('\<receive\>', '', '\<end\>', 'bW') searchpair
12 receive {_, ok} -> receive {_, ok} -> ok end after Timeout -> timeout end b = backwards W = stop at the beginning of the file start (middle) end 13 May 31, Friday -
© 1999-2013 Erlang Solutions Ltd. searchpair('\<receive\>', '', '\<end\>', 'bW') searchpair
12 receive {_, ok} -> receive {_, ok} -> ok end after Timeout -> timeout end b = backwards W = stop at the beginning of the file start (middle) end 13 May 31, Friday -
© 1999-2013 Erlang Solutions Ltd. searchpair – string, comments? 13
searchpair('\<receive\>', '', '\<end\>', 'bW') receive {_, gift} -> io:format("receive gift") end start end 13 May 31, Friday -
© 1999-2013 Erlang Solutions Ltd. searchpair – string, comments? 13
searchpair('\<receive\>', '', '\<end\>', 'bW') receive {_, gift} -> io:format("receive gift") end start end 13 May 31, Friday -
© 1999-2013 Erlang Solutions Ltd. searchpair('\<receive\>', '', '\<end\>', 'bW', \
'synIDattr(synID(line("."), col("."), 0), "name") =~? \ "string\\|comment"') searchpair – string, comments? 14 receive {_, gift} -> io:format("receive gift") end skip 13 May 31, Friday -
© 1999-2013 Erlang Solutions Ltd. function! MyIndent() let [lnum, col]
= \ searchpairpos('(', '', ')', 'bW', \ 'synIDattr(synID(line("."),col("."),0),"name") =~? \ "string\\|comment"') if lnum == 0 | return 0 | endif let word_col = match(getline(lnum), '[^ \t]', col) if word_col == -1 return word_col else return indent(lnum) + &shiftwidth endif endfunction Example using searchpair 15 13 May 31, Friday -
© 1999-2013 Erlang Solutions Ltd. function! MyIndent() let [lnum, col]
= \ searchpairpos('(', '', ')', 'bW', \ 'synIDattr(synID(line("."),col("."),0),"name") =~? \ "string\\|comment"') if lnum == 0 | return 0 | endif let word_col = match(getline(lnum), '[^ \t]', col) if word_col == -1 return word_col else return indent(lnum) + &shiftwidth endif endfunction Example using searchpair 15 13 May 31, Friday -
© 1999-2013 Erlang Solutions Ltd. function! MyIndent() let [lnum, col]
= \ searchpairpos('(', '', ')', 'bW', \ 'synIDattr(synID(line("."),col("."),0),"name") =~? \ "string\\|comment"') if lnum == 0 | return 0 | endif let word_col = match(getline(lnum), '[^ \t]', col) if word_col == -1 return word_col else return indent(lnum) + &shiftwidth endif endfunction Example using searchpair 15 13 May 31, Friday -
© 1999-2013 Erlang Solutions Ltd. function! MyIndent() let [lnum, col]
= \ searchpairpos('(', '', ')', 'bW', \ 'synIDattr(synID(line("."),col("."),0),"name") =~? \ "string\\|comment"') if lnum == 0 | return 0 | endif let word_col = match(getline(lnum), '[^ \t]', col) if word_col == -1 return word_col else return indent(lnum) + &shiftwidth endif endfunction Example using searchpair 15 13 May 31, Friday -
© 1999-2013 Erlang Solutions Ltd. function! MyIndent() let [lnum, col]
= \ searchpairpos('(', '', ')', 'bW', \ 'synIDattr(synID(line("."),col("."),0),"name") =~? \ "string\\|comment"') if lnum == 0 | return 0 | endif let word_col = match(getline(lnum), '[^ \t]', col) if word_col == -1 return word_col else return indent(lnum) + &shiftwidth endif endfunction Example using searchpair 15 my_func(Param1, Param2 13 May 31, Friday -
© 1999-2013 Erlang Solutions Ltd. function! MyIndent() let [lnum, col]
= \ searchpairpos('(', '', ')', 'bW', \ 'synIDattr(synID(line("."),col("."),0),"name") =~? \ "string\\|comment"') if lnum == 0 | return 0 | endif let word_col = match(getline(lnum), '[^ \t]', col) if word_col == -1 return word_col else return indent(lnum) + &shiftwidth endif endfunction Example using searchpair 15 my_func(Param1, Param2 my_func( Param1, 13 May 31, Friday -
© 1999-2013 Erlang Solutions Ltd. Overview 1. Erlang indentation in
Emacs 2. Indentation in Vim 3. Erlang indentation in Vim 16 + 13 May 31, Friday -
© 1999-2013 Erlang Solutions Ltd. 17 Status • Handles everything
but type specifications • Mostly Emacs compatible • Versions - Soon will be shipped with Vim 7.4 - Newest version: github.com/hcs42/vim-erlang + 13 May 31, Friday -
© 1999-2013 Erlang Solutions Ltd. Design • Main approach -
Tokenizing - Parsing backwards • Optimizations - searchpair calls - Token cache 18 Further reading: Emacs Documentation/Automatic Indentation of code + 13 May 31, Friday -
© 1999-2013 Erlang Solutions Ltd. {ok, FD} = case File
of Tokenization 19 [['{', 0, 0], ['ok', 1, 1], [',' 3, 3], ['FD', 5, 5], ['}', 7, 7], ['=', 9, 9], ['case', 11, 11], ['File', 16, 16], ['of', 21, 21]] + 13 May 31, Friday -
© 1999-2013 Erlang Solutions Ltd. {ok, FD} = case File
of Tokenization 19 [['{', 0, 0], ['ok', 1, 1], [',' 3, 3], ['FD', 5, 5], ['}', 7, 7], ['=', 9, 9], ['case', 11, 11], ['File', 16, 16], ['of', 21, 21]] Token text + 13 May 31, Friday -
© 1999-2013 Erlang Solutions Ltd. {ok, FD} = case File
of Tokenization 19 [['{', 0, 0], ['ok', 1, 1], [',' 3, 3], ['FD', 5, 5], ['}', 7, 7], ['=', 9, 9], ['case', 11, 11], ['File', 16, 16], ['of', 21, 21]] Token text Virtual column + 13 May 31, Friday -
© 1999-2013 Erlang Solutions Ltd. {ok, FD} = case File
of Tokenization 19 [['{', 0, 0], ['ok', 1, 1], [',' 3, 3], ['FD', 5, 5], ['}', 7, 7], ['=', 9, 9], ['case', 11, 11], ['File', 16, 16], ['of', 21, 21]] Token text Virtual column Real column + 13 May 31, Friday -
© 1999-2013 Erlang Solutions Ltd. Tokenization challenge 20 simple() ->
+ 13 May 31, Friday -
© 1999-2013 Erlang Solutions Ltd. Tokenization challenge 20 my_function() ->
"this is a not so multiline string". simple() -> + 13 May 31, Friday -
© 1999-2013 Erlang Solutions Ltd. Tokenization challenge • Multiline strings
and atoms 20 my_function() -> "this is a not so multiline string". simple() -> 'this is a multiline function name'() -> ok. + 13 May 31, Friday -
© 1999-2013 Erlang Solutions Ltd. func(X) -> case X of
1 -> ok; 2 -> error end. Parsing backwards example 21 + 13 May 31, Friday -
© 1999-2013 Erlang Solutions Ltd. func(X) -> case X of
1 -> ok; 2 -> error end. Parsing backwards example 1. User selects a line. 21 2 -> Line to indent + 13 May 31, Friday -
© 1999-2013 Erlang Solutions Ltd. func(X) -> case X of
1 -> ok; 2 -> error end. Parsing backwards example 1. User selects a line. 21 2 -> Line to indent 2. User presses = + 13 May 31, Friday -
© 1999-2013 Erlang Solutions Ltd. func(X) -> case X of
1 -> ok; 2 -> error end. Parsing backwards example 1. User selects a line. 21 2 -> Line to indent 2. User presses = 3. Create empty stack. + 13 May 31, Friday -
© 1999-2013 Erlang Solutions Ltd. func(X) -> case X of
1 -> ok; 2 -> error end. Parsing backwards example 1. User selects a line. 21 2 -> Line to indent 2. User presses = 3. Create empty stack. 4. Parse backwards. + 13 May 31, Friday -
© 1999-2013 Erlang Solutions Ltd. func(X) -> case X of
1 -> ok; 2 -> error end. Parsing backwards example 1. User selects a line. 21 2 -> Line to indent 2. User presses = 3. Create empty stack. 4. Parse backwards. + 13 May 31, Friday -
© 1999-2013 Erlang Solutions Ltd. func(X) -> case X of
1 -> ok; 2 -> error end. Parsing backwards example 1. User selects a line. 21 2 -> Line to indent 2. User presses = 3. Create empty stack. 4. Parse backwards. ;, 2 + 13 May 31, Friday -
© 1999-2013 Erlang Solutions Ltd. func(X) -> case X of
1 -> ok; 2 -> error end. Parsing backwards example 1. User selects a line. 21 2 -> Line to indent 2. User presses = 3. Create empty stack. 4. Parse backwards. ;, 2 + 13 May 31, Friday -
© 1999-2013 Erlang Solutions Ltd. func(X) -> case X of
1 -> ok; 2 -> error end. Parsing backwards example 1. User selects a line. 21 2 -> Line to indent 2. User presses = 3. Create empty stack. 4. Parse backwards. ;, 2 ->, 2 + 13 May 31, Friday -
© 1999-2013 Erlang Solutions Ltd. func(X) -> case X of
1 -> ok; 2 -> error end. Parsing backwards example 1. User selects a line. 21 2 -> Line to indent 2. User presses = 3. Create empty stack. 4. Parse backwards. ;, 2 ->, 2 + 13 May 31, Friday -
© 1999-2013 Erlang Solutions Ltd. func(X) -> case X of
1 -> ok; 2 -> error end. Parsing backwards example 1. User selects a line. 21 2 -> Line to indent 2. User presses = 3. Create empty stack. 4. Parse backwards. ;, 2 ->, 2 of, 11 + 13 May 31, Friday -
© 1999-2013 Erlang Solutions Ltd. func(X) -> case X of
1 -> ok; 2 -> error end. Parsing backwards example 1. User selects a line. 21 2 -> Line to indent 2. User presses = 3. Create empty stack. 4. Parse backwards. ;, 2 ->, 2 of, 11 + 13 May 31, Friday -
© 1999-2013 Erlang Solutions Ltd. func(X) -> case X of
1 -> ok; 2 -> error end. Parsing backwards example 1. User selects a line. 21 2 -> Line to indent 2. User presses = 3. Create empty stack. 4. Parse backwards. ;, 2 ->, 2 + 13 May 31, Friday -
© 1999-2013 Erlang Solutions Ltd. func(X) -> case X of
1 -> ok; 2 -> error end. Parsing backwards example 1. User selects a line. 21 2 -> Line to indent 2. User presses = 3. Create empty stack. 4. Parse backwards. ;, 2 5. Token == {'case', 4} Stack == [{'->',_}, {';', _}] → Indent line: 4 + 4 = 8 ->, 2 + 13 May 31, Friday -
© 1999-2013 Erlang Solutions Ltd. func(X) -> case X of
1 -> ok; 2 -> error end. Parsing backwards example 1. User selects a line. 21 2 -> Line to indent 2. User presses = 3. Create empty stack. 4. Parse backwards. ;, 2 5. Token == {'case', 4} Stack == [{'->',_}, {';', _}] → Indent line: 4 + 4 = 8 2 -> ->, 2 + 13 May 31, Friday -
© 1999-2013 Erlang Solutions Ltd. Parsing – semicolon and arrow
22 A == 1, B == 2, C == 3; Var -> A == 1, B == 2, C == 3; Var -> + 13 May 31, Friday -
© 1999-2013 Erlang Solutions Ltd. case X of {error, Error}
-> A == 1, B == 2, C == 3; [...] end Parsing – semicolon and arrow 22 A == 1, B == 2, C == 3; Var -> A == 1, B == 2, C == 3; Var -> + 13 May 31, Friday -
© 1999-2013 Erlang Solutions Ltd. case X of {error, Error}
-> A == 1, B == 2, C == 3; [...] end case X of {error, Error} when A == 1, B == 2, C == 3; [...]; ok -> [...] end Parsing – semicolon and arrow 22 A == 1, B == 2, C == 3; Var -> A == 1, B == 2, C == 3; Var -> + 13 May 31, Friday -
© 1999-2013 Erlang Solutions Ltd. case X of {error, Error}
-> A == 1, B == 2, C == 3; [...] end case X of {error, Error} when A == 1, B == 2, C == 3; [...]; ok -> [...] end Parsing – semicolon and arrow 22 A == 1, B == 2, C == 3; Var -> A == 1, B == 2, C == 3; Var -> + 13 May 31, Friday -
© 1999-2013 Erlang Solutions Ltd. case X of {error, Error}
-> A == 1, B == 2, C == 3; [...] end case X of {error, Error} when A == 1, B == 2, C == 3; [...]; ok -> [...] end Parsing – semicolon and arrow 22 Var -> [...] Var -> [...] + 13 May 31, Friday -
© 1999-2013 Erlang Solutions Ltd. when -> ; case X
of {error, Error} -> A == 1, B == 2, C == 3; [...] end case X of {error, Error} when A == 1, B == 2, C == 3; [...]; ok -> [...] end Parsing – semicolon and arrow 22 Var -> [...] Var -> [...] ; ; -> -> when -> when -> ; ; when when -> when -> ; when -> ; + 13 May 31, Friday -
© 1999-2013 Erlang Solutions Ltd. when -> ; case X
of {error, Error} -> A == 1, B == 2, C == 3; [...] end case X of {error, Error} when A == 1, B == 2, C == 3; [...]; ok -> [...] end Parsing – semicolon and arrow 22 Var -> [...] Var -> [...] ; ; -> -> when -> when -> ; ; when when -> when -> ; when -> ; Branch body + 13 May 31, Friday -
© 1999-2013 Erlang Solutions Ltd. when -> ; case X
of {error, Error} -> A == 1, B == 2, C == 3; [...] end case X of {error, Error} when A == 1, B == 2, C == 3; [...]; ok -> [...] end Parsing – semicolon and arrow 22 Var -> [...] Var -> [...] ; ; -> -> when -> when -> ; ; when when -> when -> ; when -> ; Branch body Branch pattern + 13 May 31, Friday -
© 1999-2013 Erlang Solutions Ltd. when -> ; case X
of {error, Error} -> A == 1, B == 2, C == 3; [...] end case X of {error, Error} when A == 1, B == 2, C == 3; [...]; ok -> [...] end Parsing – semicolon and arrow 22 Var -> [...] Var -> [...] ; ; -> -> when -> when -> ; ; when when -> when -> ; when -> ; Branch body Branch pattern Guard Branch pattern + 13 May 31, Friday -
© 1999-2013 Erlang Solutions Ltd. Parsing – catch and fun
• catch 23 try EXPRESSIONS catch try EXPRESSIONS catch + 13 May 31, Friday -
© 1999-2013 Erlang Solutions Ltd. Parsing – catch and fun
• catch 23 try EXPRESSIONS catch _:Error -> {error, Error} end try EXPRESSIONS catch f() + 13 May 31, Friday -
© 1999-2013 Erlang Solutions Ltd. Parsing – catch and fun
• catch 23 22 after -> cleanup() end try EXPRESSIONS catch _:Error -> {error, Error} end try EXPRESSIONS catch f() f(), + 13 May 31, Friday -
© 1999-2013 Erlang Solutions Ltd. Parsing – catch and fun
• catch 23 22 after -> cleanup() end try EXPRESSIONS catch _:Error -> {error, Error} end try EXPRESSIONS catch f() f(), + 13 May 31, Friday -
© 1999-2013 Erlang Solutions Ltd. Parsing – catch and fun
• catch 23 22 after -> cleanup() end try EXPRESSIONS catch _:Error -> {error, Error} end try EXPRESSIONS catch f() f(), • Variable, number, string, atom • end ) ] } $. + 13 May 31, Friday -
© 1999-2013 Erlang Solutions Ltd. Parsing – catch and fun
• catch 23 22 after -> cleanup() end try EXPRESSIONS catch _:Error -> {error, Error} end try EXPRESSIONS catch f() f(), • after and andalso band begin bnot bor bsl bsr bxor case catch div not or orelse rem try xor -> == /= =< < >= > =:= =/= + - * / ++ -- < > ( [ { ! , • Variable, number, string, atom • end ) ] } $. + 13 May 31, Friday -
© 1999-2013 Erlang Solutions Ltd. Parsing – catch and fun
• catch 23 22 after -> cleanup() end try EXPRESSIONS catch _:Error -> {error, Error} end try EXPRESSIONS catch f() f(), • after and andalso band begin bnot bor bsl bsr bxor case catch div not or orelse rem try xor -> == /= =< < >= > =:= =/= + - * / ++ -- < > ( [ { ! , • Variable, number, string, atom • end ) ] } $. • fun fun() … end fun init/0 + 13 May 31, Friday -
© 1999-2013 Erlang Solutions Ltd. Parsing – blocks and searchpair
24 flags start (middle) end searchpair('\<receive\>', '', '\<end\>', 'bW', \ 'synIDattr(synID(line("."), col("."), 0), "name") =~? \ "string\\|comment"') skip + 13 May 31, Friday -
© 1999-2013 Erlang Solutions Ltd. searchpair( \ '\<\%(case\|try\|begin\|receive\|if\)\>\|' . \
'\<fun\>\%(\s\|\n\|%.*$\)*(', \ '', \ '\<end\>', \ 'bW', \ 'synIDattr(synID(line("."), col("."), 0), "name") =~? \ "string\\|comment"') Parsing – blocks and searchpair 24 flags start (middle) end searchpair('\<receive\>', '', '\<end\>', 'bW', \ 'synIDattr(synID(line("."), col("."), 0), "name") =~? \ "string\\|comment"') skip + 13 May 31, Friday -
© 1999-2013 Erlang Solutions Ltd. searchpair( \ '\<\%(case\|try\|begin\|receive\|if\)\>\|' . \
'\<fun\>\%(\s\|\n\|%.*$\)*(', \ '', \ '\<end\>', \ 'bW', \ 'synIDattr(synID(line("."), col("."), 0), "name") =~? \ "string\\|comment"') Parsing – blocks and searchpair 24 flags start (middle) end searchpair('\<receive\>', '', '\<end\>', 'bW', \ 'synIDattr(synID(line("."), col("."), 0), "name") =~? \ "string\\|comment"') skip fun() … end – match fun init/0 – no match + 13 May 31, Friday -
© 1999-2013 Erlang Solutions Ltd. 25 Conclusion • Parsing backwards
is doable 13 May 31, Friday -
© 1999-2013 Erlang Solutions Ltd. 25 Conclusion • Parsing backwards
is doable • Consider using a stack 13 May 31, Friday -
© 1999-2013 Erlang Solutions Ltd. 25 Conclusion • Parsing backwards
is doable • Consider using a stack • Know your libraries (searchpair) 13 May 31, Friday -
© 1999-2013 Erlang Solutions Ltd. 25 Conclusion • Parsing backwards
is doable • Consider using a stack • Know your libraries (searchpair) • Follow the standard indentation style 13 May 31, Friday -
© 1999-2013 Erlang Solutions Ltd. 26 Clean code with Vim
Thank you! (Image source) 13 May 31, Friday