Commit 31ebf181 by Zachary Snow

improved parse errors for certain unmatched tokens

parent 77b2f8b6
...@@ -438,9 +438,9 @@ Descriptions :: { [Description] } ...@@ -438,9 +438,9 @@ Descriptions :: { [Description] }
| Descriptions Description { $1 ++ $2 } | Descriptions Description { $1 ++ $2 }
Description :: { [Description] } Description :: { [Description] }
: Part(ModuleKW , "endmodule" ) { [$1] } : Part(ModuleKW , endmodule ) { [$1] }
| Part(InterfaceKW, "endinterface") { [$1] } | Part(InterfaceKW, endinterface) { [$1] }
| PackageDeclaration { [$1] } | PackageDeclaration { [$1] }
| PackageItem { map PackageItem $1 } | PackageItem { map PackageItem $1 }
OptAsgn :: { Expr } OptAsgn :: { Expr }
...@@ -554,7 +554,7 @@ InterfaceKW :: { PartKW } ...@@ -554,7 +554,7 @@ InterfaceKW :: { PartKW }
: "interface" { Interface } : "interface" { Interface }
PackageDeclaration :: { Description } PackageDeclaration :: { Description }
: "package" Lifetime Identifier ";" PackageItems "endpackage" opt(Tag) { Package $2 $3 $5 } : "package" Lifetime Identifier ";" PackageItems endpackage opt(Tag) { Package $2 $3 $5 }
Tag :: { Identifier } Tag :: { Identifier }
: ":" Identifier { $2 } : ":" Identifier { $2 }
...@@ -677,9 +677,9 @@ ModuleItems :: { [ModuleItem] } ...@@ -677,9 +677,9 @@ ModuleItems :: { [ModuleItem] }
ModuleItem :: { [ModuleItem] } ModuleItem :: { [ModuleItem] }
: NonGenerateModuleItem { $1 } : NonGenerateModuleItem { $1 }
| ConditionalGenerateConstruct { [Generate [$1]] } | ConditionalGenerateConstruct { [Generate [$1]] }
| LoopGenerateConstruct { [Generate [$1]] } | LoopGenerateConstruct { [Generate [$1]] }
| "generate" GenItems "endgenerate" { [Generate $2] } | "generate" GenItems endgenerate { [Generate $2] }
NonGenerateModuleItem :: { [ModuleItem] } NonGenerateModuleItem :: { [ModuleItem] }
-- This item covers module instantiations and all declarations -- This item covers module instantiations and all declarations
: DeclTokens(";") { parseDTsAsModuleItems $1 } : DeclTokens(";") { parseDTsAsModuleItems $1 }
...@@ -844,9 +844,9 @@ PackageItem :: { [PackageItem] } ...@@ -844,9 +844,9 @@ PackageItem :: { [PackageItem] }
NonDeclPackageItem :: { [PackageItem] } NonDeclPackageItem :: { [PackageItem] }
: "typedef" Type Identifier ";" { [Decl $ ParamType Localparam $3 $2] } : "typedef" Type Identifier ";" { [Decl $ ParamType Localparam $3 $2] }
| "typedef" Type Identifier DimensionsNonEmpty ";" { [Decl $ ParamType Localparam $3 (UnpackedType $2 $4)] } | "typedef" Type Identifier DimensionsNonEmpty ";" { [Decl $ ParamType Localparam $3 (UnpackedType $2 $4)] }
| "function" Lifetime FuncRetAndName TFItems DeclsAndStmts "endfunction" opt(Tag) { [Function $2 (fst $3) (snd $3) (map defaultFuncInput $ (map makeInput $4) ++ fst $5) (snd $5)] } | "function" Lifetime FuncRetAndName TFItems DeclsAndStmts endfunction opt(Tag) { [Function $2 (fst $3) (snd $3) (map defaultFuncInput $ (map makeInput $4) ++ fst $5) (snd $5)] }
| "function" Lifetime "void" Identifier TFItems DeclsAndStmts "endfunction" opt(Tag) { [Task $2 $4 (map defaultFuncInput $ $5 ++ fst $6) (snd $6)] } | "function" Lifetime "void" Identifier TFItems DeclsAndStmts endfunction opt(Tag) { [Task $2 $4 (map defaultFuncInput $ $5 ++ fst $6) (snd $6)] }
| "task" Lifetime Identifier TFItems DeclsAndStmts "endtask" opt(Tag) { [Task $2 $3 (map defaultFuncInput $ $4 ++ fst $5) (snd $5)] } | "task" Lifetime Identifier TFItems DeclsAndStmts endtask opt(Tag) { [Task $2 $3 (map defaultFuncInput $ $4 ++ fst $5) (snd $5)] }
| "import" PackageImportItems ";" { map (uncurry Import) $2 } | "import" PackageImportItems ";" { map (uncurry Import) $2 }
| "export" PackageImportItems ";" { map (uncurry Export) $2 } | "export" PackageImportItems ";" { map (uncurry Export) $2 }
| "export" "*" "::" "*" ";" { [Export "" ""] } | "export" "*" "::" "*" ";" { [Export "" ""] }
...@@ -995,10 +995,10 @@ StmtAsgn :: { Stmt } ...@@ -995,10 +995,10 @@ StmtAsgn :: { Stmt }
| Identifier "::" Identifier ";" { Subroutine (PSIdent $1 $3) (Args [] []) } | Identifier "::" Identifier ";" { Subroutine (PSIdent $1 $3) (Args [] []) }
| Identifier "::" Identifier CallArgs ";" { Subroutine (PSIdent $1 $3) $4 } | Identifier "::" Identifier CallArgs ";" { Subroutine (PSIdent $1 $3) $4 }
StmtNonAsgn :: { Stmt } StmtNonAsgn :: { Stmt }
: StmtBlock(BlockKWSeq, "end" ) { $1 } : StmtBlock(BlockKWSeq, end ) { $1 }
| StmtBlock(BlockKWPar, "join") { $1 } | StmtBlock(BlockKWPar, join) { $1 }
| StmtNonBlock { $1 } | StmtNonBlock { $1 }
| Identifier ":" StmtNonBlock { Block Seq $1 [] [$3] } | Identifier ":" StmtNonBlock { Block Seq $1 [] [$3] }
StmtBlock(begin, end) :: { Stmt } StmtBlock(begin, end) :: { Stmt }
: begin StrTag DeclsAndStmts end StrTag { uncurry (Block $1 $ combineTags $2 $5) $3 } : begin StrTag DeclsAndStmts end StrTag { uncurry (Block $1 $ combineTags $2 $5) $3 }
| Identifier ":" begin DeclsAndStmts end StrTag { uncurry (Block $3 $ combineTags $1 $6) $4 } | Identifier ":" begin DeclsAndStmts end StrTag { uncurry (Block $3 $ combineTags $1 $6) $4 }
...@@ -1317,7 +1317,7 @@ LoopGenerateConstruct :: { GenItem } ...@@ -1317,7 +1317,7 @@ LoopGenerateConstruct :: { GenItem }
: "for" "(" GenvarInitialization ";" Expr ";" GenvarIteration ")" GenItem { $3 $5 $7 $9 } : "for" "(" GenvarInitialization ";" Expr ";" GenvarIteration ")" GenItem { $3 $5 $7 $9 }
GenBlock :: { (Identifier, [GenItem]) } GenBlock :: { (Identifier, [GenItem]) }
: "begin" StrTag GenItems "end" StrTag { (combineTags $2 $5, $3) } : "begin" StrTag GenItems end StrTag { (combineTags $2 $5, $3) }
GenCases :: { [GenCase] } GenCases :: { [GenCase] }
: GenCase { [$1] } : GenCase { [$1] }
...@@ -1379,6 +1379,15 @@ Trace :: { String } ...@@ -1379,6 +1379,15 @@ Trace :: { String }
position :: { Position } position :: { Position }
: {- empty -} {% gets fst } : {- empty -} {% gets fst }
end : "end" {} | error {% missingToken "end" }
endfunction : "endfunction" {} | error {% missingToken "endfunction" }
endgenerate : "endgenerate" {} | error {% missingToken "endgenerate" }
endinterface : "endinterface" {} | error {% missingToken "endinterface" }
endmodule : "endmodule" {} | error {% missingToken "endmodule" }
endpackage : "endpackage" {} | error {% missingToken "endpackage" }
endtask : "endtask" {} | error {% missingToken "endtask" }
join : "join" {} | error {% missingToken "join" }
{ {
type ParseState = StateT (Position, [Token]) (ExceptT String IO) type ParseState = StateT (Position, [Token]) (ExceptT String IO)
...@@ -1399,7 +1408,9 @@ positionKeep cont = do ...@@ -1399,7 +1408,9 @@ positionKeep cont = do
parseError :: Token -> ParseState a parseError :: Token -> ParseState a
parseError a = case a of parseError a = case a of
TokenEOF -> throwError $ "Parse error: no tokens left to parse." TokenEOF -> do
p <- gets fst
throwError $ show p ++ ": Parse error: unexpected end of file."
Token t s p -> throwError $ show p ++ ": Parse error: unexpected token '" Token t s p -> throwError $ show p ++ ": Parse error: unexpected token '"
++ s ++ "' (" ++ show t ++ ")." ++ s ++ "' (" ++ show t ++ ")."
...@@ -1474,4 +1485,9 @@ addMIAttr :: Attr -> ModuleItem -> ModuleItem ...@@ -1474,4 +1485,9 @@ addMIAttr :: Attr -> ModuleItem -> ModuleItem
addMIAttr _ (item @ (MIPackageItem (Decl CommentDecl{}))) = item addMIAttr _ (item @ (MIPackageItem (Decl CommentDecl{}))) = item
addMIAttr attr item = MIAttr attr item addMIAttr attr item = MIAttr attr item
missingToken :: String -> ParseState a
missingToken expected = do
p <- gets fst
throwError $ show p ++ ": Parse error: missing expected `" ++ expected ++ "`"
} }
// pattern: missing expected `end`
module top;
initial
begin
$display("FOO");
$display("BAR");
endmodule
// pattern: missing expected `endfunction`
module top;
function automatic foo;
input inp;
foo = inp;
endmodule
// pattern: missing expected `endgenerate`
module top;
generate
initial $display("FOO");
endmodule
// pattern: missing expected `endinterface`
interface foo;
// pattern: missing expected `endinterface`
interface foo;
module bar;
endmodule
// pattern: missing expected `endmodule`
module foo;
// pattern: missing expected `endmodule`
module foo;
module bar;
endmodule
// pattern: missing expected `endpackage`
package foo;
// pattern: missing expected `endtask`
module top;
task automatic foo;
input inp;
endmodule
// pattern: missing expected `join`
module top;
initial
fork
$display("FOO");
$display("BAR");
endmodule
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment