Skip to content

Commit

Permalink
Fix empty values in environment file
Browse files Browse the repository at this point in the history
Empty strings cause corrupt environment files, since two tabs are
written in a row which are of course lexed as a single piece of
whitespace.

Alter the format so that the @ symbol on its own means an empty string
(I'd have used something closer to $\epsilon$, but it'd never be
portable...). The single string @ is represented by \@.

Signed-off-by: David Allsopp <david.allsopp@metastack.com>
  • Loading branch information
dra27 committed Jul 1, 2016
1 parent f943c9b commit 246b518
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 5 deletions.
7 changes: 6 additions & 1 deletion src/format/opamFile.ml
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,12 @@ module LinesBase = struct
aux acc (i-1) in
aux ([],0) (len - 1)

let escape_spaces str =
let escape_spaces = function
| "" ->
"@"
| "@" ->
"\\@"
| str ->
let len = String.length str in
match find_escapes str len with
| [], _ -> str
Expand Down
12 changes: 8 additions & 4 deletions src/format/opamLineLexer.mll
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,19 @@ let word = Buffer.create 57

}

let normalchar = [^' ' '\t' '\n' '\\']
let normalchar = [^' ' '\t' '\r' '\n' '\\']

rule main = parse
| '\n' { Lexing.new_line lexbuf; NEWLINE }
| '\n' | "\r\n"
{ Lexing.new_line lexbuf; NEWLINE }
| [' ' '\t']+ { main lexbuf }
| ('@' normalchar*) as w '\\'
{ Buffer.reset word ; Buffer.add_string word w; escaped lexbuf }
| ('@' normalchar*) as w
{ if w = "@" then WORD "" else WORD w }
| (normalchar* as w) '\\'
{ Buffer.reset word ; Buffer.add_string word w; escaped lexbuf }
| (normalchar* as w)
| (normalchar+ as w)
{ WORD w }
| eof { EOF }

Expand All @@ -42,7 +47,6 @@ and escaped = parse
let main lexbuf =
let rec aux lines words =
match main lexbuf with
| WORD "" -> aux lines words
| WORD s -> aux lines (s::words)
| NEWLINE ->
let lines = if words = [] then lines else List.rev words::lines in
Expand Down

0 comments on commit 246b518

Please # to comment.