Coverage for src/m6rclib/metaphor_token.py: 100%

1# Copyright 2024 M6R Ltd.

2#

3# Licensed under the Apache License, Version 2.0 (the "License");

4# you may not use this file except in compliance with the License.

5# You may obtain a copy of the License at

6#

7# http://www.apache.org/licenses/LICENSE-2.0

8#

9# Unless required by applicable law or agreed to in writing, software

10# distributed under the License is distributed on an "AS IS" BASIS,

11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.

12# See the License for the specific language governing permissions and

13# limitations under the License.

14

15 from dataclasses import dataclass

16 from enum import IntEnum

17

18 class TokenType(IntEnum):

19 """

20 Enum-like class representing different types of tokens in the source file.

21 """

22 NONE: int = 0

23 INDENT: int = 1

24 OUTDENT: int = 2

25 INCLUDE: int = 3

26 EMBED: int = 4

27 KEYWORD_TEXT: int = 5

28 TEXT: int = 6

29 ACTION: int = 7

30 CONTEXT: int = 8

31 ROLE: int = 9

32 BAD_INDENT: int = 10

33 BAD_OUTDENT: int = 11

34 TAB: int = 12

35 END_OF_FILE: int = 13

36

37

38@dataclass(frozen=True)

39 class Token:

40 """

41 Represents a token in the input stream.

42

43 Attributes:

44 type (TokenType): The type of the token (e.g., TEXT, ACTION).

45 value (str): The actual string value of the token.

46 input (str): The entire line of input where the token appears.

47 filename (str): The file where the token was read from.

48 line (int): The line number in the file where the token is located.

49 column (int): The column number where the token starts.

50 """

51 type: TokenType

52 value: str

53 input: str

54 filename: str

55 line: int

56 column: int

57

58 def __str__(self) -> str:

59 return f"Token(type=TokenType.{self.type.name}, value='{self.value}', " \

60 f"line={self.line}, column={self.column})"