Skip to content

Commit

Permalink
[cocas] Rename exceptions
Browse files Browse the repository at this point in the history
  • Loading branch information
cjvth committed Feb 8, 2024
1 parent d9defbc commit a9bc14c
Show file tree
Hide file tree
Showing 21 changed files with 72 additions and 72 deletions.
2 changes: 1 addition & 1 deletion cocas/assembler/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from .assembler import assemble_files, assemble_module
from .ast_builder import build_ast
from .exceptions import CdmAssemblerException
from .exceptions import AssemblerException
from .macro_processor import process_macros
from .object_generator import generate_object_module
from .targets import import_target, list_assembler_targets
10 changes: 5 additions & 5 deletions cocas/assembler/ast_builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
UntilLoopNode,
WhileLoopNode,
)
from .exceptions import AntlrErrorListener, CdmAssemblerException, CdmExceptionTag
from .exceptions import AntlrErrorListener, AsmExceptionTag, AssemblerException
from .generated import AsmLexer, AsmParser, AsmParserVisitor


Expand Down Expand Up @@ -126,7 +126,7 @@ def visitConnective_condition(self, ctx: AsmParser.Connective_conditionContext):
cond = self.visitCondition(ctx.condition())
cond.conjunction = ctx.conjunction().getText()
if cond.conjunction != 'and' and cond.conjunction != 'or':
raise CdmAssemblerException(CdmExceptionTag.ASM, self.source_path, ctx.start.line - self.line_offset,
raise AssemblerException(AsmExceptionTag.ASM, self.source_path, ctx.start.line - self.line_offset,
'Expected "and" or "or" in compound condition')
return cond

Expand Down Expand Up @@ -218,7 +218,7 @@ def visitStandaloneLabel(self, ctx: AsmParser.StandaloneLabelContext) -> LabelDe
label_decl = self.visitLabel_declaration(ctx.label_declaration())
label_decl.external = ctx.Ext() is not None
if label_decl.entry and label_decl.external:
raise CdmAssemblerException(CdmExceptionTag.ASM, self.source_path, ctx.start.line - self.line_offset,
raise AssemblerException(AsmExceptionTag.ASM, self.source_path, ctx.start.line - self.line_offset,
f'Label {label_decl.label.name} cannot be both external and entry')
return label_decl

Expand Down Expand Up @@ -257,12 +257,12 @@ def build_ast(input_stream: InputStream, filepath: Path):
str_path = filepath.absolute().as_posix()
lexer = AsmLexer(input_stream)
lexer.removeErrorListeners()
lexer.addErrorListener(AntlrErrorListener(CdmExceptionTag.ASM, str_path))
lexer.addErrorListener(AntlrErrorListener(AsmExceptionTag.ASM, str_path))
token_stream = CommonTokenStream(lexer)
token_stream.fill()
parser = AsmParser(token_stream)
parser.removeErrorListeners()
parser.addErrorListener(AntlrErrorListener(CdmExceptionTag.ASM, str_path))
parser.addErrorListener(AntlrErrorListener(AsmExceptionTag.ASM, str_path))
cst = parser.program()
bav = BuildAstVisitor(str_path)
result = bav.visit(cst)
Expand Down
6 changes: 3 additions & 3 deletions cocas/assembler/code_block.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
UntilLoopNode,
WhileLoopNode,
)
from .exceptions import CdmAssemblerException, CdmExceptionTag, CdmTempException
from .exceptions import AsmExceptionTag, AssemblerException, CdmTempException
from .targets import ICodeSegment, TargetInstructionsInterface


Expand All @@ -39,8 +39,8 @@ def __init__(self, address: int, lines: list, target_instructions: Type[TargetIn
target_instructions.finish(temp_storage)
except CdmTempException as e:
# if it isn't ok, must be at least one line
raise CdmAssemblerException(CdmExceptionTag.ASM, lines[-1].location.file,
lines[-1].location.line, e.message)
raise AssemblerException(AsmExceptionTag.ASM, lines[-1].location.file,
lines[-1].location.line, e.message)

def append_label(self, label_name):
self.labels[label_name] = self.address + self.size
Expand Down
8 changes: 4 additions & 4 deletions cocas/assembler/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from cocas.assembler.generated import AsmParser


class CdmExceptionTag(Enum):
class AsmExceptionTag(Enum):
MACRO = "Macro"
ASM = "Assembler"

Expand All @@ -19,8 +19,8 @@ def __init__(self, message: str):
self.message = message


class CdmAssemblerException(Exception):
def __init__(self, tag: CdmExceptionTag, file: str, line: int, description: str):
class AssemblerException(Exception):
def __init__(self, tag: AsmExceptionTag, file: str, line: int, description: str):
self.tag = tag
self.file = file
self.line = line
Expand All @@ -36,4 +36,4 @@ def syntaxError(self, recognizer, offending_symbol, line, column, msg, e):
if isinstance(recognizer, AsmParser):
line = line - recognizer.current_offset
self.file = recognizer.current_file
raise CdmAssemblerException(self.tag, self.file, line, msg)
raise AssemblerException(self.tag, self.file, line, msg)
10 changes: 5 additions & 5 deletions cocas/assembler/macro_processor.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

from cocas.object_module import CodeLocation

from .exceptions import AntlrErrorListener, CdmAssemblerException, CdmExceptionTag, CdmTempException
from .exceptions import AntlrErrorListener, AsmExceptionTag, AssemblerException, CdmTempException
from .generated import MacroLexer, MacroParser, MacroVisitor


Expand Down Expand Up @@ -182,7 +182,7 @@ def visitMlb(self, ctx: MacroParser.MlbContext):
try:
self.add_macro(self.visitMlb_macro(child))
except CdmTempException as e:
raise CdmAssemblerException(CdmExceptionTag.MACRO, self.filepath, child.start.line, e.message)
raise AssemblerException(AsmExceptionTag.MACRO, self.filepath, child.start.line, e.message)
return self.macros

def visitProgram(self, ctx: MacroParser.ProgramContext):
Expand All @@ -205,7 +205,7 @@ def visitProgram(self, ctx: MacroParser.ProgramContext):
self.rewriter.insertBeforeToken(child.start, expanded_text)
self.rewriter.delete(self.rewriter.DEFAULT_PROGRAM_NAME, child.start, child.stop)
except CdmTempException as e:
raise CdmAssemblerException(CdmExceptionTag.MACRO, self.filepath, child.start.line, e.message)
raise AssemblerException(AsmExceptionTag.MACRO, self.filepath, child.start.line, e.message)

def visitMacro(self, ctx: MacroParser.MacroContext):
header = ctx.macro_header()
Expand Down Expand Up @@ -305,12 +305,12 @@ def process_macros(input_stream: InputStream, library_macros, filepath: Path):
str_path = filepath.absolute().as_posix()
lexer = MacroLexer(input_stream)
lexer.removeErrorListeners()
lexer.addErrorListener(AntlrErrorListener(CdmExceptionTag.MACRO, str_path))
lexer.addErrorListener(AntlrErrorListener(AsmExceptionTag.MACRO, str_path))
token_stream = CommonTokenStream(lexer)

parser = MacroParser(token_stream)
parser.removeErrorListeners()
parser.addErrorListener(AntlrErrorListener(CdmExceptionTag.MACRO, str_path))
parser.addErrorListener(AntlrErrorListener(AsmExceptionTag.MACRO, str_path))
cst = parser.program()
rewriter = TokenStreamRewriter(token_stream)
emv = ExpandMacrosVisitor(rewriter, library_macros, str_path)
Expand Down
4 changes: 2 additions & 2 deletions cocas/assembler/object_generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@

from .ast_nodes import InstructionNode, LabelDeclarationNode, ProgramNode, TemplateSectionNode
from .code_block import Section
from .exceptions import CdmExceptionTag
from .exceptions import AsmExceptionTag
from .targets import TargetInstructionsInterface

TAG = CdmExceptionTag.ASM
TAG = AsmExceptionTag.ASM


@dataclass
Expand Down
4 changes: 2 additions & 2 deletions cocas/assembler/targets/abstract_code_segments.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

from cocas.object_module import CodeLocation

from ..exceptions import CdmAssemblerException, CdmExceptionTag
from ..exceptions import AsmExceptionTag, AssemblerException

if TYPE_CHECKING:
from cocas.object_module import ObjectSectionRecord
Expand Down Expand Up @@ -98,4 +98,4 @@ def fill(self, object_record: "ObjectSectionRecord", section: "Section", labels:


def _error(segment: ICodeSegment, message: str):
raise CdmAssemblerException(CdmExceptionTag.ASM, segment.location.file, segment.location.line, message)
raise AssemblerException(AsmExceptionTag.ASM, segment.location.file, segment.location.line, message)
4 changes: 2 additions & 2 deletions cocas/assembler/targets/cdm16/code_segments.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
from cocas.object_module import CodeLocation, ExternalEntry

from ...ast_nodes import LabelNode, RegisterNode, RelocatableExpressionNode, TemplateFieldNode
from ...exceptions import CdmAssemblerException, CdmExceptionTag
from ...exceptions import AsmExceptionTag, AssemblerException
from .. import IAlignedSegment, IAlignmentPaddingSegment, ICodeSegment, IVaryingLengthSegment

if TYPE_CHECKING:
Expand All @@ -22,7 +22,7 @@ def pack(fmt, *args):


def _error(segment: ICodeSegment, message: str):
raise CdmAssemblerException(CdmExceptionTag.ASM, segment.location.file, segment.location.line, message)
raise AssemblerException(AsmExceptionTag.ASM, segment.location.file, segment.location.line, message)


class CodeSegment(ICodeSegment, ABC):
Expand Down
8 changes: 4 additions & 4 deletions cocas/assembler/targets/cdm16/target_instructions.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from typing import Callable, Union, get_args, get_origin

from ...ast_nodes import InstructionNode, LabelNode, RegisterNode, RelocatableExpressionNode
from ...exceptions import CdmAssemblerException, CdmExceptionTag, CdmTempException
from ...exceptions import AsmExceptionTag, AssemblerException, CdmTempException
from .. import ICodeSegment, TargetInstructionsInterface
from .code_segments import (
AlignmentPaddingSegment,
Expand Down Expand Up @@ -56,10 +56,10 @@ def assemble_instruction(line: InstructionNode, temp_storage: dict) -> list[ICod
return h.handler(line, temp_storage, h.instructions[line.mnemonic])
if line.mnemonic.startswith('b'):
return TargetInstructions.branch(line)
raise CdmAssemblerException(CdmExceptionTag.ASM, line.location.file, line.location.line,
raise AssemblerException(AsmExceptionTag.ASM, line.location.file, line.location.line,
f'Unknown instruction "{line.mnemonic}"')
except CdmTempException as e:
raise CdmAssemblerException(CdmExceptionTag.ASM, line.location.file, line.location.line, e.message)
raise AssemblerException(AsmExceptionTag.ASM, line.location.file, line.location.line, e.message)

@staticmethod
def finish(temp_storage: dict):
Expand Down Expand Up @@ -184,7 +184,7 @@ def branch(line: InstructionNode, inverse=False) -> list[ICodeSegment]:
branch_code = pair.inv_code if not inverse else pair.code
break
else:
raise CdmAssemblerException(CdmExceptionTag.ASM, line.location.file, line.location.line,
raise AssemblerException(AsmExceptionTag.ASM, line.location.file, line.location.line,
f'Invalid branch condition: {cond}')
assert_count_args(line.arguments, RelocatableExpressionNode)
return [Branch(line.location, branch_code, line.arguments[0])]
Expand Down
4 changes: 2 additions & 2 deletions cocas/assembler/targets/cdm8/code_segments.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from cocas.object_module import CodeLocation, ExternalEntry

from ...ast_nodes import LabelNode, RelocatableExpressionNode, TemplateFieldNode
from ...exceptions import CdmAssemblerException, CdmExceptionTag
from ...exceptions import AsmExceptionTag, AssemblerException
from .. import IAlignmentPaddingSegment, ICodeSegment

if TYPE_CHECKING:
Expand All @@ -14,7 +14,7 @@


def _error(segment: ICodeSegment, message: str):
raise CdmAssemblerException(CdmExceptionTag.ASM, segment.location.file, segment.location.line, message)
raise AssemblerException(AsmExceptionTag.ASM, segment.location.file, segment.location.line, message)


# noinspection DuplicatedCode
Expand Down
8 changes: 4 additions & 4 deletions cocas/assembler/targets/cdm8/target_instructions.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from bitstruct import pack

from ...ast_nodes import InstructionNode, LabelNode, RegisterNode, RelocatableExpressionNode
from ...exceptions import CdmAssemblerException, CdmExceptionTag, CdmTempException
from ...exceptions import AsmExceptionTag, AssemblerException, CdmTempException
from .. import ICodeSegment, TargetInstructionsInterface
from .code_segments import AlignmentPaddingSegment, BytesSegment, ExpressionSegment

Expand Down Expand Up @@ -48,10 +48,10 @@ def assemble_instruction(line: InstructionNode, temp_storage: dict) -> list[ICod
return h.handler(line, temp_storage, h.instructions[line.mnemonic])
if line.mnemonic.startswith('b'):
return TargetInstructions.branch(line)
raise CdmAssemblerException(CdmExceptionTag.ASM, line.location.file, line.location.line,
raise AssemblerException(AsmExceptionTag.ASM, line.location.file, line.location.line,
f'Unknown instruction "{line.mnemonic}"')
except CdmTempException as e:
raise CdmAssemblerException(CdmExceptionTag.ASM, line.location.file, line.location.line, e.message)
raise AssemblerException(AsmExceptionTag.ASM, line.location.file, line.location.line, e.message)

@staticmethod
def finish(temp_storage: dict):
Expand Down Expand Up @@ -155,7 +155,7 @@ def branch(line: InstructionNode, inverse=False) -> list[ICodeSegment]:
branch_code = pair.inv_code if not inverse else pair.code
break
else:
raise CdmAssemblerException(CdmExceptionTag.ASM, line.location.file, line.location.line,
raise AssemblerException(AsmExceptionTag.ASM, line.location.file, line.location.line,
f'Invalid branch condition: {cond}')
assert_count_args(line.arguments, RelocatableExpressionNode)
return [BytesSegment(pack('u4u4', 0xE, branch_code), line.location),
Expand Down
10 changes: 5 additions & 5 deletions cocas/assembler/targets/cdm8e/code_segments.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@
from cocas.object_module import CodeLocation, ExternalEntry

from ...ast_nodes import LabelNode, RelocatableExpressionNode, TemplateFieldNode
from ...exceptions import CdmAssemblerException, CdmExceptionTag
from ...exceptions import AsmExceptionTag, AssemblerException
from .. import ICodeSegment, IVaryingLengthSegment
from .simple_instructions import simple_instructions

TAG = CdmExceptionTag.ASM
TAG = AsmExceptionTag.ASM

if TYPE_CHECKING:
from cocas.object_module import ObjectSectionRecord
Expand Down Expand Up @@ -178,10 +178,10 @@ def update_varying_length(self, pos: int, section: "Section", labels: dict[str,
if label_name in labels:
labels[label_name] += shift_length
return shift_length
except CdmAssemblerException as e:
except AssemblerException as e:
raise e
except Exception as e:
raise CdmAssemblerException(TAG, self.location.file, self.location.line, str(e))
raise AssemblerException(TAG, self.location.file, self.location.line, str(e))

def fill(self, object_record: "ObjectSectionRecord", section: "Section", labels: dict[str, int],
templates: dict[str, dict[str, int]]):
Expand All @@ -201,7 +201,7 @@ def fill(self, object_record: "ObjectSectionRecord", section: "Section", labels:


def _error(segment: ICodeSegment, message: str):
raise CdmAssemblerException(TAG, segment.location.file, segment.location.line, message)
raise AssemblerException(TAG, segment.location.file, segment.location.line, message)


def eval_rel_expr_seg(seg: RelocatableExpressionSegment, s: "Section",
Expand Down
4 changes: 2 additions & 2 deletions cocas/assembler/targets/cdm8e/target_instructions.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import bitstruct

from ...ast_nodes import InstructionNode, LabelNode, RegisterNode, RelocatableExpressionNode
from ...exceptions import CdmAssemblerException, CdmExceptionTag, CdmTempException
from ...exceptions import AsmExceptionTag, AssemblerException, CdmTempException
from .. import ICodeSegment, TargetInstructionsInterface
from .code_segments import (
BytesSegment,
Expand Down Expand Up @@ -53,7 +53,7 @@ def assemble_instruction(line: InstructionNode, temp_storage) \
segment.location = line.location
return segments
except CdmTempException as e:
raise CdmAssemblerException(CdmExceptionTag.ASM, line.location.file, line.location.line, e.message)
raise AssemblerException(AsmExceptionTag.ASM, line.location.file, line.location.line, e.message)

@staticmethod
def finish(temp_storage: dict):
Expand Down
12 changes: 6 additions & 6 deletions cocas/exception_handlers.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
from colorama import Fore, Style

from cocas.assembler import CdmAssemblerException
from cocas.linker import CdmLinkException
from cocas.object_file import CdmObjectFileException
from cocas.assembler import AssemblerException
from cocas.linker import LinkerException
from cocas.object_file import ObjectFileException


def log_exception(tag: str, message: str):
Expand All @@ -18,18 +18,18 @@ def log_os_error(e: OSError):
exit(1)


def log_asm_exception(e: CdmAssemblerException):
def log_asm_exception(e: AssemblerException):
print(f'[{e.tag.value}] {Fore.RED}ERROR{Fore.RESET} at line {Style.BRIGHT}{e.line}{Style.RESET_ALL} of '
f'{Style.BRIGHT}{e.file}{Style.RESET_ALL}')
print(f'{e.description}')


def log_object_file_exception(e: CdmObjectFileException):
def log_object_file_exception(e: ObjectFileException):
print(f'[Object file] {Fore.RED}ERROR{Fore.RESET} at line {Style.BRIGHT}{e.line}{Style.RESET_ALL} of '
f'{Style.BRIGHT}{e.file}{Style.RESET_ALL}')
print(f'{e.description}')


def log_link_exception(_: CdmLinkException):
def log_link_exception(_: LinkerException):
# todo: find this code from previous revisions
pass
2 changes: 1 addition & 1 deletion cocas/linker/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from .debug_export import debug_export, write_debug_export
from .exceptions import CdmLinkException
from .exceptions import LinkerException
from .image import write_image
from .linker import link
2 changes: 1 addition & 1 deletion cocas/linker/exceptions.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
class CdmLinkException(Exception):
class LinkerException(Exception):
def __init__(self, message: str):
self.message = message
Loading

0 comments on commit a9bc14c

Please sign in to comment.