Advertisement
Guest User

Untitled

a guest
Jun 25th, 2019
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.17 KB | None | 0 0
  1. class
  2. {
  3. New(fileName)
  4. {
  5.  
  6. .file = File(fileName,'r')
  7.  
  8. .currentLine = 0
  9. .wordsInLine = #[]
  10. .positionInLine = 0
  11. .currentWord = ""
  12. .token = ""
  13.  
  14. .tokensTypes = #["KEYWORD","SYMBOL","IDENTIFIER",
  15. "INT_CONST","STRING_CONST","ERROR"]
  16. .comments = "(^//.*)|\*"
  17. .symbol = "(.*)(\\{|\\}|\\(|\\)|\\[|\\]|\\.|\\,|;|\\+|-|\\*|/|&|\\||<|>|=|~)(.*)"
  18. .keyword = "^(class|constructor|function|method|field|static|var|int|
  19. char|boolean|void|true|false|null|this|let|do|if|else|while|return)$"
  20. .idetifier = "^[^\\d\\W]\\w*\\Z"
  21. .int = "^[\d]+$"
  22. .str = "^[\"][\w]"
  23. .current = #["",""]
  24. .tokens = Object()
  25. }
  26. Token()
  27. {
  28. return .token
  29. }
  30. TypeToEnum(str)
  31. {
  32. for (i = 0; i < .tokensTypes.Size(); i++)
  33. if .tokensTypes[i] is str
  34. return i
  35. return -1
  36. }
  37.  
  38. SetCurrentLine()
  39. {
  40. do{
  41. .currentLine = .file.Readline()
  42. .currentLine = .currentLine.LeftTrim()
  43. }while .currentLine.Blank?() or .isComment()
  44. if .currentLine =~ "\""
  45. .replaceSpaces()
  46. if .currentLine =~ .symbol
  47. .addSpaceToSymbol()
  48. Print(.currentLine)
  49. .wordsInLine = .currentLine.Trim().Tr(" \t", " ").Split(" ")
  50. }
  51.  
  52. replaceSpaces()
  53. {
  54. inString = false
  55. for (i = 0; i < .currentLine.Size(); ++i)
  56. {
  57. if .currentLine[i] is "\""
  58. inString = not inString
  59. if .currentLine[i] is " " and inString
  60. .currentLine = .currentLine.ReplaceSubstr(i,1,"_")
  61. }
  62. }
  63.  
  64. addSpaceToSymbol()
  65. {
  66. symbols = #["[","{","}","(",")","[","]",".",",",";","+",
  67. "-","*","&","|","<",">","=","~","]"]
  68. for sym in symbols
  69. if .currentLine.Has?(sym)
  70. {
  71. if .special(sym)
  72. .currentLine = .currentLine.Replace("\\" $ sym," "$ "\\" $ sym $ " ")
  73. else
  74. .currentLine = .currentLine.Replace(sym," "$ sym $ " ")
  75. }
  76. }
  77.  
  78. special(sym)
  79. {
  80. specials = #["[","]","(",")","."]
  81. for s in specials
  82. {
  83. if sym is s
  84. return true
  85. }
  86. return false
  87. }
  88.  
  89. isComment()
  90. {
  91. return .currentLine =~ .comments
  92. }
  93. CommentsRemover(code)
  94. {
  95. lines = code.Lines()
  96. result = ""
  97. for (i = 0; i < lines.Size(); ++i){
  98. if(lines[i] is "")
  99. {
  100. continue;
  101. }
  102. line = lines[i].Split(" ")
  103. line = line.LeftTrim()
  104. if line[0] is '/' and line[1] is '/'
  105. {
  106. continue;
  107. }
  108. result $= lines[i]
  109. }
  110. return result
  111. }
  112.  
  113. hasMoreTokens()
  114. {
  115. x = .file.Tell()
  116. i = .file.Readline()
  117. if (i is false and .positionInLine >= .wordsInLine.Size()){
  118. .file.Close()
  119. return false
  120. }
  121. .file.Seek(x,"set")
  122. return true
  123. }
  124. Advance()
  125. {
  126. if (not .hasMoreTokens()) { return; }
  127.  
  128. if (.positionInLine >= .wordsInLine.Size()) {
  129. .SetCurrentLine()
  130. .positionInLine = 0
  131. }
  132. .token = .wordsInLine[.positionInLine++];
  133. }
  134.  
  135. TokenType()
  136. {
  137. if (.token =~ .keyword) { return .TypeToEnum("KEYWORD") }
  138. else if (.token =~ .symbol) { return .TypeToEnum("SYMBOL") }
  139. else if (.token =~ .idetifier) { return .TypeToEnum("IDENTIFIER") }
  140. else if (.token =~ .int) { return .TypeToEnum("INT_CONST") }
  141. else if (.token =~ .str) { return .TypeToEnum("STRING_CONST") }
  142. else {
  143. Print(.token $ " ERR")
  144. return .TypeToEnum("ERROR") }
  145. }
  146. KeyWord()
  147. {
  148. return .current[0].Upper()
  149. }
  150. Symbol()
  151. {
  152. if (.token is "<") { return "&lt;" }
  153. else if (.token is ">") { return "&gt;";}
  154. else if (.token is "&") { return "&amp;" }
  155. else return .token
  156. }
  157.  
  158.  
  159. StringVal()
  160. {
  161. .token = .token.Replace("_"," ")
  162. return .token
  163. }
  164. Xml()
  165. {
  166. result = "<tokens>\n"
  167. while (.hasMoreTokens()) {
  168. .Advance();
  169. type = .TokenType()
  170. switch type {
  171. case .TypeToEnum("KEYWORD"):
  172. result $= Xml("keyword"," " $ .token $ " ") $ "\n"
  173. case .TypeToEnum("SYMBOL"):
  174. result $= Xml("symbol"," " $ .Symbol() $ " ") $ "\n"
  175. case .TypeToEnum("IDENTIFIER"):
  176. result $= Xml("identifier", " " $ .token $ " ") $ "\n"
  177. case .TypeToEnum("INT_CONST"):
  178. result $= Xml("integerConstant", " " $ .token $ " ") $ "\n"
  179. case .TypeToEnum("STRING_CONST"):
  180. result $= Xml("stringConstant", " " $ .StringVal() $ " ") $ "\n"
  181. default:
  182. result $= Xml("error"," ERROR ") $ "\n"
  183. }
  184. }
  185. return result $ "</tokens>"
  186. }
  187. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement