2016-10-02 22:30:28 -07:00
#[ cfg(test) ]
2016-10-28 19:38:03 -07:00
mod unit ;
#[ cfg(test) ]
mod integration ;
2016-10-02 22:30:28 -07:00
2016-10-23 16:43:52 -07:00
mod app ;
pub use app ::app ;
2016-10-22 23:18:26 -07:00
#[ macro_use ]
extern crate lazy_static ;
2016-10-02 22:30:28 -07:00
extern crate regex ;
2016-10-07 17:56:52 -07:00
extern crate tempdir ;
2016-10-30 16:15:18 -07:00
extern crate itertools ;
2016-10-02 22:30:28 -07:00
use std ::io ::prelude ::* ;
2016-10-05 13:58:18 -07:00
use std ::{ fs , fmt , process , io } ;
2016-10-02 22:30:28 -07:00
use std ::fmt ::Display ;
use regex ::Regex ;
2016-10-30 14:37:03 -07:00
use std ::collections ::{ BTreeMap as Map , BTreeSet as Set } ;
2016-10-02 22:30:28 -07:00
2016-10-07 17:56:52 -07:00
use std ::os ::unix ::fs ::PermissionsExt ;
2016-10-02 22:30:28 -07:00
macro_rules ! warn {
( $( $arg :tt ) * ) = > { {
extern crate std ;
use std ::io ::prelude ::* ;
let _ = writeln! ( & mut std ::io ::stderr ( ) , $( $arg ) * ) ;
} } ;
}
2016-10-28 00:06:36 -07:00
2016-10-02 22:30:28 -07:00
macro_rules ! die {
( $( $arg :tt ) * ) = > { {
extern crate std ;
warn! ( $( $arg ) * ) ;
std ::process ::exit ( - 1 )
} } ;
}
2016-10-23 16:43:52 -07:00
trait Slurp {
2016-10-02 22:30:28 -07:00
fn slurp ( & mut self ) -> Result < String , std ::io ::Error > ;
}
impl Slurp for fs ::File {
fn slurp ( & mut self ) -> Result < String , std ::io ::Error > {
let mut destination = String ::new ( ) ;
try ! ( self . read_to_string ( & mut destination ) ) ;
Ok ( destination )
}
}
fn re ( pattern : & str ) -> Regex {
Regex ::new ( pattern ) . unwrap ( )
}
2016-10-23 16:43:52 -07:00
#[ derive(PartialEq, Debug) ]
struct Recipe < ' a > {
2016-10-31 19:11:27 -07:00
line_number : usize ,
name : & ' a str ,
lines : Vec < Vec < Fragment < ' a > > > ,
dependencies : Vec < & ' a str > ,
dependency_tokens : Vec < Token < ' a > > ,
parameters : Vec < & ' a str > ,
parameter_tokens : Vec < Token < ' a > > ,
shebang : bool ,
2016-10-02 22:30:28 -07:00
}
2016-10-23 23:38:49 -07:00
#[ derive(PartialEq, Debug) ]
2016-10-27 09:44:07 -07:00
enum Fragment < ' a > {
2016-10-27 00:13:10 -07:00
Text { text : Token < ' a > } ,
2016-10-29 22:56:47 -07:00
Expression { expression : Expression < ' a > } ,
2016-10-16 18:59:49 -07:00
}
2016-10-27 00:13:10 -07:00
#[ derive(PartialEq, Debug) ]
2016-10-25 19:11:58 -07:00
enum Expression < ' a > {
2016-10-26 20:54:44 -07:00
Variable { name : & ' a str , token : Token < ' a > } ,
2016-10-28 00:06:36 -07:00
String { raw : & ' a str , cooked : String } ,
2016-10-29 23:39:12 -07:00
Backtick { raw : & ' a str , token : Token < ' a > } ,
2016-10-25 19:11:58 -07:00
Concatination { lhs : Box < Expression < ' a > > , rhs : Box < Expression < ' a > > } ,
}
2016-10-27 00:13:10 -07:00
impl < ' a > Expression < ' a > {
fn variables ( & ' a self ) -> Variables < ' a > {
Variables {
stack : vec ! [ self ] ,
}
}
}
struct Variables < ' a > {
stack : Vec < & ' a Expression < ' a > > ,
}
impl < ' a > Iterator for Variables < ' a > {
type Item = & ' a Token < ' a > ;
fn next ( & mut self ) -> Option < & ' a Token < ' a > > {
match self . stack . pop ( ) {
2016-10-29 01:58:30 -07:00
None | Some ( & Expression ::String { .. } ) | Some ( & Expression ::Backtick { .. } ) = > None ,
2016-10-27 00:13:10 -07:00
Some ( & Expression ::Variable { ref token , .. } ) = > Some ( token ) ,
Some ( & Expression ::Concatination { ref lhs , ref rhs } ) = > {
self . stack . push ( lhs ) ;
self . stack . push ( rhs ) ;
self . next ( )
}
}
}
}
2016-10-25 19:11:58 -07:00
impl < ' a > Display for Expression < ' a > {
fn fmt ( & self , f : & mut fmt ::Formatter ) -> Result < ( ) , fmt ::Error > {
match * self {
2016-10-29 23:39:12 -07:00
Expression ::Backtick { raw , .. } = > try ! ( write! ( f , " `{}` " , raw ) ) ,
2016-10-25 19:11:58 -07:00
Expression ::Concatination { ref lhs , ref rhs } = > try ! ( write! ( f , " {} + {} " , lhs , rhs ) ) ,
2016-10-29 01:58:30 -07:00
Expression ::String { raw , .. } = > try ! ( write! ( f , " \" {} \" " , raw ) ) ,
Expression ::Variable { name , .. } = > try ! ( write! ( f , " {} " , name ) ) ,
2016-10-25 19:11:58 -07:00
}
Ok ( ( ) )
}
}
2016-10-05 13:58:18 -07:00
#[ cfg(unix) ]
2016-10-23 20:39:50 -07:00
fn error_from_signal ( recipe : & str , exit_status : process ::ExitStatus ) -> RunError {
2016-10-05 13:58:18 -07:00
use std ::os ::unix ::process ::ExitStatusExt ;
match exit_status . signal ( ) {
Some ( signal ) = > RunError ::Signal { recipe : recipe , signal : signal } ,
None = > RunError ::UnknownFailure { recipe : recipe } ,
}
}
#[ cfg(windows) ]
2016-10-23 20:39:50 -07:00
fn error_from_signal ( recipe : & str , exit_status : process ::ExitStatus ) -> RunError {
2016-10-05 13:58:18 -07:00
RunError ::UnknownFailure { recipe : recipe }
}
2016-10-30 00:20:29 -07:00
#[ cfg(unix) ]
fn backtick_error_from_signal ( exit_status : process ::ExitStatus ) -> RunError < 'static > {
use std ::os ::unix ::process ::ExitStatusExt ;
match exit_status . signal ( ) {
Some ( signal ) = > RunError ::BacktickSignal { signal : signal } ,
None = > RunError ::BacktickUnknownFailure ,
}
}
#[ cfg(windows) ]
fn backtick_error_from_signal ( exit_status : process ::ExitStatus ) -> RunError < 'static > {
RunError ::BacktickUnknownFailure
}
2016-10-30 16:15:18 -07:00
fn export_env < ' a > (
command : & mut process ::Command ,
scope : & Map < & ' a str , String > ,
exports : & Set < & ' a str > ,
) -> Result < ( ) , RunError < ' a > > {
for name in exports {
if let Some ( value ) = scope . get ( name ) {
command . env ( name , value ) ;
} else {
return Err ( RunError ::InternalError {
message : format ! ( " scope does not contain exported variable `{}` " , name ) ,
} ) ;
}
}
Ok ( ( ) )
}
2016-10-29 23:39:12 -07:00
2016-10-30 16:15:18 -07:00
fn run_backtick < ' a > (
raw : & str ,
token : & Token < ' a > ,
scope : & Map < & ' a str , String > ,
exports : & Set < & ' a str > ,
2016-11-05 01:01:43 -07:00
quiet : bool ,
2016-10-30 16:15:18 -07:00
) -> Result < String , RunError < ' a > > {
let mut cmd = process ::Command ::new ( " sh " ) ;
try ! ( export_env ( & mut cmd , scope , exports ) ) ;
cmd . arg ( " -cu " )
2016-11-05 01:01:43 -07:00
. arg ( raw ) ;
cmd . stderr ( if quiet {
process ::Stdio ::null ( )
} else {
process ::Stdio ::inherit ( )
} ) ;
2016-10-30 16:15:18 -07:00
match cmd . output ( ) {
2016-10-30 00:20:29 -07:00
Ok ( output ) = > {
if let Some ( code ) = output . status . code ( ) {
if code ! = 0 {
return Err ( RunError ::BacktickCode {
2016-10-30 01:27:05 -07:00
token : token . clone ( ) ,
2016-10-30 00:20:29 -07:00
code : code ,
} ) ;
}
} else {
return Err ( backtick_error_from_signal ( output . status ) ) ;
}
match std ::str ::from_utf8 ( & output . stdout ) {
2016-10-30 00:25:04 -07:00
Err ( error ) = > Err ( RunError ::BacktickUtf8Error { utf8_error : error } ) ,
2016-10-30 00:20:29 -07:00
Ok ( utf8 ) = > {
Ok ( if utf8 . ends_with ( '\n' ) {
& utf8 [ 0 .. utf8 . len ( ) - 1 ]
} else if utf8 . ends_with ( " \r \n " ) {
& utf8 [ 0 .. utf8 . len ( ) - 2 ]
} else {
utf8
} . to_string ( ) )
}
2016-10-29 23:39:12 -07:00
}
2016-10-30 00:20:29 -07:00
}
Err ( error ) = > Err ( RunError ::BacktickIoError { io_error : error } ) ,
2016-10-29 23:39:12 -07:00
}
2016-10-29 22:56:47 -07:00
}
2016-10-03 23:55:55 -07:00
impl < ' a > Recipe < ' a > {
2016-10-29 21:51:39 -07:00
fn run (
2016-10-30 00:20:29 -07:00
& self ,
2016-10-29 21:51:39 -07:00
arguments : & [ & ' a str ] ,
2016-10-30 14:37:03 -07:00
scope : & Map < & ' a str , String > ,
2016-10-30 16:15:18 -07:00
exports : & Set < & ' a str > ,
2016-11-05 01:01:43 -07:00
options : & RunOptions ,
2016-10-29 21:51:39 -07:00
) -> Result < ( ) , RunError < ' a > > {
2016-10-29 22:56:47 -07:00
let argument_map = arguments . iter ( ) . enumerate ( )
2016-10-31 19:11:27 -07:00
. map ( | ( i , argument ) | ( self . parameters [ i ] , * argument ) ) . collect ( ) ;
2016-10-29 00:55:47 -07:00
2016-10-29 22:56:47 -07:00
let mut evaluator = Evaluator {
2016-10-30 14:37:03 -07:00
evaluated : Map ::new ( ) ,
2016-10-29 22:56:47 -07:00
scope : scope ,
2016-10-30 16:15:18 -07:00
exports : exports ,
2016-10-30 14:37:03 -07:00
assignments : & Map ::new ( ) ,
overrides : & Map ::new ( ) ,
2016-11-05 01:01:43 -07:00
quiet : options . quiet ,
2016-10-29 22:56:47 -07:00
} ;
2016-10-29 00:14:41 -07:00
2016-10-07 17:56:52 -07:00
if self . shebang {
2016-10-29 22:56:47 -07:00
let mut evaluated_lines = vec! [ ] ;
for line in & self . lines {
2016-10-29 23:39:12 -07:00
evaluated_lines . push ( try ! ( evaluator . evaluate_line ( & line , & argument_map ) ) ) ;
2016-10-29 22:56:47 -07:00
}
2016-11-05 01:01:43 -07:00
if options . dry_run {
2016-10-30 13:14:39 -07:00
for line in evaluated_lines {
warn! ( " {} " , line ) ;
}
return Ok ( ( ) ) ;
}
2016-10-07 17:56:52 -07:00
let tmp = try ! (
2016-10-30 18:12:59 -07:00
tempdir ::TempDir ::new ( " just " )
2016-10-07 17:56:52 -07:00
. map_err ( | error | RunError ::TmpdirIoError { recipe : self . name , io_error : error } )
) ;
let mut path = tmp . path ( ) . to_path_buf ( ) ;
path . push ( self . name ) ;
{
let mut f = try ! (
fs ::File ::create ( & path )
. map_err ( | error | RunError ::TmpdirIoError { recipe : self . name , io_error : error } )
) ;
let mut text = String ::new ( ) ;
// add the shebang
2016-10-29 00:14:41 -07:00
text + = & evaluated_lines [ 0 ] ;
2016-10-07 17:56:52 -07:00
text + = " \n " ;
// add blank lines so that lines in the generated script
// have the same line number as the corresponding lines
// in the justfile
for _ in 1 .. ( self . line_number + 2 ) {
text + = " \n "
}
2016-10-29 00:14:41 -07:00
for line in & evaluated_lines [ 1 .. ] {
2016-10-28 00:06:36 -07:00
text + = line ;
2016-10-07 17:56:52 -07:00
text + = " \n " ;
}
try ! (
f . write_all ( text . as_bytes ( ) )
. map_err ( | error | RunError ::TmpdirIoError { recipe : self . name , io_error : error } )
) ;
2016-10-05 13:58:18 -07:00
}
2016-10-07 17:56:52 -07:00
// get current permissions
let mut perms = try ! (
fs ::metadata ( & path )
. map_err ( | error | RunError ::TmpdirIoError { recipe : self . name , io_error : error } )
) . permissions ( ) ;
// make the script executable
let current_mode = perms . mode ( ) ;
perms . set_mode ( current_mode | 0o100 ) ;
try ! ( fs ::set_permissions ( & path , perms ) . map_err ( | error | RunError ::TmpdirIoError { recipe : self . name , io_error : error } ) ) ;
// run it!
2016-10-30 16:15:18 -07:00
let mut command = process ::Command ::new ( path ) ;
try ! ( export_env ( & mut command , scope , exports ) ) ;
try ! ( match command . status ( ) {
2016-10-05 13:58:18 -07:00
Ok ( exit_status ) = > if let Some ( code ) = exit_status . code ( ) {
if code = = 0 {
Ok ( ( ) )
} else {
Err ( RunError ::Code { recipe : self . name , code : code } )
}
} else {
Err ( error_from_signal ( self . name , exit_status ) )
} ,
2016-10-07 17:56:52 -07:00
Err ( io_error ) = > Err ( RunError ::TmpdirIoError { recipe : self . name , io_error : io_error } )
2016-10-05 13:58:18 -07:00
} ) ;
2016-10-07 17:56:52 -07:00
} else {
2016-10-29 22:56:47 -07:00
for line in & self . lines {
2016-10-29 23:39:12 -07:00
let evaluated = & try ! ( evaluator . evaluate_line ( & line , & argument_map ) ) ;
2016-10-29 22:56:47 -07:00
let mut command = evaluated . as_str ( ) ;
2016-11-05 01:01:43 -07:00
let quiet_command = command . starts_with ( '@' ) ;
if quiet_command {
2016-10-30 13:14:39 -07:00
command = & command [ 1 .. ] ;
}
2016-11-05 01:01:43 -07:00
if options . dry_run | | ! ( quiet_command | | options . quiet ) {
2016-10-07 17:56:52 -07:00
warn! ( " {} " , command ) ;
2016-10-30 13:14:39 -07:00
}
2016-11-05 01:01:43 -07:00
if options . dry_run {
2016-10-30 13:14:39 -07:00
continue ;
2016-10-07 17:56:52 -07:00
}
2016-10-30 16:15:18 -07:00
let mut cmd = process ::Command ::new ( " sh " ) ;
cmd . arg ( " -cu " ) . arg ( command ) ;
2016-11-05 01:01:43 -07:00
if options . quiet {
cmd . stderr ( process ::Stdio ::null ( ) ) ;
cmd . stdout ( process ::Stdio ::null ( ) ) ;
}
2016-10-30 16:15:18 -07:00
try ! ( export_env ( & mut cmd , scope , exports ) ) ;
try ! ( match cmd . status ( ) {
2016-10-07 17:56:52 -07:00
Ok ( exit_status ) = > if let Some ( code ) = exit_status . code ( ) {
if code = = 0 {
Ok ( ( ) )
} else {
Err ( RunError ::Code { recipe : self . name , code : code } )
}
} else {
Err ( error_from_signal ( self . name , exit_status ) )
} ,
Err ( io_error ) = > Err ( RunError ::IoError { recipe : self . name , io_error : io_error } )
} ) ;
}
2016-10-03 23:55:55 -07:00
}
Ok ( ( ) )
}
}
2016-10-23 16:43:52 -07:00
impl < ' a > Display for Recipe < ' a > {
fn fmt ( & self , f : & mut fmt ::Formatter ) -> Result < ( ) , fmt ::Error > {
try ! ( write! ( f , " {} " , self . name ) ) ;
2016-10-31 19:11:27 -07:00
for parameter in & self . parameters {
try ! ( write! ( f , " {} " , parameter ) ) ;
2016-10-23 16:43:52 -07:00
}
try ! ( write! ( f , " : " ) ) ;
for dependency in & self . dependencies {
try ! ( write! ( f , " {} " , dependency ) )
}
2016-10-23 23:38:49 -07:00
2016-10-27 09:44:07 -07:00
for ( i , pieces ) in self . lines . iter ( ) . enumerate ( ) {
2016-10-23 16:43:52 -07:00
if i = = 0 {
try ! ( writeln! ( f , " " ) ) ;
}
2016-10-27 00:13:10 -07:00
for ( j , piece ) in pieces . iter ( ) . enumerate ( ) {
2016-10-23 23:38:49 -07:00
if j = = 0 {
try ! ( write! ( f , " " ) ) ;
}
2016-10-29 21:51:39 -07:00
match * piece {
Fragment ::Text { ref text } = > try ! ( write! ( f , " {} " , text . lexeme ) ) ,
Fragment ::Expression { ref expression , .. } = > try ! ( write! ( f , " {}{}{} " , " {{ " , expression , " }} " ) ) ,
2016-10-23 23:38:49 -07:00
}
}
2016-10-27 09:44:07 -07:00
if i + 1 < self . lines . len ( ) {
2016-10-23 23:38:49 -07:00
try ! ( write! ( f , " \n " ) ) ;
2016-10-23 16:43:52 -07:00
}
}
Ok ( ( ) )
}
}
2016-10-29 20:39:21 -07:00
fn resolve_recipes < ' a > (
2016-10-30 14:37:03 -07:00
recipes : & Map < & ' a str , Recipe < ' a > > ,
assignments : & Map < & ' a str , Expression < ' a > > ,
2016-10-29 20:39:21 -07:00
text : & ' a str ,
) -> Result < ( ) , Error < ' a > > {
2016-10-25 19:11:58 -07:00
let mut resolver = Resolver {
2016-10-30 14:37:03 -07:00
seen : Set ::new ( ) ,
2016-10-29 20:39:21 -07:00
stack : vec ! [ ] ,
2016-10-30 14:37:03 -07:00
resolved : Set ::new ( ) ,
2016-10-29 20:39:21 -07:00
recipes : recipes ,
2016-10-25 19:11:58 -07:00
} ;
for recipe in recipes . values ( ) {
try ! ( resolver . resolve ( & recipe ) ) ;
}
2016-10-29 20:39:21 -07:00
for recipe in recipes . values ( ) {
for line in & recipe . lines {
for fragment in line {
if let Fragment ::Expression { ref expression , .. } = * fragment {
for variable in expression . variables ( ) {
let name = variable . lexeme ;
2016-10-31 19:11:27 -07:00
if ! ( assignments . contains_key ( name ) | | recipe . parameters . contains ( & name ) ) {
2016-10-29 20:39:21 -07:00
// There's a borrow issue here that seems too difficult to solve.
// The error derived from the variable token has too short a lifetime,
// so we create a new error from its contents, which do live long
// enough.
//
// I suspect the solution here is to give recipes, pieces, and expressions
// two lifetime parameters instead of one, with one being the lifetime
// of the struct, and the second being the lifetime of the tokens
// that it contains
2016-10-29 22:56:47 -07:00
let error = variable . error ( ErrorKind ::UndefinedVariable { variable : name } ) ;
2016-10-29 20:39:21 -07:00
return Err ( Error {
text : text ,
index : error . index ,
line : error . line ,
column : error . column ,
width : error . width ,
2016-10-29 22:56:47 -07:00
kind : ErrorKind ::UndefinedVariable {
2016-10-29 20:39:21 -07:00
variable : & text [ error . index .. error . index + error . width . unwrap ( ) ] ,
}
} ) ;
}
}
}
}
}
}
2016-10-25 19:11:58 -07:00
Ok ( ( ) )
}
struct Resolver < ' a : ' b , ' b > {
stack : Vec < & ' a str > ,
2016-10-30 14:37:03 -07:00
seen : Set < & ' a str > ,
resolved : Set < & ' a str > ,
recipes : & ' b Map < & ' a str , Recipe < ' a > > ,
2016-10-25 19:11:58 -07:00
}
impl < ' a , ' b > Resolver < ' a , ' b > {
fn resolve ( & mut self , recipe : & Recipe < ' a > ) -> Result < ( ) , Error < ' a > > {
if self . resolved . contains ( recipe . name ) {
return Ok ( ( ) )
}
self . stack . push ( recipe . name ) ;
self . seen . insert ( recipe . name ) ;
for dependency_token in & recipe . dependency_tokens {
match self . recipes . get ( dependency_token . lexeme ) {
Some ( dependency ) = > if ! self . resolved . contains ( dependency . name ) {
if self . seen . contains ( dependency . name ) {
let first = self . stack [ 0 ] ;
self . stack . push ( first ) ;
return Err ( dependency_token . error ( ErrorKind ::CircularRecipeDependency {
recipe : recipe . name ,
circle : self . stack . iter ( )
. skip_while ( | name | * * name ! = dependency . name )
. cloned ( ) . collect ( )
} ) ) ;
}
return self . resolve ( dependency ) ;
} ,
None = > return Err ( dependency_token . error ( ErrorKind ::UnknownDependency {
recipe : recipe . name ,
unknown : dependency_token . lexeme
} ) ) ,
}
}
self . resolved . insert ( recipe . name ) ;
self . stack . pop ( ) ;
Ok ( ( ) )
}
}
2016-10-29 20:39:21 -07:00
fn resolve_assignments < ' a > (
2016-10-30 14:37:03 -07:00
assignments : & Map < & ' a str , Expression < ' a > > ,
assignment_tokens : & Map < & ' a str , Token < ' a > > ,
2016-10-29 20:39:21 -07:00
) -> Result < ( ) , Error < ' a > > {
let mut resolver = AssignmentResolver {
assignments : assignments ,
assignment_tokens : assignment_tokens ,
stack : vec ! [ ] ,
2016-10-30 14:37:03 -07:00
seen : Set ::new ( ) ,
evaluated : Set ::new ( ) ,
2016-10-29 20:39:21 -07:00
} ;
for name in assignments . keys ( ) {
try ! ( resolver . resolve_assignment ( name ) ) ;
}
Ok ( ( ) )
}
struct AssignmentResolver < ' a : ' b , ' b > {
2016-10-30 14:37:03 -07:00
assignments : & ' b Map < & ' a str , Expression < ' a > > ,
assignment_tokens : & ' b Map < & ' a str , Token < ' a > > ,
2016-10-29 20:39:21 -07:00
stack : Vec < & ' a str > ,
2016-10-30 14:37:03 -07:00
seen : Set < & ' a str > ,
evaluated : Set < & ' a str > ,
2016-10-29 20:39:21 -07:00
}
impl < ' a : ' b , ' b > AssignmentResolver < ' a , ' b > {
fn resolve_assignment ( & mut self , name : & ' a str ) -> Result < ( ) , Error < ' a > > {
if self . evaluated . contains ( name ) {
return Ok ( ( ) ) ;
}
self . seen . insert ( name ) ;
self . stack . push ( name ) ;
if let Some ( expression ) = self . assignments . get ( name ) {
try ! ( self . resolve_expression ( expression ) ) ;
self . evaluated . insert ( name ) ;
} else {
2016-10-29 22:56:47 -07:00
return Err ( internal_error ( format! ( " attempted to resolve unknown assignment ` {} ` " , name ) ) ) ;
2016-10-29 20:39:21 -07:00
}
Ok ( ( ) )
}
fn resolve_expression ( & mut self , expression : & Expression < ' a > ) -> Result < ( ) , Error < ' a > > {
match * expression {
Expression ::Variable { name , ref token } = > {
if self . evaluated . contains ( name ) {
return Ok ( ( ) ) ;
} else if self . seen . contains ( name ) {
2016-10-29 22:56:47 -07:00
let token = & self . assignment_tokens [ name ] ;
2016-10-29 20:39:21 -07:00
self . stack . push ( name ) ;
return Err ( token . error ( ErrorKind ::CircularVariableDependency {
variable : name ,
circle : self . stack . clone ( ) ,
} ) ) ;
} else if self . assignments . contains_key ( name ) {
try ! ( self . resolve_assignment ( name ) ) ;
} else {
2016-10-29 22:56:47 -07:00
return Err ( token . error ( ErrorKind ::UndefinedVariable { variable : name } ) ) ;
2016-10-29 20:39:21 -07:00
}
}
Expression ::Concatination { ref lhs , ref rhs } = > {
try ! ( self . resolve_expression ( lhs ) ) ;
try ! ( self . resolve_expression ( rhs ) ) ;
}
2016-10-29 23:39:12 -07:00
Expression ::String { .. } | Expression ::Backtick { .. } = > { }
2016-10-29 20:39:21 -07:00
}
Ok ( ( ) )
}
}
2016-10-29 21:51:39 -07:00
fn evaluate_assignments < ' a > (
2016-10-30 14:37:03 -07:00
assignments : & Map < & ' a str , Expression < ' a > > ,
overrides : & Map < & str , & str > ,
2016-11-05 01:01:43 -07:00
quiet : bool ,
2016-10-30 14:37:03 -07:00
) -> Result < Map < & ' a str , String > , RunError < ' a > > {
2016-10-29 21:51:39 -07:00
let mut evaluator = Evaluator {
2016-11-05 01:01:43 -07:00
assignments : assignments ,
2016-10-30 14:37:03 -07:00
evaluated : Map ::new ( ) ,
2016-10-30 16:15:18 -07:00
exports : & Set ::new ( ) ,
2016-10-30 03:08:28 -07:00
overrides : overrides ,
2016-11-05 01:01:43 -07:00
quiet : quiet ,
scope : & Map ::new ( ) ,
2016-10-29 21:51:39 -07:00
} ;
2016-10-29 00:14:41 -07:00
2016-10-29 21:51:39 -07:00
for name in assignments . keys ( ) {
try ! ( evaluator . evaluate_assignment ( name ) ) ;
2016-10-29 00:14:41 -07:00
}
2016-10-29 21:51:39 -07:00
Ok ( evaluator . evaluated )
2016-10-29 00:14:41 -07:00
}
2016-10-25 19:11:58 -07:00
struct Evaluator < ' a : ' b , ' b > {
2016-11-05 01:01:43 -07:00
assignments : & ' b Map < & ' a str , Expression < ' a > > ,
2016-10-30 14:37:03 -07:00
evaluated : Map < & ' a str , String > ,
2016-10-30 16:15:18 -07:00
exports : & ' b Set < & ' a str > ,
2016-10-30 14:37:03 -07:00
overrides : & ' b Map < & ' b str , & ' b str > ,
2016-11-05 01:01:43 -07:00
quiet : bool ,
scope : & ' b Map < & ' a str , String > ,
2016-10-25 19:11:58 -07:00
}
impl < ' a , ' b > Evaluator < ' a , ' b > {
2016-10-29 22:56:47 -07:00
fn evaluate_line (
& mut self ,
2016-10-30 00:20:29 -07:00
line : & [ Fragment < ' a > ] ,
2016-10-30 14:37:03 -07:00
arguments : & Map < & str , & str >
2016-10-29 22:56:47 -07:00
) -> Result < String , RunError < ' a > > {
let mut evaluated = String ::new ( ) ;
for fragment in line {
match * fragment {
Fragment ::Text { ref text } = > evaluated + = text . lexeme ,
Fragment ::Expression { ref expression } = > {
evaluated + = & try ! ( self . evaluate_expression ( expression , arguments ) ) ;
}
}
}
Ok ( evaluated )
}
2016-10-29 21:51:39 -07:00
fn evaluate_assignment ( & mut self , name : & ' a str ) -> Result < ( ) , RunError < ' a > > {
2016-10-25 19:11:58 -07:00
if self . evaluated . contains_key ( name ) {
return Ok ( ( ) ) ;
}
if let Some ( expression ) = self . assignments . get ( name ) {
2016-10-30 03:08:28 -07:00
if let Some ( value ) = self . overrides . get ( name ) {
self . evaluated . insert ( name , value . to_string ( ) ) ;
} else {
2016-10-30 14:37:03 -07:00
let value = try ! ( self . evaluate_expression ( expression , & Map ::new ( ) ) ) ;
2016-10-30 03:08:28 -07:00
self . evaluated . insert ( name , value ) ;
}
2016-10-25 19:11:58 -07:00
} else {
2016-10-29 22:56:47 -07:00
return Err ( RunError ::InternalError {
message : format ! ( " attempted to evaluated unknown assignment {} " , name )
} ) ;
2016-10-25 19:11:58 -07:00
}
2016-10-29 00:14:41 -07:00
Ok ( ( ) )
}
fn evaluate_expression (
& mut self ,
2016-10-30 00:20:29 -07:00
expression : & Expression < ' a > ,
2016-10-30 14:37:03 -07:00
arguments : & Map < & str , & str >
2016-10-29 21:51:39 -07:00
) -> Result < String , RunError < ' a > > {
2016-10-25 19:11:58 -07:00
Ok ( match * expression {
2016-10-29 21:51:39 -07:00
Expression ::Variable { name , .. } = > {
2016-10-25 19:11:58 -07:00
if self . evaluated . contains_key ( name ) {
2016-10-29 22:56:47 -07:00
self . evaluated [ name ] . clone ( )
2016-10-29 00:14:41 -07:00
} else if self . scope . contains_key ( name ) {
2016-10-29 22:56:47 -07:00
self . scope [ name ] . clone ( )
2016-10-29 00:14:41 -07:00
} else if self . assignments . contains_key ( name ) {
2016-10-25 19:11:58 -07:00
try ! ( self . evaluate_assignment ( name ) ) ;
2016-10-29 22:56:47 -07:00
self . evaluated [ name ] . clone ( )
2016-10-29 00:14:41 -07:00
} else if arguments . contains_key ( name ) {
2016-10-29 22:56:47 -07:00
arguments [ name ] . to_string ( )
2016-10-29 00:14:41 -07:00
} else {
2016-10-29 22:56:47 -07:00
return Err ( RunError ::InternalError {
message : format ! ( " attempted to evaluate undefined variable `{}` " , name )
} ) ;
2016-10-03 23:55:55 -07:00
}
2016-10-25 19:11:58 -07:00
}
2016-10-29 22:56:47 -07:00
Expression ::String { ref cooked , .. } = > cooked . clone ( ) ,
2016-10-30 16:15:18 -07:00
Expression ::Backtick { raw , ref token } = > {
2016-11-05 01:01:43 -07:00
try ! ( run_backtick ( raw , token , & self . scope , & self . exports , self . quiet ) )
2016-10-30 16:15:18 -07:00
}
2016-10-25 19:11:58 -07:00
Expression ::Concatination { ref lhs , ref rhs } = > {
2016-10-29 21:51:39 -07:00
try ! ( self . evaluate_expression ( lhs , arguments ) )
+
& try ! ( self . evaluate_expression ( rhs , arguments ) )
2016-10-25 19:11:58 -07:00
}
} )
2016-10-03 23:55:55 -07:00
}
}
2016-10-22 23:18:26 -07:00
#[ derive(Debug, PartialEq) ]
2016-10-23 16:43:52 -07:00
struct Error < ' a > {
2016-10-22 23:18:26 -07:00
text : & ' a str ,
index : usize ,
line : usize ,
column : usize ,
2016-10-23 16:43:52 -07:00
width : Option < usize > ,
2016-10-22 23:18:26 -07:00
kind : ErrorKind < ' a > ,
2016-10-02 22:30:28 -07:00
}
#[ derive(Debug, PartialEq) ]
enum ErrorKind < ' a > {
2016-10-25 19:11:58 -07:00
CircularRecipeDependency { recipe : & ' a str , circle : Vec < & ' a str > } ,
CircularVariableDependency { variable : & ' a str , circle : Vec < & ' a str > } ,
2016-10-31 19:11:27 -07:00
DependencyHasParameters { recipe : & ' a str , dependency : & ' a str } ,
2016-10-28 00:06:36 -07:00
DuplicateDependency { recipe : & ' a str , dependency : & ' a str } ,
2016-10-31 19:11:27 -07:00
DuplicateParameter { recipe : & ' a str , parameter : & ' a str } ,
2016-10-23 16:43:52 -07:00
DuplicateRecipe { recipe : & ' a str , first : usize } ,
2016-10-25 19:11:58 -07:00
DuplicateVariable { variable : & ' a str } ,
2016-10-23 16:43:52 -07:00
ExtraLeadingWhitespace ,
2016-10-02 22:30:28 -07:00
InconsistentLeadingWhitespace { expected : & ' a str , found : & ' a str } ,
2016-10-28 00:06:36 -07:00
InternalError { message : String } ,
InvalidEscapeSequence { character : char } ,
MixedLeadingWhitespace { whitespace : & ' a str } ,
2016-10-06 17:43:30 -07:00
OuterShebang ,
2016-10-31 19:11:27 -07:00
ParameterShadowsVariable { parameter : & ' a str } ,
UndefinedVariable { variable : & ' a str } ,
2016-10-28 00:06:36 -07:00
UnexpectedToken { expected : Vec < TokenKind > , found : TokenKind } ,
2016-10-23 16:43:52 -07:00
UnknownDependency { recipe : & ' a str , unknown : & ' a str } ,
2016-10-16 18:59:49 -07:00
UnknownStartOfToken ,
2016-10-28 00:06:36 -07:00
UnterminatedString ,
2016-10-02 22:30:28 -07:00
}
2016-10-29 22:56:47 -07:00
fn internal_error ( message : String ) -> Error < 'static > {
Error {
text : " " ,
index : 0 ,
line : 0 ,
column : 0 ,
width : None ,
kind : ErrorKind ::InternalError { message : message }
}
}
2016-10-02 22:30:28 -07:00
fn show_whitespace ( text : & str ) -> String {
text . chars ( ) . map ( | c | match c { '\t' = > 't' , ' ' = > 's' , _ = > c } ) . collect ( )
}
2016-10-23 16:43:52 -07:00
fn mixed_whitespace ( text : & str ) -> bool {
2016-10-02 22:30:28 -07:00
! ( text . chars ( ) . all ( | c | c = = ' ' ) | | text . chars ( ) . all ( | c | c = = '\t' ) )
}
2016-10-30 03:08:28 -07:00
struct And < ' a , T : ' a + Display > ( & ' a [ T ] ) ;
struct Or < ' a , T : ' a + Display > ( & ' a [ T ] ) ;
impl < ' a , T : Display > Display for And < ' a , T > {
fn fmt ( & self , f : & mut fmt ::Formatter ) -> Result < ( ) , fmt ::Error > {
conjoin ( f , self . 0 , " and " )
}
}
2016-10-23 16:43:52 -07:00
impl < ' a , T : Display > Display for Or < ' a , T > {
fn fmt ( & self , f : & mut fmt ::Formatter ) -> Result < ( ) , fmt ::Error > {
2016-10-30 03:08:28 -07:00
conjoin ( f , self . 0 , " or " )
}
}
fn conjoin < T : Display > (
f : & mut fmt ::Formatter ,
values : & [ T ] ,
conjunction : & str ,
) -> Result < ( ) , fmt ::Error > {
match values . len ( ) {
2016-10-23 16:43:52 -07:00
0 = > { } ,
2016-10-30 03:08:28 -07:00
1 = > try ! ( write! ( f , " {} " , values [ 0 ] ) ) ,
2 = > try ! ( write! ( f , " {} {} {} " , values [ 0 ] , conjunction , values [ 1 ] ) ) ,
_ = > for ( i , item ) in values . iter ( ) . enumerate ( ) {
2016-10-23 16:43:52 -07:00
try ! ( write! ( f , " {} " , item ) ) ;
2016-10-30 03:08:28 -07:00
if i = = values . len ( ) - 1 {
} else if i = = values . len ( ) - 2 {
try ! ( write! ( f , " , {} " , conjunction ) ) ;
2016-10-23 16:43:52 -07:00
} else {
try ! ( write! ( f , " , " ) )
}
2016-10-02 22:30:28 -07:00
} ,
}
2016-10-23 16:43:52 -07:00
Ok ( ( ) )
2016-10-02 22:30:28 -07:00
}
impl < ' a > Display for Error < ' a > {
fn fmt ( & self , f : & mut fmt ::Formatter ) -> Result < ( ) , fmt ::Error > {
2016-10-28 20:34:25 -07:00
try ! ( write! ( f , " error: " ) ) ;
2016-10-28 19:56:33 -07:00
2016-10-02 22:30:28 -07:00
match self . kind {
2016-10-25 19:11:58 -07:00
ErrorKind ::CircularRecipeDependency { recipe , ref circle } = > {
if circle . len ( ) = = 2 {
2016-10-28 20:40:16 -07:00
try ! ( write! ( f , " recipe `{}` depends on itself " , recipe ) ) ;
2016-10-25 19:11:58 -07:00
} else {
2016-10-28 20:40:16 -07:00
try ! ( write! ( f , " recipe `{}` has circular dependency `{}` " , recipe , circle . join ( " -> " ) ) ) ;
2016-10-25 19:11:58 -07:00
}
return Ok ( ( ) ) ;
}
ErrorKind ::CircularVariableDependency { variable , ref circle } = > {
2016-10-28 20:40:16 -07:00
try ! ( write! ( f , " assignment to `{}` has circular dependency: `{}` " , variable , circle . join ( " -> " ) ) ) ;
2016-10-23 16:43:52 -07:00
return Ok ( ( ) ) ;
}
2016-10-28 00:06:36 -07:00
ErrorKind ::InvalidEscapeSequence { character } = > {
2016-10-28 20:40:16 -07:00
try ! ( writeln! ( f , " ` \\ {}` is not a valid escape sequence " , character . escape_default ( ) . collect ::< String > ( ) ) ) ;
2016-10-28 00:06:36 -07:00
}
2016-10-31 19:11:27 -07:00
ErrorKind ::DuplicateParameter { recipe , parameter } = > {
try ! ( writeln! ( f , " recipe `{}` has duplicate parameter `{}` " , recipe , parameter ) ) ;
2016-10-23 16:43:52 -07:00
}
2016-10-25 19:11:58 -07:00
ErrorKind ::DuplicateVariable { variable } = > {
2016-10-28 20:40:16 -07:00
try ! ( writeln! ( f , " variable `{}` is has multiple definitions " , variable ) ) ;
2016-10-25 19:11:58 -07:00
}
2016-10-23 16:43:52 -07:00
ErrorKind ::UnexpectedToken { ref expected , found } = > {
try ! ( writeln! ( f , " expected {} but found {} " , Or ( expected ) , found ) ) ;
}
ErrorKind ::DuplicateDependency { recipe , dependency } = > {
2016-10-28 20:40:16 -07:00
try ! ( writeln! ( f , " recipe `{}` has duplicate dependency `{}` " , recipe , dependency ) ) ;
2016-10-23 16:43:52 -07:00
}
ErrorKind ::DuplicateRecipe { recipe , first } = > {
2016-10-28 20:40:16 -07:00
try ! ( write! ( f , " recipe `{}` first defined on line {} is redefined on line {} " ,
2016-10-23 16:43:52 -07:00
recipe , first , self . line ) ) ;
return Ok ( ( ) ) ;
}
2016-10-31 19:11:27 -07:00
ErrorKind ::DependencyHasParameters { recipe , dependency } = > {
try ! ( writeln! ( f , " recipe `{}` depends on `{}` which requires arguments. dependencies may not require arguments " , recipe , dependency ) ) ;
2016-10-29 00:14:41 -07:00
}
2016-10-31 19:11:27 -07:00
ErrorKind ::ParameterShadowsVariable { parameter } = > {
try ! ( writeln! ( f , " parameter `{}` shadows variable of the same name " , parameter ) ) ;
2016-10-25 19:11:58 -07:00
}
2016-10-23 16:43:52 -07:00
ErrorKind ::MixedLeadingWhitespace { whitespace } = > {
try ! ( writeln! ( f ,
2016-10-28 20:40:16 -07:00
" found a mix of tabs and spaces in leading whitespace: `{}` \n leading whitespace may consist of tabs or spaces, but not both " ,
2016-10-23 16:43:52 -07:00
show_whitespace ( whitespace )
) ) ;
}
ErrorKind ::ExtraLeadingWhitespace = > {
try ! ( writeln! ( f , " recipe line has extra leading whitespace " ) ) ;
}
2016-10-02 22:30:28 -07:00
ErrorKind ::InconsistentLeadingWhitespace { expected , found } = > {
try ! ( writeln! ( f ,
2016-10-28 20:40:16 -07:00
" inconsistant leading whitespace: recipe started with `{}` but found line with `{}`: " ,
2016-10-02 22:30:28 -07:00
show_whitespace ( expected ) , show_whitespace ( found )
) ) ;
}
2016-10-06 17:43:30 -07:00
ErrorKind ::OuterShebang = > {
2016-10-28 20:40:16 -07:00
try ! ( writeln! ( f , " a shebang `#!` is reserved syntax outside of recipes " ) )
2016-10-06 17:43:30 -07:00
}
2016-10-23 16:43:52 -07:00
ErrorKind ::UnknownDependency { recipe , unknown } = > {
2016-10-28 20:40:16 -07:00
try ! ( writeln! ( f , " recipe `{}` has unknown dependency `{}` " , recipe , unknown ) ) ;
2016-10-23 16:43:52 -07:00
}
2016-10-29 22:56:47 -07:00
ErrorKind ::UndefinedVariable { variable } = > {
try ! ( writeln! ( f , " variable `{}` not defined " , variable ) ) ;
2016-10-25 19:11:58 -07:00
}
2016-10-16 18:59:49 -07:00
ErrorKind ::UnknownStartOfToken = > {
2016-10-25 19:11:58 -07:00
try ! ( writeln! ( f , " unknown start of token: " ) ) ;
2016-10-16 18:59:49 -07:00
}
2016-10-28 00:06:36 -07:00
ErrorKind ::UnterminatedString = > {
try ! ( writeln! ( f , " unterminated string " ) ) ;
}
2016-10-22 23:18:26 -07:00
ErrorKind ::InternalError { ref message } = > {
2016-10-30 18:12:59 -07:00
try ! ( writeln! ( f , " internal error, this may indicate a bug in just: {} \n consider filing an issue: https://github.com/casey/just/issues/new " , message ) ) ;
2016-10-22 23:18:26 -07:00
}
2016-10-02 22:30:28 -07:00
}
match self . text . lines ( ) . nth ( self . line ) {
2016-10-28 20:34:25 -07:00
Some ( line ) = > {
2016-10-31 21:53:31 -07:00
let displayed_line = self . line + 1 ;
let line_number_width = displayed_line . to_string ( ) . len ( ) ;
2016-10-28 20:34:25 -07:00
try ! ( write! ( f , " {0:1$} | \n " , " " , line_number_width ) ) ;
2016-10-31 21:53:31 -07:00
try ! ( write! ( f , " {} | {} \n " , displayed_line , line ) ) ;
2016-10-28 20:34:25 -07:00
try ! ( write! ( f , " {0:1$} | " , " " , line_number_width ) ) ;
2016-10-31 21:53:31 -07:00
try ! ( write! ( f , " {0:1$}{2:^<3$} " , " " , self . column , " " , self . width . unwrap_or ( 1 ) ) ) ;
2016-10-28 20:34:25 -07:00
} ,
2016-10-23 16:43:52 -07:00
None = > if self . index ! = self . text . len ( ) {
2016-10-28 20:34:25 -07:00
try ! ( write! ( f , " internal error: Error has invalid line number: {} " , self . line + 1 ) )
2016-10-23 16:43:52 -07:00
} ,
2016-10-07 17:56:52 -07:00
} ;
2016-10-02 22:30:28 -07:00
Ok ( ( ) )
}
}
2016-10-23 16:43:52 -07:00
struct Justfile < ' a > {
2016-10-30 14:37:03 -07:00
recipes : Map < & ' a str , Recipe < ' a > > ,
assignments : Map < & ' a str , Expression < ' a > > ,
2016-10-30 16:15:18 -07:00
exports : Set < & ' a str > ,
2016-10-02 22:30:28 -07:00
}
2016-11-05 00:31:38 -07:00
#[ derive(Default) ]
struct RunOptions < ' a > {
dry_run : bool ,
evaluate : bool ,
overrides : Map < & ' a str , & ' a str > ,
2016-11-05 01:01:43 -07:00
quiet : bool ,
2016-11-05 00:31:38 -07:00
}
2016-10-29 00:14:41 -07:00
impl < ' a , ' b > Justfile < ' a > where ' a : ' b {
2016-10-23 16:43:52 -07:00
fn first ( & self ) -> Option < & ' a str > {
2016-10-02 22:30:28 -07:00
let mut first : Option < & Recipe < ' a > > = None ;
2016-10-23 20:39:50 -07:00
for recipe in self . recipes . values ( ) {
2016-10-02 22:30:28 -07:00
if let Some ( first_recipe ) = first {
2016-10-06 17:43:30 -07:00
if recipe . line_number < first_recipe . line_number {
2016-10-02 22:30:28 -07:00
first = Some ( recipe )
}
} else {
first = Some ( recipe ) ;
}
}
first . map ( | recipe | recipe . name )
}
2016-10-23 16:43:52 -07:00
fn count ( & self ) -> usize {
2016-10-03 23:55:55 -07:00
self . recipes . len ( )
}
2016-10-23 16:43:52 -07:00
fn recipes ( & self ) -> Vec < & ' a str > {
2016-10-03 23:55:55 -07:00
self . recipes . keys ( ) . cloned ( ) . collect ( )
}
2016-10-30 03:08:28 -07:00
fn run (
& ' a self ,
2016-10-30 13:14:39 -07:00
arguments : & [ & ' a str ] ,
2016-11-05 01:01:43 -07:00
options : & RunOptions < ' a > ,
2016-10-30 03:08:28 -07:00
) -> Result < ( ) , RunError < ' a > > {
2016-11-05 00:31:38 -07:00
let unknown_overrides = options . overrides . keys ( ) . cloned ( )
2016-10-30 03:08:28 -07:00
. filter ( | name | ! self . assignments . contains_key ( name ) )
. collect ::< Vec < _ > > ( ) ;
if ! unknown_overrides . is_empty ( ) {
return Err ( RunError ::UnknownOverrides { overrides : unknown_overrides } ) ;
}
2016-11-05 01:01:43 -07:00
let scope = try ! ( evaluate_assignments ( & self . assignments , & options . overrides , options . quiet ) ) ;
2016-11-05 00:31:38 -07:00
if options . evaluate {
2016-10-30 13:14:39 -07:00
for ( name , value ) in scope {
println! ( " {} = \" {} \" " , name , value ) ;
}
return Ok ( ( ) ) ;
}
2016-10-30 14:37:03 -07:00
let mut ran = Set ::new ( ) ;
2016-10-29 21:51:39 -07:00
2016-10-29 00:14:41 -07:00
for ( i , argument ) in arguments . iter ( ) . enumerate ( ) {
if let Some ( recipe ) = self . recipes . get ( argument ) {
2016-10-31 19:11:27 -07:00
if ! recipe . parameters . is_empty ( ) {
2016-10-29 00:14:41 -07:00
if i ! = 0 {
2016-10-31 19:11:27 -07:00
return Err ( RunError ::NonLeadingRecipeWithParameters { recipe : recipe . name } ) ;
2016-10-29 00:14:41 -07:00
}
let rest = & arguments [ 1 .. ] ;
2016-10-31 19:11:27 -07:00
if recipe . parameters . len ( ) ! = rest . len ( ) {
2016-10-29 00:14:41 -07:00
return Err ( RunError ::ArgumentCountMismatch {
recipe : recipe . name ,
found : rest . len ( ) ,
2016-10-31 19:11:27 -07:00
expected : recipe . parameters . len ( ) ,
2016-10-29 00:14:41 -07:00
} ) ;
}
2016-11-05 01:01:43 -07:00
try ! ( self . run_recipe ( recipe , rest , & scope , & mut ran , options ) ) ;
2016-10-29 00:14:41 -07:00
return Ok ( ( ) ) ;
}
} else {
break ;
}
}
2016-10-03 23:55:55 -07:00
let mut missing = vec! [ ] ;
2016-10-29 00:14:41 -07:00
for recipe in arguments {
2016-10-03 23:55:55 -07:00
if ! self . recipes . contains_key ( recipe ) {
missing . push ( * recipe ) ;
2016-10-02 22:30:28 -07:00
}
}
2016-10-23 20:39:50 -07:00
if ! missing . is_empty ( ) {
2016-10-03 23:55:55 -07:00
return Err ( RunError ::UnknownRecipes { recipes : missing } ) ;
}
2016-10-29 22:56:47 -07:00
for recipe in arguments . iter ( ) . map ( | name | & self . recipes [ name ] ) {
2016-11-05 01:01:43 -07:00
try ! ( self . run_recipe ( recipe , & [ ] , & scope , & mut ran , options ) ) ;
2016-10-03 23:55:55 -07:00
}
Ok ( ( ) )
2016-10-02 22:30:28 -07:00
}
2016-10-05 16:03:11 -07:00
2016-10-29 22:56:47 -07:00
fn run_recipe < ' c > (
& ' c self ,
2016-10-30 00:20:29 -07:00
recipe : & Recipe < ' a > ,
2016-10-29 21:51:39 -07:00
arguments : & [ & ' a str ] ,
2016-10-30 14:37:03 -07:00
scope : & Map < & ' c str , String > ,
ran : & mut Set < & ' a str > ,
2016-11-05 01:01:43 -07:00
options : & RunOptions < ' a > ,
2016-10-29 21:51:39 -07:00
) -> Result < ( ) , RunError > {
for dependency_name in & recipe . dependencies {
if ! ran . contains ( dependency_name ) {
2016-11-05 01:01:43 -07:00
try ! ( self . run_recipe ( & self . recipes [ dependency_name ] , & [ ] , scope , ran , options ) ) ;
2016-10-29 21:51:39 -07:00
}
}
2016-11-05 01:01:43 -07:00
try ! ( recipe . run ( arguments , & scope , & self . exports , options ) ) ;
2016-10-29 21:51:39 -07:00
ran . insert ( recipe . name ) ;
Ok ( ( ) )
}
2016-10-23 16:43:52 -07:00
fn get ( & self , name : & str ) -> Option < & Recipe < ' a > > {
2016-10-05 16:03:11 -07:00
self . recipes . get ( name )
}
2016-10-03 23:55:55 -07:00
}
2016-10-02 22:30:28 -07:00
2016-10-25 19:11:58 -07:00
impl < ' a > Display for Justfile < ' a > {
fn fmt ( & self , f : & mut fmt ::Formatter ) -> Result < ( ) , fmt ::Error > {
let mut items = self . recipes . len ( ) + self . assignments . len ( ) ;
for ( name , expression ) in & self . assignments {
2016-10-30 16:15:18 -07:00
if self . exports . contains ( name ) {
try ! ( write! ( f , " export " ) ) ;
}
2016-10-29 21:51:39 -07:00
try ! ( write! ( f , " {} = {} " , name , expression ) ) ;
2016-10-25 19:11:58 -07:00
items - = 1 ;
if items ! = 0 {
2016-10-28 16:41:46 -07:00
try ! ( write! ( f , " \n \n " ) ) ;
2016-10-25 19:11:58 -07:00
}
}
for recipe in self . recipes . values ( ) {
2016-10-29 21:51:39 -07:00
try ! ( write! ( f , " {} " , recipe ) ) ;
2016-10-25 19:11:58 -07:00
items - = 1 ;
if items ! = 0 {
2016-10-28 16:41:46 -07:00
try ! ( write! ( f , " \n \n " ) ) ;
2016-10-25 19:11:58 -07:00
}
}
Ok ( ( ) )
}
}
2016-10-05 13:58:18 -07:00
#[ derive(Debug) ]
2016-10-23 16:43:52 -07:00
enum RunError < ' a > {
2016-10-29 00:14:41 -07:00
ArgumentCountMismatch { recipe : & ' a str , found : usize , expected : usize } ,
2016-10-03 23:55:55 -07:00
Code { recipe : & ' a str , code : i32 } ,
2016-10-29 01:58:30 -07:00
InternalError { message : String } ,
2016-10-05 13:58:18 -07:00
IoError { recipe : & ' a str , io_error : io ::Error } ,
2016-10-31 19:11:27 -07:00
NonLeadingRecipeWithParameters { recipe : & ' a str } ,
2016-10-29 01:58:30 -07:00
Signal { recipe : & ' a str , signal : i32 } ,
2016-10-07 17:56:52 -07:00
TmpdirIoError { recipe : & ' a str , io_error : io ::Error } ,
2016-10-29 01:58:30 -07:00
UnknownFailure { recipe : & ' a str } ,
UnknownRecipes { recipes : Vec < & ' a str > } ,
2016-10-30 03:08:28 -07:00
UnknownOverrides { overrides : Vec < & ' a str > } ,
2016-10-30 01:27:05 -07:00
BacktickCode { code : i32 , token : Token < ' a > } ,
2016-10-30 00:20:29 -07:00
BacktickIoError { io_error : io ::Error } ,
BacktickSignal { signal : i32 } ,
BacktickUtf8Error { utf8_error : std ::str ::Utf8Error } ,
BacktickUnknownFailure ,
2016-10-03 23:55:55 -07:00
}
impl < ' a > Display for RunError < ' a > {
fn fmt ( & self , f : & mut fmt ::Formatter ) -> Result < ( ) , fmt ::Error > {
2016-10-23 20:39:50 -07:00
match * self {
RunError ::UnknownRecipes { ref recipes } = > {
2016-10-03 23:55:55 -07:00
if recipes . len ( ) = = 1 {
2016-11-02 22:01:38 -07:00
try ! ( write! ( f , " Justfile does not contain recipe `{}` " , recipes [ 0 ] ) ) ;
2016-10-03 23:55:55 -07:00
} else {
try ! ( write! ( f , " Justfile does not contain recipes: {} " , recipes . join ( " " ) ) ) ;
} ;
} ,
2016-10-30 03:08:28 -07:00
RunError ::UnknownOverrides { ref overrides } = > {
try ! ( write! ( f , " {} set on the command line but not present in justfile " ,
And ( overrides ) ) )
} ,
2016-10-31 19:11:27 -07:00
RunError ::NonLeadingRecipeWithParameters { recipe } = > {
2016-10-29 00:14:41 -07:00
try ! ( write! ( f , " Recipe `{}` takes arguments and so must be the first and only recipe specified on the command line " , recipe ) ) ;
} ,
RunError ::ArgumentCountMismatch { recipe , found , expected } = > {
try ! ( write! ( f , " Recipe `{}` takes {} argument{}, but {}{} were found " ,
recipe , expected , if expected = = 1 { " " } else { " s " } ,
if found < expected { " only " } else { " " } , found ) ) ;
2016-10-27 00:31:50 -07:00
} ,
2016-10-23 20:39:50 -07:00
RunError ::Code { recipe , code } = > {
2016-10-28 15:59:50 -07:00
try ! ( write! ( f , " Recipe \" {} \" failed with exit code {} " , recipe , code ) ) ;
2016-10-03 23:55:55 -07:00
} ,
2016-10-23 20:39:50 -07:00
RunError ::Signal { recipe , signal } = > {
2016-10-05 13:58:18 -07:00
try ! ( write! ( f , " Recipe \" {} \" wast terminated by signal {} " , recipe , signal ) ) ;
}
2016-10-23 20:39:50 -07:00
RunError ::UnknownFailure { recipe } = > {
2016-10-05 13:58:18 -07:00
try ! ( write! ( f , " Recipe \" {} \" failed for an unknown reason " , recipe ) ) ;
} ,
2016-10-23 20:39:50 -07:00
RunError ::IoError { recipe , ref io_error } = > {
2016-10-05 13:58:18 -07:00
try ! ( match io_error . kind ( ) {
2016-10-30 18:12:59 -07:00
io ::ErrorKind ::NotFound = > write! ( f , " Recipe \" {} \" could not be run because just could not find `sh` the command: \n {} " , recipe , io_error ) ,
io ::ErrorKind ::PermissionDenied = > write! ( f , " Recipe \" {} \" could not be run because just could not run `sh`: \n {} " , recipe , io_error ) ,
2016-10-30 00:20:29 -07:00
_ = > write! ( f , " Recipe \" {} \" could not be run because of an IO error while launching `sh`: \n {} " , recipe , io_error ) ,
2016-10-05 13:58:18 -07:00
} ) ;
} ,
2016-10-23 20:39:50 -07:00
RunError ::TmpdirIoError { recipe , ref io_error } = >
2016-10-07 17:56:52 -07:00
try ! ( write! ( f , " Recipe \" {} \" could not be run because of an IO error while trying to create a temporary directory or write a file to that directory`: \n {} " , recipe , io_error ) ) ,
2016-10-30 01:27:05 -07:00
RunError ::BacktickCode { code , ref token } = > {
try ! ( write! ( f , " backtick failed with exit code {} \n " , code ) ) ;
match token . text . lines ( ) . nth ( token . line ) {
Some ( line ) = > {
let line_number_width = token . line . to_string ( ) . len ( ) ;
try ! ( write! ( f , " {0:1$} | \n " , " " , line_number_width ) ) ;
try ! ( write! ( f , " {} | {} \n " , token . line + 1 , line ) ) ;
try ! ( write! ( f , " {0:1$} | " , " " , line_number_width ) ) ;
try ! ( write! ( f , " {0:1$}{2:^<3$} " , " " ,
token . column + token . prefix . len ( ) , " " , token . lexeme . len ( ) ) ) ;
} ,
None = > if token . index ! = token . text . len ( ) {
try ! ( write! ( f , " internal error: Error has invalid line number: {} " , token . line + 1 ) )
} ,
}
2016-10-30 00:20:29 -07:00
}
RunError ::BacktickSignal { signal } = > {
2016-10-30 01:27:05 -07:00
try ! ( write! ( f , " backtick was terminated by signal {} " , signal ) ) ;
2016-10-30 00:20:29 -07:00
}
RunError ::BacktickUnknownFailure = > {
2016-10-30 01:27:05 -07:00
try ! ( write! ( f , " backtick failed for an uknown reason " ) ) ;
2016-10-30 00:20:29 -07:00
}
RunError ::BacktickIoError { ref io_error } = > {
try ! ( match io_error . kind ( ) {
2016-10-30 18:12:59 -07:00
io ::ErrorKind ::NotFound = > write! ( f , " backtick could not be run because just could not find `sh` the command: \n {} " , io_error ) ,
io ::ErrorKind ::PermissionDenied = > write! ( f , " backtick could not be run because just could not run `sh`: \n {} " , io_error ) ,
2016-10-30 00:20:29 -07:00
_ = > write! ( f , " backtick could not be run because of an IO error while launching `sh`: \n {} " , io_error ) ,
} ) ;
}
RunError ::BacktickUtf8Error { ref utf8_error } = > {
try ! ( write! ( f , " backtick succeeded but stdout was not utf8: {} " , utf8_error ) ) ;
}
2016-10-29 00:55:47 -07:00
RunError ::InternalError { ref message } = > {
2016-10-30 18:12:59 -07:00
try ! ( write! ( f , " internal error, this may indicate a bug in just: {} \n consider filing an issue: https://github.com/casey/just/issues/new " , message ) ) ;
2016-10-29 00:55:47 -07:00
}
2016-10-03 23:55:55 -07:00
}
2016-10-30 01:27:05 -07:00
2016-10-03 23:55:55 -07:00
Ok ( ( ) )
2016-10-02 22:30:28 -07:00
}
}
2016-10-30 01:27:05 -07:00
#[ derive(Debug, PartialEq, Clone) ]
2016-10-16 18:59:49 -07:00
struct Token < ' a > {
2016-10-22 23:18:26 -07:00
index : usize ,
2016-10-16 18:59:49 -07:00
line : usize ,
2016-10-22 23:18:26 -07:00
column : usize ,
2016-10-23 16:43:52 -07:00
text : & ' a str ,
2016-10-16 18:59:49 -07:00
prefix : & ' a str ,
lexeme : & ' a str ,
2016-10-27 18:48:55 -07:00
kind : TokenKind ,
2016-10-16 18:59:49 -07:00
}
2016-10-22 23:18:26 -07:00
impl < ' a > Token < ' a > {
2016-10-23 16:43:52 -07:00
fn error ( & self , kind : ErrorKind < ' a > ) -> Error < ' a > {
2016-10-22 23:18:26 -07:00
Error {
2016-10-23 16:43:52 -07:00
text : self . text ,
index : self . index + self . prefix . len ( ) ,
2016-10-22 23:18:26 -07:00
line : self . line ,
2016-10-23 16:43:52 -07:00
column : self . column + self . prefix . len ( ) ,
width : Some ( self . lexeme . len ( ) ) ,
2016-10-22 23:18:26 -07:00
kind : kind ,
}
}
}
2016-10-16 18:59:49 -07:00
#[ derive(Debug, PartialEq, Clone, Copy) ]
2016-10-23 18:46:04 -07:00
enum TokenKind {
2016-10-29 01:58:30 -07:00
Backtick ,
2016-10-16 18:59:49 -07:00
Colon ,
Comment ,
Dedent ,
2016-10-29 01:58:30 -07:00
Eof ,
Eol ,
Equals ,
Indent ,
2016-10-26 20:54:44 -07:00
InterpolationEnd ,
2016-10-29 01:58:30 -07:00
InterpolationStart ,
2016-10-26 20:54:44 -07:00
Line ,
2016-10-29 01:58:30 -07:00
Name ,
Plus ,
StringToken ,
2016-10-30 16:56:22 -07:00
RawString ,
2016-10-29 01:58:30 -07:00
Text ,
2016-10-16 18:59:49 -07:00
}
2016-10-23 18:46:04 -07:00
impl Display for TokenKind {
2016-10-23 16:43:52 -07:00
fn fmt ( & self , f : & mut fmt ::Formatter ) -> Result < ( ) , fmt ::Error > {
try ! ( write! ( f , " {} " , match * self {
2016-10-29 01:58:30 -07:00
Backtick = > " backtick " ,
2016-10-26 20:54:44 -07:00
Colon = > " \" : \" " ,
Comment = > " comment " ,
Dedent = > " dedent " ,
Eof = > " end of file " ,
2016-10-29 01:58:30 -07:00
Eol = > " end of line " ,
Equals = > " \" = \" " ,
Indent = > " indent " ,
InterpolationEnd = > " }} " ,
InterpolationStart = > " {{ " ,
Line = > " command " ,
Name = > " name " ,
Plus = > " \" + \" " ,
StringToken = > " string " ,
2016-10-30 16:56:22 -07:00
RawString = > " raw string " ,
2016-10-29 01:58:30 -07:00
Text = > " command text " ,
2016-10-23 16:43:52 -07:00
} ) ) ;
Ok ( ( ) )
}
}
2016-10-23 18:46:04 -07:00
use TokenKind ::* ;
2016-10-16 18:59:49 -07:00
fn token ( pattern : & str ) -> Regex {
let mut s = String ::new ( ) ;
s + = r "^(?m)([ \t]*)(" ;
s + = pattern ;
s + = " ) " ;
re ( & s )
}
2016-10-28 00:06:36 -07:00
fn tokenize ( text : & str ) -> Result < Vec < Token > , Error > {
2016-10-22 23:18:26 -07:00
lazy_static! {
2016-10-30 14:37:03 -07:00
static ref BACKTICK : Regex = token ( r "`[^`\n\r]*`" ) ;
static ref COLON : Regex = token ( r ":" ) ;
static ref COMMENT : Regex = token ( r "#([^!].*)?$" ) ;
static ref EOF : Regex = token ( r "(?-m)$" ) ;
static ref EOL : Regex = token ( r "\n|\r\n" ) ;
static ref EQUALS : Regex = token ( r "=" ) ;
static ref INTERPOLATION_END : Regex = token ( r "[}][}]" ) ;
static ref INTERPOLATION_START_TOKEN : Regex = token ( r "[{][{]" ) ;
static ref NAME : Regex = token ( r "([a-zA-Z_-][a-zA-Z0-9_-]*)" ) ;
static ref PLUS : Regex = token ( r "[+]" ) ;
static ref STRING : Regex = token ( " \" " ) ;
2016-10-30 16:56:22 -07:00
static ref RAW_STRING : Regex = token ( r # "'[^'\r\n]*'"# ) ;
2016-10-27 09:44:07 -07:00
static ref INDENT : Regex = re ( r "^([ \t]*)[^ \t\n\r]" ) ;
static ref INTERPOLATION_START : Regex = re ( r "^[{][{]" ) ;
static ref LEADING_TEXT : Regex = re ( r "^(?m)(.+?)[{][{]" ) ;
2016-10-29 01:58:30 -07:00
static ref LINE : Regex = re ( r "^(?m)[ \t]+[^ \t\n\r].*$" ) ;
2016-10-27 09:44:07 -07:00
static ref TEXT : Regex = re ( r "^(?m)(.+)" ) ;
2016-10-26 20:54:44 -07:00
}
#[ derive(PartialEq) ]
enum State < ' a > {
Start ,
Indent ( & ' a str ) ,
Text ,
Interpolation ,
}
2016-10-16 18:59:49 -07:00
fn indentation ( text : & str ) -> Option < & str > {
2016-10-22 23:18:26 -07:00
INDENT . captures ( text ) . map ( | captures | captures . at ( 1 ) . unwrap ( ) )
2016-10-16 18:59:49 -07:00
}
2016-10-27 09:44:07 -07:00
let mut tokens = vec! [ ] ;
let mut rest = text ;
let mut index = 0 ;
let mut line = 0 ;
let mut column = 0 ;
let mut state = vec! [ State ::Start ] ;
2016-10-22 23:18:26 -07:00
macro_rules ! error {
( $kind :expr ) = > { {
Err ( Error {
text : text ,
index : index ,
line : line ,
column : column ,
2016-10-23 16:43:52 -07:00
width : None ,
2016-10-22 23:18:26 -07:00
kind : $kind ,
} )
} } ;
}
2016-10-16 18:59:49 -07:00
loop {
2016-10-22 23:18:26 -07:00
if column = = 0 {
2016-10-27 18:48:55 -07:00
if let Some ( kind ) = match ( state . last ( ) . unwrap ( ) , indentation ( rest ) ) {
2016-10-22 23:18:26 -07:00
// ignore: was no indentation and there still isn't
2016-10-23 20:39:50 -07:00
// or current line is blank
2016-10-26 20:54:44 -07:00
( & State ::Start , Some ( " " ) ) | ( _ , None ) = > {
2016-10-22 23:18:26 -07:00
None
}
// indent: was no indentation, now there is
2016-10-26 20:54:44 -07:00
( & State ::Start , Some ( current ) ) = > {
2016-10-23 16:43:52 -07:00
if mixed_whitespace ( current ) {
return error! ( ErrorKind ::MixedLeadingWhitespace { whitespace : current } )
}
2016-10-26 20:54:44 -07:00
//indent = Some(current);
state . push ( State ::Indent ( current ) ) ;
2016-10-16 18:59:49 -07:00
Some ( Indent )
}
2016-10-22 23:18:26 -07:00
// dedent: there was indentation and now there isn't
2016-10-26 20:54:44 -07:00
( & State ::Indent ( _ ) , Some ( " " ) ) = > {
// indent = None;
state . pop ( ) ;
2016-10-22 23:18:26 -07:00
Some ( Dedent )
}
// was indentation and still is, check if the new indentation matches
2016-10-26 20:54:44 -07:00
( & State ::Indent ( previous ) , Some ( current ) ) = > {
2016-10-16 18:59:49 -07:00
if ! current . starts_with ( previous ) {
2016-10-22 23:18:26 -07:00
return error! ( ErrorKind ::InconsistentLeadingWhitespace {
expected : previous ,
found : current
} ) ;
2016-10-16 18:59:49 -07:00
}
None
}
2016-10-26 20:54:44 -07:00
// at column 0 in some other state: this should never happen
( & State ::Text , _ ) | ( & State ::Interpolation , _ ) = > {
return error! ( ErrorKind ::InternalError {
message : " unexpected state at column 0 " . to_string ( )
} ) ;
}
2016-10-16 18:59:49 -07:00
} {
tokens . push ( Token {
2016-10-22 23:18:26 -07:00
index : index ,
2016-10-16 18:59:49 -07:00
line : line ,
2016-10-22 23:18:26 -07:00
column : column ,
2016-10-23 16:43:52 -07:00
text : text ,
2016-10-16 18:59:49 -07:00
prefix : " " ,
lexeme : " " ,
2016-10-27 18:48:55 -07:00
kind : kind ,
2016-10-16 18:59:49 -07:00
} ) ;
}
}
2016-10-23 16:43:52 -07:00
// insert a dedent if we're indented and we hit the end of the file
2016-10-28 00:06:36 -07:00
if & State ::Start ! = state . last ( ) . unwrap ( ) & & EOF . is_match ( rest ) {
tokens . push ( Token {
index : index ,
line : line ,
column : column ,
text : text ,
prefix : " " ,
lexeme : " " ,
kind : Dedent ,
} ) ;
2016-10-23 16:43:52 -07:00
}
2016-10-27 18:48:55 -07:00
let ( prefix , lexeme , kind ) =
2016-10-26 20:54:44 -07:00
if let ( 0 , & State ::Indent ( indent ) , Some ( captures ) ) = ( column , state . last ( ) . unwrap ( ) , LINE . captures ( rest ) ) {
2016-10-16 18:59:49 -07:00
let line = captures . at ( 0 ) . unwrap ( ) ;
if ! line . starts_with ( indent ) {
2016-10-22 23:18:26 -07:00
return error! ( ErrorKind ::InternalError { message : " unexpected indent " . to_string ( ) } ) ;
2016-10-16 18:59:49 -07:00
}
2016-10-26 20:54:44 -07:00
state . push ( State ::Text ) ;
( & line [ 0 .. indent . len ( ) ] , " " , Line )
} else if let Some ( captures ) = EOF . captures ( rest ) {
( captures . at ( 1 ) . unwrap ( ) , captures . at ( 2 ) . unwrap ( ) , Eof )
2016-10-28 00:06:36 -07:00
} else if let State ::Text = * state . last ( ) . unwrap ( ) {
2016-10-26 20:54:44 -07:00
if let Some ( captures ) = INTERPOLATION_START . captures ( rest ) {
state . push ( State ::Interpolation ) ;
( " " , captures . at ( 0 ) . unwrap ( ) , InterpolationStart )
} else if let Some ( captures ) = LEADING_TEXT . captures ( rest ) {
( " " , captures . at ( 1 ) . unwrap ( ) , Text )
} else if let Some ( captures ) = TEXT . captures ( rest ) {
( " " , captures . at ( 1 ) . unwrap ( ) , Text )
} else if let Some ( captures ) = EOL . captures ( rest ) {
state . pop ( ) ;
( captures . at ( 1 ) . unwrap ( ) , captures . at ( 2 ) . unwrap ( ) , Eol )
} else {
return error! ( ErrorKind ::InternalError {
message : format ! ( " Could not match token in text state: \" {} \" " , rest )
} ) ;
}
2016-10-27 09:44:07 -07:00
} else if let Some ( captures ) = INTERPOLATION_START_TOKEN . captures ( rest ) {
( captures . at ( 1 ) . unwrap ( ) , captures . at ( 2 ) . unwrap ( ) , InterpolationStart )
2016-10-26 20:54:44 -07:00
} else if let Some ( captures ) = INTERPOLATION_END . captures ( rest ) {
2016-10-27 09:44:07 -07:00
if state . last ( ) . unwrap ( ) = = & State ::Interpolation {
state . pop ( ) ;
2016-10-26 20:54:44 -07:00
}
( captures . at ( 1 ) . unwrap ( ) , captures . at ( 2 ) . unwrap ( ) , InterpolationEnd )
2016-10-22 23:18:26 -07:00
} else if let Some ( captures ) = NAME . captures ( rest ) {
2016-10-16 18:59:49 -07:00
( captures . at ( 1 ) . unwrap ( ) , captures . at ( 2 ) . unwrap ( ) , Name )
2016-10-22 23:18:26 -07:00
} else if let Some ( captures ) = EOL . captures ( rest ) {
2016-10-26 20:54:44 -07:00
if state . last ( ) . unwrap ( ) = = & State ::Interpolation {
2016-10-29 22:56:47 -07:00
return error! ( ErrorKind ::InternalError {
message : " hit EOL while still in interpolation state " . to_string ( )
} ) ;
2016-10-26 20:54:44 -07:00
}
2016-10-16 18:59:49 -07:00
( captures . at ( 1 ) . unwrap ( ) , captures . at ( 2 ) . unwrap ( ) , Eol )
2016-10-29 01:58:30 -07:00
} else if let Some ( captures ) = BACKTICK . captures ( rest ) {
( captures . at ( 1 ) . unwrap ( ) , captures . at ( 2 ) . unwrap ( ) , Backtick )
2016-10-22 23:18:26 -07:00
} else if let Some ( captures ) = COLON . captures ( rest ) {
2016-10-16 18:59:49 -07:00
( captures . at ( 1 ) . unwrap ( ) , captures . at ( 2 ) . unwrap ( ) , Colon )
2016-10-25 19:11:58 -07:00
} else if let Some ( captures ) = PLUS . captures ( rest ) {
( captures . at ( 1 ) . unwrap ( ) , captures . at ( 2 ) . unwrap ( ) , Plus )
2016-10-22 23:18:26 -07:00
} else if let Some ( captures ) = EQUALS . captures ( rest ) {
2016-10-16 18:59:49 -07:00
( captures . at ( 1 ) . unwrap ( ) , captures . at ( 2 ) . unwrap ( ) , Equals )
2016-10-22 23:18:26 -07:00
} else if let Some ( captures ) = COMMENT . captures ( rest ) {
2016-10-16 18:59:49 -07:00
( captures . at ( 1 ) . unwrap ( ) , captures . at ( 2 ) . unwrap ( ) , Comment )
2016-10-30 16:56:22 -07:00
} else if let Some ( captures ) = RAW_STRING . captures ( rest ) {
( captures . at ( 1 ) . unwrap ( ) , captures . at ( 2 ) . unwrap ( ) , RawString )
2016-10-25 19:11:58 -07:00
} else if let Some ( captures ) = STRING . captures ( rest ) {
2016-10-28 00:06:36 -07:00
let prefix = captures . at ( 1 ) . unwrap ( ) ;
let contents = & rest [ prefix . len ( ) + 1 .. ] ;
if contents . is_empty ( ) {
return error! ( ErrorKind ::UnterminatedString ) ;
}
let mut len = 0 ;
let mut escape = false ;
for c in contents . chars ( ) {
if c = = '\n' | | c = = '\r' {
return error! ( ErrorKind ::UnterminatedString ) ;
} else if ! escape & & c = = '"' {
break ;
} else if ! escape & & c = = '\\' {
escape = true ;
} else if escape {
escape = false ;
}
len + = c . len_utf8 ( ) ;
}
let start = prefix . len ( ) ;
let content_end = start + len + 1 ;
if escape | | content_end > = rest . len ( ) {
return error! ( ErrorKind ::UnterminatedString ) ;
}
( prefix , & rest [ start .. content_end + 1 ] , StringToken )
2016-10-25 19:11:58 -07:00
} else if rest . starts_with ( " #! " ) {
return error! ( ErrorKind ::OuterShebang )
2016-10-16 18:59:49 -07:00
} else {
2016-10-25 19:11:58 -07:00
return error! ( ErrorKind ::UnknownStartOfToken )
2016-10-16 18:59:49 -07:00
} ;
let len = prefix . len ( ) + lexeme . len ( ) ;
tokens . push ( Token {
2016-10-22 23:18:26 -07:00
index : index ,
line : line ,
column : column ,
2016-10-16 18:59:49 -07:00
prefix : prefix ,
2016-10-23 16:43:52 -07:00
text : text ,
2016-10-16 18:59:49 -07:00
lexeme : lexeme ,
2016-10-27 18:48:55 -07:00
kind : kind ,
2016-10-16 18:59:49 -07:00
} ) ;
2016-10-26 20:54:44 -07:00
if len = = 0 {
2016-10-29 22:56:47 -07:00
let last = tokens . last ( ) . unwrap ( ) ;
match last . kind {
2016-10-26 20:54:44 -07:00
Eof = > { } ,
2016-10-29 22:56:47 -07:00
_ = > return Err ( last . error ( ErrorKind ::InternalError {
message : format ! ( " zero length token: {:?} " , last )
} ) ) ,
2016-10-26 20:54:44 -07:00
}
}
2016-10-27 18:48:55 -07:00
match tokens . last ( ) . unwrap ( ) . kind {
2016-10-16 18:59:49 -07:00
Eol = > {
line + = 1 ;
2016-10-22 23:18:26 -07:00
column = 0 ;
2016-10-16 18:59:49 -07:00
} ,
Eof = > {
break ;
} ,
_ = > {
2016-10-22 23:18:26 -07:00
column + = len ;
2016-10-16 18:59:49 -07:00
}
}
rest = & rest [ len .. ] ;
2016-10-22 23:18:26 -07:00
index + = len ;
2016-10-16 18:59:49 -07:00
}
Ok ( tokens )
}
2016-10-23 20:39:50 -07:00
fn parse ( text : & str ) -> Result < Justfile , Error > {
2016-10-22 23:18:26 -07:00
let tokens = try ! ( tokenize ( text ) ) ;
2016-10-27 18:48:55 -07:00
let filtered : Vec < _ > = tokens . into_iter ( ) . filter ( | token | token . kind ! = Comment ) . collect ( ) ;
2016-10-30 16:15:18 -07:00
let parser = Parser {
2016-10-30 16:34:39 -07:00
text : text ,
tokens : itertools ::put_back ( filtered ) ,
recipes : Map ::< & str , Recipe > ::new ( ) ,
assignments : Map ::< & str , Expression > ::new ( ) ,
assignment_tokens : Map ::< & str , Token > ::new ( ) ,
exports : Set ::< & str > ::new ( ) ,
2016-10-22 23:18:26 -07:00
} ;
let justfile = try ! ( parser . file ( ) ) ;
Ok ( justfile )
2016-10-16 18:59:49 -07:00
}
2016-10-22 23:18:26 -07:00
struct Parser < ' a > {
2016-10-30 16:34:39 -07:00
text : & ' a str ,
tokens : itertools ::PutBack < std ::vec ::IntoIter < Token < ' a > > > ,
recipes : Map < & ' a str , Recipe < ' a > > ,
assignments : Map < & ' a str , Expression < ' a > > ,
assignment_tokens : Map < & ' a str , Token < ' a > > ,
exports : Set < & ' a str > ,
2016-10-16 18:59:49 -07:00
}
2016-10-22 23:18:26 -07:00
impl < ' a > Parser < ' a > {
2016-10-27 18:48:55 -07:00
fn peek ( & mut self , kind : TokenKind ) -> bool {
2016-10-30 16:15:18 -07:00
let next = self . tokens . next ( ) . unwrap ( ) ;
let result = next . kind = = kind ;
self . tokens . put_back ( next ) ;
result
2016-10-23 16:43:52 -07:00
}
2016-10-27 18:48:55 -07:00
fn accept ( & mut self , kind : TokenKind ) -> Option < Token < ' a > > {
if self . peek ( kind ) {
2016-10-22 23:18:26 -07:00
self . tokens . next ( )
2016-10-16 18:59:49 -07:00
} else {
None
}
}
2016-10-27 18:48:55 -07:00
fn accepted ( & mut self , kind : TokenKind ) -> bool {
self . accept ( kind ) . is_some ( )
2016-10-16 18:59:49 -07:00
}
2016-10-27 18:48:55 -07:00
fn expect ( & mut self , kind : TokenKind ) -> Option < Token < ' a > > {
if self . peek ( kind ) {
2016-10-23 16:43:52 -07:00
self . tokens . next ( ) ;
None
} else {
self . tokens . next ( )
}
2016-10-22 23:18:26 -07:00
}
2016-10-23 16:43:52 -07:00
fn expect_eol ( & mut self ) -> Option < Token < ' a > > {
if self . peek ( Eol ) {
self . accept ( Eol ) ;
None
} else if self . peek ( Eof ) {
None
} else {
self . tokens . next ( )
2016-10-16 18:59:49 -07:00
}
}
2016-10-25 19:11:58 -07:00
fn unexpected_token ( & self , found : & Token < ' a > , expected : & [ TokenKind ] ) -> Error < ' a > {
found . error ( ErrorKind ::UnexpectedToken {
expected : expected . to_vec ( ) ,
2016-10-27 18:48:55 -07:00
found : found . kind ,
2016-10-25 19:11:58 -07:00
} )
}
2016-10-30 16:34:39 -07:00
fn recipe ( & mut self , name : Token < ' a > ) -> Result < ( ) , Error < ' a > > {
if let Some ( recipe ) = self . recipes . get ( name . lexeme ) {
return Err ( name . error ( ErrorKind ::DuplicateRecipe {
recipe : recipe . name ,
first : recipe . line_number
} ) ) ;
}
2016-10-31 19:11:27 -07:00
let mut parameters = vec! [ ] ;
let mut parameter_tokens = vec! [ ] ;
while let Some ( parameter ) = self . accept ( Name ) {
if parameters . contains ( & parameter . lexeme ) {
return Err ( parameter . error ( ErrorKind ::DuplicateParameter {
recipe : name . lexeme , parameter : parameter . lexeme
2016-10-23 16:43:52 -07:00
} ) ) ;
2016-10-16 18:59:49 -07:00
}
2016-10-31 19:11:27 -07:00
parameters . push ( parameter . lexeme ) ;
parameter_tokens . push ( parameter ) ;
2016-10-16 18:59:49 -07:00
}
2016-10-23 16:43:52 -07:00
if let Some ( token ) = self . expect ( Colon ) {
2016-10-31 19:11:27 -07:00
// if we haven't accepted any parameters, an equals
2016-10-26 20:54:44 -07:00
// would have been fine as part of an assignment
2016-10-31 19:11:27 -07:00
if parameters . is_empty ( ) {
2016-10-25 19:11:58 -07:00
return Err ( self . unexpected_token ( & token , & [ Name , Colon , Equals ] ) ) ;
} else {
return Err ( self . unexpected_token ( & token , & [ Name , Colon ] ) ) ;
}
2016-10-23 16:43:52 -07:00
}
2016-10-16 18:59:49 -07:00
let mut dependencies = vec! [ ] ;
2016-10-23 16:43:52 -07:00
let mut dependency_tokens = vec! [ ] ;
while let Some ( dependency ) = self . accept ( Name ) {
if dependencies . contains ( & dependency . lexeme ) {
return Err ( dependency . error ( ErrorKind ::DuplicateDependency {
2016-10-30 16:34:39 -07:00
recipe : name . lexeme ,
2016-10-23 16:43:52 -07:00
dependency : dependency . lexeme
} ) ) ;
2016-10-16 18:59:49 -07:00
}
2016-10-23 16:43:52 -07:00
dependencies . push ( dependency . lexeme ) ;
dependency_tokens . push ( dependency ) ;
}
if let Some ( token ) = self . expect_eol ( ) {
return Err ( self . unexpected_token ( & token , & [ Name , Eol , Eof ] ) ) ;
2016-10-16 18:59:49 -07:00
}
2016-10-27 09:44:07 -07:00
let mut lines = vec! [ ] ;
2016-10-27 00:13:10 -07:00
let mut shebang = false ;
2016-10-26 22:04:12 -07:00
if self . accepted ( Indent ) {
while ! self . accepted ( Dedent ) {
2016-10-27 00:13:10 -07:00
if self . accepted ( Eol ) {
continue ;
}
2016-10-26 22:04:12 -07:00
if let Some ( token ) = self . expect ( Line ) {
return Err ( token . error ( ErrorKind ::InternalError {
2016-10-27 18:48:55 -07:00
message : format ! ( " Expected a line but got {} " , token . kind )
2016-10-26 22:04:12 -07:00
} ) )
}
let mut pieces = vec! [ ] ;
2016-10-27 00:13:10 -07:00
while ! ( self . accepted ( Eol ) | | self . peek ( Dedent ) ) {
2016-10-26 22:04:12 -07:00
if let Some ( token ) = self . accept ( Text ) {
2016-10-27 00:13:10 -07:00
if pieces . is_empty ( ) {
2016-10-27 09:44:07 -07:00
if lines . is_empty ( ) {
2016-10-27 00:13:10 -07:00
if token . lexeme . starts_with ( " #! " ) {
shebang = true ;
}
2016-11-02 22:01:38 -07:00
} else if ! shebang & & ( token . lexeme . starts_with ( ' ' ) | |
token . lexeme . starts_with ( '\t' ) ) {
2016-10-27 00:13:10 -07:00
return Err ( token . error ( ErrorKind ::ExtraLeadingWhitespace ) ) ;
}
}
2016-10-27 09:44:07 -07:00
pieces . push ( Fragment ::Text { text : token } ) ;
2016-10-26 22:04:12 -07:00
} else if let Some ( token ) = self . expect ( InterpolationStart ) {
return Err ( self . unexpected_token ( & token , & [ Text , InterpolationStart , Eol ] ) ) ;
} else {
2016-10-27 18:01:07 -07:00
pieces . push ( Fragment ::Expression {
2016-10-29 22:56:47 -07:00
expression : try ! ( self . expression ( true ) )
2016-10-27 18:01:07 -07:00
} ) ;
2016-10-26 22:04:12 -07:00
if let Some ( token ) = self . expect ( InterpolationEnd ) {
return Err ( self . unexpected_token ( & token , & [ InterpolationEnd ] ) ) ;
}
}
}
2016-10-27 09:44:07 -07:00
lines . push ( pieces ) ;
2016-10-26 22:04:12 -07:00
}
}
2016-10-30 16:34:39 -07:00
self . recipes . insert ( name . lexeme , Recipe {
line_number : name . line ,
name : name . lexeme ,
2016-10-23 16:43:52 -07:00
dependencies : dependencies ,
dependency_tokens : dependency_tokens ,
2016-10-31 19:11:27 -07:00
parameters : parameters ,
parameter_tokens : parameter_tokens ,
2016-10-27 18:48:55 -07:00
lines : lines ,
2016-10-23 16:43:52 -07:00
shebang : shebang ,
2016-10-30 16:34:39 -07:00
} ) ;
Ok ( ( ) )
2016-10-16 18:59:49 -07:00
}
2016-10-22 23:18:26 -07:00
2016-10-30 16:34:39 -07:00
2016-10-26 22:04:12 -07:00
fn expression ( & mut self , interpolation : bool ) -> Result < Expression < ' a > , Error < ' a > > {
2016-10-25 19:11:58 -07:00
let first = self . tokens . next ( ) . unwrap ( ) ;
2016-10-27 18:48:55 -07:00
let lhs = match first . kind {
2016-10-30 16:56:22 -07:00
Name = > Expression ::Variable { name : first . lexeme , token : first } ,
Backtick = > Expression ::Backtick {
2016-10-29 23:39:12 -07:00
raw : & first . lexeme [ 1 .. first . lexeme . len ( ) - 1 ] ,
token : first
} ,
2016-10-30 16:56:22 -07:00
RawString = > {
let raw = & first . lexeme [ 1 .. first . lexeme . len ( ) - 1 ] ;
Expression ::String { raw : raw , cooked : raw . to_string ( ) }
}
2016-10-28 00:06:36 -07:00
StringToken = > {
let raw = & first . lexeme [ 1 .. first . lexeme . len ( ) - 1 ] ;
let mut cooked = String ::new ( ) ;
let mut escape = false ;
for c in raw . chars ( ) {
if escape {
match c {
'n' = > cooked . push ( '\n' ) ,
'r' = > cooked . push ( '\r' ) ,
't' = > cooked . push ( '\t' ) ,
'\\' = > cooked . push ( '\\' ) ,
'"' = > cooked . push ( '"' ) ,
other = > return Err ( first . error ( ErrorKind ::InvalidEscapeSequence {
character : other ,
} ) ) ,
}
escape = false ;
continue ;
}
if c = = '\\' {
escape = true ;
continue ;
}
cooked . push ( c ) ;
}
Expression ::String { raw : raw , cooked : cooked }
}
_ = > return Err ( self . unexpected_token ( & first , & [ Name , StringToken ] ) ) ,
2016-10-25 19:11:58 -07:00
} ;
if self . accepted ( Plus ) {
2016-10-26 22:04:12 -07:00
let rhs = try ! ( self . expression ( interpolation ) ) ;
2016-10-25 19:11:58 -07:00
Ok ( Expression ::Concatination { lhs : Box ::new ( lhs ) , rhs : Box ::new ( rhs ) } )
2016-10-26 22:04:12 -07:00
} else if interpolation & & self . peek ( InterpolationEnd ) {
Ok ( lhs )
2016-10-25 19:11:58 -07:00
} else if let Some ( token ) = self . expect_eol ( ) {
2016-10-26 22:04:12 -07:00
if interpolation {
2016-10-27 00:13:10 -07:00
return Err ( self . unexpected_token ( & token , & [ Plus , Eol , InterpolationEnd ] ) )
2016-10-26 22:04:12 -07:00
} else {
Err ( self . unexpected_token ( & token , & [ Plus , Eol ] ) )
}
2016-10-25 19:11:58 -07:00
} else {
Ok ( lhs )
}
2016-10-22 23:18:26 -07:00
}
2016-10-16 18:59:49 -07:00
2016-10-30 16:34:39 -07:00
fn assignment ( & mut self , name : Token < ' a > , export : bool ) -> Result < ( ) , Error < ' a > > {
if self . assignments . contains_key ( name . lexeme ) {
return Err ( name . error ( ErrorKind ::DuplicateVariable { variable : name . lexeme } ) ) ;
}
if export {
self . exports . insert ( name . lexeme ) ;
}
let expression = try ! ( self . expression ( false ) ) ;
self . assignments . insert ( name . lexeme , expression ) ;
self . assignment_tokens . insert ( name . lexeme , name ) ;
Ok ( ( ) )
}
2016-10-22 23:18:26 -07:00
2016-10-30 16:34:39 -07:00
fn file ( mut self ) -> Result < Justfile < ' a > , Error < ' a > > {
2016-10-22 23:18:26 -07:00
loop {
match self . tokens . next ( ) {
2016-10-27 18:48:55 -07:00
Some ( token ) = > match token . kind {
2016-10-22 23:18:26 -07:00
Eof = > break ,
Eol = > continue ,
2016-10-30 16:15:18 -07:00
Name = > if token . lexeme = = " export " {
let next = self . tokens . next ( ) . unwrap ( ) ;
if next . kind = = Name & & self . accepted ( Equals ) {
2016-10-30 16:34:39 -07:00
try ! ( self . assignment ( next , true ) ) ;
2016-10-30 16:15:18 -07:00
} else {
self . tokens . put_back ( next ) ;
2016-10-30 16:34:39 -07:00
try ! ( self . recipe ( token ) ) ;
2016-10-30 16:15:18 -07:00
}
} else if self . accepted ( Equals ) {
2016-10-30 16:34:39 -07:00
try ! ( self . assignment ( token , false ) ) ;
2016-10-23 16:43:52 -07:00
} else {
2016-10-30 16:34:39 -07:00
try ! ( self . recipe ( token ) ) ;
2016-10-23 16:43:52 -07:00
} ,
Comment = > return Err ( token . error ( ErrorKind ::InternalError {
message : " found comment in token stream " . to_string ( )
} ) ) ,
2016-10-27 09:44:07 -07:00
_ = > return return Err ( self . unexpected_token ( & token , & [ Name ] ) ) ,
2016-10-22 23:18:26 -07:00
} ,
None = > return Err ( Error {
text : self . text ,
index : 0 ,
line : 0 ,
column : 0 ,
2016-10-23 16:43:52 -07:00
width : None ,
2016-10-22 23:18:26 -07:00
kind : ErrorKind ::InternalError {
message : " unexpected end of token stream " . to_string ( )
}
} ) ,
}
}
2016-10-16 18:59:49 -07:00
2016-10-23 16:43:52 -07:00
if let Some ( token ) = self . tokens . next ( ) {
return Err ( token . error ( ErrorKind ::InternalError {
2016-10-27 18:48:55 -07:00
message : format ! ( " unexpected token remaining after parsing completed: {:?} " , token . kind )
2016-10-22 23:18:26 -07:00
} ) )
2016-10-06 17:43:30 -07:00
}
2016-10-30 16:34:39 -07:00
try ! ( resolve_recipes ( & self . recipes , & self . assignments , self . text ) ) ;
2016-10-23 16:43:52 -07:00
2016-10-30 16:34:39 -07:00
for recipe in self . recipes . values ( ) {
2016-10-31 19:11:27 -07:00
for parameter in & recipe . parameter_tokens {
if self . assignments . contains_key ( parameter . lexeme ) {
return Err ( parameter . error ( ErrorKind ::ParameterShadowsVariable {
parameter : parameter . lexeme
2016-10-25 19:11:58 -07:00
} ) ) ;
}
}
2016-10-27 00:13:10 -07:00
2016-10-29 00:14:41 -07:00
for dependency in & recipe . dependency_tokens {
2016-10-31 19:11:27 -07:00
if ! self . recipes [ dependency . lexeme ] . parameters . is_empty ( ) {
return Err ( dependency . error ( ErrorKind ::DependencyHasParameters {
2016-10-29 00:14:41 -07:00
recipe : recipe . name ,
dependency : dependency . lexeme ,
} ) ) ;
}
}
2016-10-23 16:43:52 -07:00
}
2016-10-30 16:34:39 -07:00
try ! ( resolve_assignments ( & self . assignments , & self . assignment_tokens ) ) ;
2016-10-29 20:39:21 -07:00
2016-10-27 18:48:55 -07:00
Ok ( Justfile {
2016-10-30 16:34:39 -07:00
recipes : self . recipes ,
assignments : self . assignments ,
exports : self . exports ,
2016-10-25 19:11:58 -07:00
} )
2016-10-02 22:30:28 -07:00
}
}