@@ -209,6 +209,8 @@ pub enum Error<'a> {
209
209
ZeroStride ,
210
210
#[ error( "not a composite type: {0:?}" ) ]
211
211
NotCompositeType ( crate :: TypeInner ) ,
212
+ #[ error( "function redefinition: `{0}`" ) ]
213
+ FunctionRedefinition ( & ' a str ) ,
212
214
//MutabilityViolation(&'a str),
213
215
// TODO: these could be replaced with more detailed errors
214
216
#[ error( "other error" ) ]
@@ -237,7 +239,7 @@ impl<'a> Lexer<'a> {
237
239
self . clone ( ) . next ( )
238
240
}
239
241
240
- fn expect ( & mut self , expected : Token < ' a > ) -> Result < ( ) , Error < ' a > > {
242
+ fn expect ( & mut self , expected : Token < ' _ > ) -> Result < ( ) , Error < ' a > > {
241
243
let token = self . next ( ) ;
242
244
if token == expected {
243
245
Ok ( ( ) )
@@ -246,7 +248,7 @@ impl<'a> Lexer<'a> {
246
248
}
247
249
}
248
250
249
- fn skip ( & mut self , what : Token < ' a > ) -> bool {
251
+ fn skip ( & mut self , what : Token < ' _ > ) -> bool {
250
252
let ( token, rest) = lex:: consume_token ( self . input ) ;
251
253
if token == what {
252
254
self . input = rest;
@@ -296,7 +298,7 @@ impl<'a> Lexer<'a> {
296
298
Ok ( pair)
297
299
}
298
300
299
- fn take_until ( & mut self , what : Token < ' a > ) -> Result < Lexer < ' a > , Error < ' a > > {
301
+ fn take_until ( & mut self , what : Token < ' _ > ) -> Result < Lexer < ' a > , Error < ' a > > {
300
302
let original_input = self . input ;
301
303
let initial_len = self . input . len ( ) ;
302
304
let mut used_len = 0 ;
@@ -449,14 +451,16 @@ pub struct ParseError<'a> {
449
451
pub struct Parser {
450
452
scopes : Vec < Scope > ,
451
453
lookup_type : FastHashMap < String , Handle < crate :: Type > > ,
452
- std_namespace : Option < String > ,
454
+ function_lookup : FastHashMap < String , Handle < crate :: Function > > ,
455
+ std_namespace : Option < Vec < String > > ,
453
456
}
454
457
455
458
impl Parser {
456
459
pub fn new ( ) -> Self {
457
460
Parser {
458
461
scopes : Vec :: new ( ) ,
459
462
lookup_type : FastHashMap :: default ( ) ,
463
+ function_lookup : FastHashMap :: default ( ) ,
460
464
std_namespace : None ,
461
465
}
462
466
}
@@ -543,6 +547,49 @@ impl Parser {
543
547
}
544
548
}
545
549
550
+ fn parse_function_call < ' a > (
551
+ & mut self ,
552
+ lexer : & Lexer < ' a > ,
553
+ mut ctx : ExpressionContext < ' a , ' _ , ' _ > ,
554
+ ) -> Result < Option < ( crate :: Expression , Lexer < ' a > ) > , Error < ' a > > {
555
+ let mut lexer = lexer. clone ( ) ;
556
+
557
+ let external_function = if let Some ( std_namespaces) = self . std_namespace . as_deref ( ) {
558
+ std_namespaces. iter ( ) . all ( |namespace| {
559
+ lexer. skip ( Token :: Word ( namespace) ) && lexer. skip ( Token :: DoubleColon )
560
+ } )
561
+ } else {
562
+ false
563
+ } ;
564
+
565
+ let origin = if external_function {
566
+ let function = lexer. next_ident ( ) ?;
567
+ crate :: FunctionOrigin :: External ( function. to_string ( ) )
568
+ } else if let Ok ( function) = lexer. next_ident ( ) {
569
+ if let Some ( & function) = self . function_lookup . get ( function) {
570
+ crate :: FunctionOrigin :: Local ( function)
571
+ } else {
572
+ return Ok ( None ) ;
573
+ }
574
+ } else {
575
+ return Ok ( None ) ;
576
+ } ;
577
+
578
+ if !lexer. skip ( Token :: Paren ( '(' ) ) {
579
+ return Ok ( None ) ;
580
+ }
581
+
582
+ let mut arguments = Vec :: new ( ) ;
583
+ while !lexer. skip ( Token :: Paren ( ')' ) ) {
584
+ if !arguments. is_empty ( ) {
585
+ lexer. expect ( Token :: Separator ( ',' ) ) ?;
586
+ }
587
+ let arg = self . parse_general_expression ( & mut lexer, ctx. reborrow ( ) ) ?;
588
+ arguments. push ( arg) ;
589
+ }
590
+ Ok ( Some ( ( crate :: Expression :: Call { origin, arguments } , lexer) ) )
591
+ }
592
+
546
593
fn parse_const_expression < ' a > (
547
594
& mut self ,
548
595
lexer : & mut Lexer < ' a > ,
@@ -654,22 +701,11 @@ impl Parser {
654
701
self . scopes . pop ( ) ;
655
702
return Ok ( * handle) ;
656
703
}
657
- if self . std_namespace . as_deref ( ) == Some ( word) {
658
- lexer. expect ( Token :: DoubleColon ) ?;
659
- let name = lexer. next_ident ( ) ?;
660
- let mut arguments = Vec :: new ( ) ;
661
- lexer. expect ( Token :: Paren ( '(' ) ) ?;
662
- while !lexer. skip ( Token :: Paren ( ')' ) ) {
663
- if !arguments. is_empty ( ) {
664
- lexer. expect ( Token :: Separator ( ',' ) ) ?;
665
- }
666
- let arg = self . parse_general_expression ( lexer, ctx. reborrow ( ) ) ?;
667
- arguments. push ( arg) ;
668
- }
669
- crate :: Expression :: Call {
670
- origin : crate :: FunctionOrigin :: External ( name. to_owned ( ) ) ,
671
- arguments,
672
- }
704
+ if let Some ( ( expr, new_lexer) ) =
705
+ self . parse_function_call ( & backup, ctx. reborrow ( ) ) ?
706
+ {
707
+ * lexer = new_lexer;
708
+ expr
673
709
} else {
674
710
* lexer = backup;
675
711
let ty = self . parse_type_decl ( lexer, ctx. types ) ?;
@@ -1295,6 +1331,7 @@ impl Parser {
1295
1331
lexer : & mut Lexer < ' a > ,
1296
1332
mut context : StatementContext < ' a , ' _ , ' _ > ,
1297
1333
) -> Result < Option < crate :: Statement > , Error < ' a > > {
1334
+ let backup = lexer. clone ( ) ;
1298
1335
match lexer. next ( ) {
1299
1336
Token :: Separator ( ';' ) => Ok ( Some ( crate :: Statement :: Empty ) ) ,
1300
1337
Token :: Paren ( '}' ) => Ok ( None ) ,
@@ -1387,15 +1424,26 @@ impl Parser {
1387
1424
"continue" => crate :: Statement :: Continue ,
1388
1425
ident => {
1389
1426
// assignment
1390
- let var_expr = context. lookup_ident . lookup ( ident) ?;
1391
- let left = self . parse_postfix ( lexer, context. as_expression ( ) , var_expr) ?;
1392
- lexer. expect ( Token :: Operation ( '=' ) ) ?;
1393
- let value =
1394
- self . parse_general_expression ( lexer, context. as_expression ( ) ) ?;
1395
- lexer. expect ( Token :: Separator ( ';' ) ) ?;
1396
- crate :: Statement :: Store {
1397
- pointer : left,
1398
- value,
1427
+ if let Some ( & var_expr) = context. lookup_ident . get ( ident) {
1428
+ let left =
1429
+ self . parse_postfix ( lexer, context. as_expression ( ) , var_expr) ?;
1430
+ lexer. expect ( Token :: Operation ( '=' ) ) ?;
1431
+ let value =
1432
+ self . parse_general_expression ( lexer, context. as_expression ( ) ) ?;
1433
+ lexer. expect ( Token :: Separator ( ';' ) ) ?;
1434
+ crate :: Statement :: Store {
1435
+ pointer : left,
1436
+ value,
1437
+ }
1438
+ } else if let Some ( ( expr, new_lexer) ) =
1439
+ self . parse_function_call ( & backup, context. as_expression ( ) ) ?
1440
+ {
1441
+ * lexer = new_lexer;
1442
+ context. expressions . append ( expr) ;
1443
+ lexer. expect ( Token :: Separator ( ';' ) ) ?;
1444
+ crate :: Statement :: Empty
1445
+ } else {
1446
+ return Err ( Error :: UnknownIdent ( ident) ) ;
1399
1447
}
1400
1448
}
1401
1449
} ;
@@ -1459,35 +1507,45 @@ impl Parser {
1459
1507
} else {
1460
1508
Some ( self . parse_type_decl ( lexer, & mut module. types ) ?)
1461
1509
} ;
1510
+
1511
+ let fun_handle = module. functions . append ( crate :: Function {
1512
+ name : Some ( fun_name. to_string ( ) ) ,
1513
+ parameter_types,
1514
+ return_type,
1515
+ global_usage : Vec :: new ( ) ,
1516
+ local_variables : Arena :: new ( ) ,
1517
+ expressions,
1518
+ body : Vec :: new ( ) ,
1519
+ } ) ;
1520
+ if self
1521
+ . function_lookup
1522
+ . insert ( fun_name. to_string ( ) , fun_handle)
1523
+ . is_some ( )
1524
+ {
1525
+ return Err ( Error :: FunctionRedefinition ( fun_name) ) ;
1526
+ }
1527
+ let fun = module. functions . get_mut ( fun_handle) ;
1528
+
1462
1529
// read body
1463
- let mut local_variables = Arena :: new ( ) ;
1464
1530
let mut typifier = Typifier :: new ( ) ;
1465
- let body = self . parse_block (
1531
+ fun . body = self . parse_block (
1466
1532
lexer,
1467
1533
StatementContext {
1468
1534
lookup_ident : & mut lookup_ident,
1469
1535
typifier : & mut typifier,
1470
- variables : & mut local_variables,
1471
- expressions : & mut expressions,
1536
+ variables : & mut fun . local_variables ,
1537
+ expressions : & mut fun . expressions ,
1472
1538
types : & mut module. types ,
1473
1539
constants : & mut module. constants ,
1474
1540
global_vars : & module. global_variables ,
1475
1541
} ,
1476
1542
) ?;
1477
1543
// done
1478
- let global_usage = crate :: GlobalUse :: scan ( & expressions, & body, & module. global_variables ) ;
1544
+ fun. global_usage =
1545
+ crate :: GlobalUse :: scan ( & fun. expressions , & fun. body , & module. global_variables ) ;
1479
1546
self . scopes . pop ( ) ;
1480
1547
1481
- let fun = crate :: Function {
1482
- name : Some ( fun_name. to_owned ( ) ) ,
1483
- parameter_types,
1484
- return_type,
1485
- global_usage,
1486
- local_variables,
1487
- expressions,
1488
- body,
1489
- } ;
1490
- Ok ( module. functions . append ( fun) )
1548
+ Ok ( fun_handle)
1491
1549
}
1492
1550
1493
1551
fn parse_global_decl < ' a > (
@@ -1554,10 +1612,16 @@ impl Parser {
1554
1612
other => return Err ( Error :: Unexpected ( other) ) ,
1555
1613
} ;
1556
1614
lexer. expect ( Token :: Word ( "as" ) ) ?;
1557
- let namespace = lexer. next_ident ( ) ?;
1558
- lexer. expect ( Token :: Separator ( ';' ) ) ?;
1615
+ let mut namespaces = Vec :: new ( ) ;
1616
+ loop {
1617
+ namespaces. push ( lexer. next_ident ( ) ?. to_owned ( ) ) ;
1618
+ if lexer. skip ( Token :: Separator ( ';' ) ) {
1619
+ break ;
1620
+ }
1621
+ lexer. expect ( Token :: DoubleColon ) ?;
1622
+ }
1559
1623
match path {
1560
- "GLSL.std.450" => self . std_namespace = Some ( namespace . to_owned ( ) ) ,
1624
+ "GLSL.std.450" => self . std_namespace = Some ( namespaces ) ,
1561
1625
_ => return Err ( Error :: UnknownImport ( path) ) ,
1562
1626
}
1563
1627
self . scopes . pop ( ) ;
0 commit comments