Indentation-sensitive Erlang 3
Ulf Wiger - Ulf Wiger - June 11, 2008So, maybe I’m thick enough not to realise in advance when I’m barking up the wrong tree, or perhaps I just like to follow things through to the bitter end, just to know what exactly didn’t work…?
I’ve gone one more round with my indentation-sensitive Erlang scanner/parser. For a while, it looked like I was winning, but eventually, I had to admit defeat in the face of funs and record constructors.
Again, the approach I thought I’d take was to add indentation tokens to the normal scanner, and then add rule clauses to the normal grammar to make it understand both indentation-sensitive code, and all the code you’re accustomed to writing. The normal parser is an LR(1) grammar, which means that the additions have to be quite symmetrical in order to work.
This seemed to work in just about all the places that mattered, but I was stumped by funs and record constructors. The main with funs was that you could no longer write them in the conventional way (failing one of my preconditions), and record constructors usually cannot be written without including a few commans in a way that was …uhm, less than obvious.
I include the test module, which pretty much illustrates what works, and what doesn’t. At this point, I doubt that I can achieve better than a 90% solution, which is probably just good enough to eventually drive people crazy.
It’s been fun, though. I’m have no particular craving for indentation-sensitive syntax myself, so I thought I’d tackle this as a learning experience. Not for the first time, I feel like concluding that retrofitting concepts onto existing programming languages is usually very difficult to do well.
The code is at http://svn.ulf.wiger.net/indent/branches/0.3
-module(test). -record(r,{a,b}). -compile(export_all). -scan(indentation). f(X) -> X+2 g(X) -> X+4 . % dot can either be 'outdented' or % terminating last line g1(X) -> X+4. h(X) -> Y = case X of a -> {a} b -> {b} end % end is optional Y h1(X) -> Y = case X of a -> {a} b -> {b} Y h2(X) -> case X of a -> 1 b -> 2 h3(X) -> case X of a -> 1; % must use semicolon hereb -> 2 i(a) -> a i(b) -> b j(A,B) -> {A B} j1(A,B,C) -> {A B % indents must be greater than 1; C} % else they count as aligned k() -> S = "a string " "spanning multiple " "lines" {S} l0() -> fun(1) -> 1; (2) -> 2 end. l1() -> fun % must break line here and indent.
(1) -> 1 (2) -> 2 %%% This, alas, doesn't work
%%% %%% l2() -> %%% fun(1) -> %%% 1; %%% (2) -> %%% 2 %%% end m() -> #r{a = 1, b = 2}. m1() -> R = #r{a = 1} R#r.a %%% indentation syntax works poorly for record assignment. %%% Both commas are needed
m2() -> R = #r{a = 1, b = 2}, R test() -> 2 = f(0), 4 = f(2), 4 = g(0), 4 = g1(0), 8 = g(4), {a} = h(a), {b} = h(b), {a} = h1(a), {b} = h1(b), 1 = h2(a), 2 = h2(b), 1 = h3(a), 2 = h3(b), a = i(a), b = i(b), {a,b} = j(a,b), {a,b,c} = j1(a,b,c), {"a string spanning multiple lines"} = k(), F0 = l0(), 1 = F0(1), 2 = F0(2), F1 = l1(), 1 = F1(1), 2 = F1(2), {r,1,2} = m(), 1 = m1(), ok.
Categories: Blogs Ulf Wiger
Comments
No comments so far, you could be the first.Add comment
Erlang on Twitter
» Mgnadya (Mega Nadya Rustanti): WooooRT @erlangtriaji: Maap bude salah liat ._. RT @Mgnadya: Elaaaang bukan erlang -_- RT @erlangtriaji: sama” RT… http://t.co/O0DxJgAr
» levicole (Levi Kennedy): @pharkmillups Still seeing it. I might just have to manually install it. The version of Erlang required by Riak is not current version in HB
» quercialwji2 (Quercia Quinn): @Maro7861 http://t.co/pPiIpTCx
» quercialwji2 (Quercia Quinn): @RolArens http://t.co/pPiIpTCx
» quercialwji2 (Quercia Quinn): @kreese555 http://t.co/pPiIpTCx
» erlang (Andreas Åkre Solberg): RT @SaraJChipps: Node.js is like taking a bubble bath in JavaScript.
» trabajosit (Empleos en IT): argentina Desarrolladores - iOS, Ruby, Erlang: Estamos buscando un líder de desarrollo que quiera hacer una gran… http://t.co/zhTLpGI1
» erlang (Andreas Åkre Solberg): jQuery Scroll Path Plugin http://t.co/DHZ0W36c via @JoelBesada
» martyns (martynas): fighting with load bursts. probably its time to deploy https://t.co/xJxKcRmQ. #erlang
» Louellaoqk (Louella Delaroca): Informationsmanagement in Hochschulen (German Edition): Die Informations- und Kommunikationstechnik (IuK) erlang… http://t.co/95UJGZS3
Statistics
Number of aggregated posts: 10456
Number of comments: 1445
Most recent article: February 06, 2012
Latest comments
» simple smile on Scale means Skills: Very informative article. Pretty sure people would love to go to that place for shopping. Specially to those who are…
» simplesmile on 27 January 2012: Erlang Solutions embarks on an Erlang Embedded KTP: Your article will make the world better. Thanks again and good luck to you in your life. See you next time.simplesmile
» tandblekning easewhite on 08 February 2012: Erlang Express 3-day Course in San Francisco on 8 February: ncomprehensible to me now, but in general, the usefulness and significance is overwhelmingtandblekning easewhite