1+ package tools.samt.ls
2+
3+ import tools.samt.common.DiagnosticContext
4+ import tools.samt.common.DiagnosticException
5+ import tools.samt.common.SourceFile
6+ import tools.samt.lexer.Lexer
7+ import tools.samt.lexer.Token
8+ import tools.samt.parser.FileNode
9+ import tools.samt.parser.Parser
10+
11+ class FileInfo (
12+ val diagnosticContext : DiagnosticContext ,
13+ val sourceFile : SourceFile ,
14+ val tokens : List <Token >,
15+ val fileNode : FileNode ? = null
16+ )
17+
18+ fun parseFile (sourceFile : SourceFile ): FileInfo {
19+ val diagnosticContext = DiagnosticContext (sourceFile)
20+
21+ val tokens = Lexer .scan(sourceFile.content.reader(), diagnosticContext).toList()
22+
23+ if (diagnosticContext.hasErrors()) {
24+ return FileInfo (diagnosticContext, sourceFile, tokens)
25+ }
26+
27+ val fileNode = try {
28+ Parser .parse(sourceFile, tokens.asSequence(), diagnosticContext)
29+ } catch (e: DiagnosticException ) {
30+ // error message is added to the diagnostic console, so it can be ignored here
31+ return FileInfo (diagnosticContext, sourceFile, tokens)
32+ }
33+
34+ return FileInfo (diagnosticContext, sourceFile, tokens, fileNode)
35+ }
0 commit comments