Advertisement
Guest User

Untitled

a guest
Aug 20th, 2017
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.35 KB | None | 0 0
  1. package messageparser;
  2.  
  3. import botelements.*;
  4. import botelements.messageelements.*;
  5. import java.util.ArrayList;
  6.  
  7. %%
  8. %class MessageScanner
  9.  
  10. %unicode
  11. %line
  12. %column
  13. %public
  14. %type BotMessage
  15. %{
  16.     private BotMessage message = new BotMessage();
  17.     private MessagePart currentPart= new MessagePart();
  18.     private ResourceMessagePart currentResource;
  19.     private ProgramMessagePart currentProgram;
  20.    
  21.     private int resourceRetrunState;
  22.    
  23.     public int getLine() {
  24.         return yyline + 1;
  25.     }
  26.    
  27.     public int getColumn() {
  28.         return yycolumn + 1;
  29.     }
  30. %}
  31.  
  32. EOL = \r|\n|\r\n
  33. WhiteSpace = {EOL}|\t|" "
  34.  
  35. RawMessage = [^]
  36.  
  37. BotMentionTag = [\@][Bb]ot
  38. SenderMentionTag = [\@][Ss]ender
  39. MentionTag = {BotMentionTag}|{SenderMentionTag}
  40.  
  41. IdentifierDigitAndLetter = [a-zA-Z0-9]
  42. IdentifierRepeatingBlock = [_]+{IdentifierDigitAndLetter}+
  43. Identifier = ([a-zA-Z]+{IdentifierDigitAndLetter}*){IdentifierRepeatingBlock}*| {IdentifierRepeatingBlock}+
  44.  
  45. DecimalDigit = [0-9]
  46. DecimalIntegerConstant = [+-]?{DecimalDigit}+
  47. PositiveDecimalIntegerConstant = [+]?{DecimalDigit}+
  48.  
  49. %state RESOURCE_STATE
  50. %state RESOURCE_ID_STATE
  51. %state RESOURCE_COUNT_STATE
  52.  
  53. %state PROGRAM_CLASS_STATE
  54. %state PROGRAM_FUNCTION_STATE
  55. %state PROGRAM_ARGUMENT_STATE
  56.  
  57.  
  58. %%
  59.  
  60. <YYINITIAL> {
  61.     {MentionTag} {
  62.         message.messageParts.add(currentPart);
  63.         currentPart = new MessagePart();
  64.         currentPart.rawMessage = yytext();
  65.         currentPart.type = MessagePart.MessagePartType.MentionTag;
  66.         message.messageParts.add(currentPart);
  67.         currentPart = new MessagePart();
  68.     }
  69.     "\\<" {
  70.         currentPart.rawMessage = currentPart.rawMessage + "<";
  71.     }
  72.     "\\~" {
  73.         currentPart.rawMessage = currentPart.rawMessage + "~";
  74.     }
  75.     "\\{" {
  76.         currentPart.rawMessage = currentPart.rawMessage + "{";
  77.     }
  78.     [\<] {
  79.         message.messageParts.add(currentPart);
  80.         currentPart = new MessagePart();
  81.         currentPart.resource = new ResourceMessagePart();
  82.         currentPart.type = MessagePart.MessagePartType.Resource;
  83.         currentResource = currentPart.resource;
  84.         resourceRetrunState = YYINITIAL;
  85.         yybegin(RESOURCE_STATE);
  86.         //further process on the currentResource
  87.         message.messageParts.add(currentPart);
  88.         currentPart = new MessagePart();
  89.     }
  90.     [\~] {
  91.         message.messageParts.add(currentPart);
  92.         currentPart = new MessagePart();
  93.         currentPart.program = new ProgramMessagePart();
  94.         currentProgram = currentPart.program;
  95.         currentPart.type = MessagePart.MessagePartType.Program;
  96.         yybegin(PROGRAM_CLASS_STATE);
  97.         //further process on the currentProgram
  98.         message.messageParts.add(currentPart);
  99.         currentPart = new MessagePart();
  100.     }
  101.     {RawMessage} {
  102.         currentPart.rawMessage = currentPart.rawMessage + yytext();
  103.     }
  104.     <<EOF>> {
  105.         return message;
  106.     }
  107. }
  108.  
  109. <RESOURCE_STATE> {
  110.     "\\>" {
  111.         currentResource.name = currentResource.name + ">";
  112.     }
  113.     ">{" {
  114.         yybegin(RESOURCE_ID_STATE);
  115.     }
  116.     [\>] {
  117.         yybegin(resourceRetrunState);
  118.     }
  119.     [^] {
  120.         currentResource.name = currentResource.name + yytext();
  121.     }
  122.     <<EOF>> {
  123.         yybegin(resourceRetrunState);
  124.     }
  125. }
  126.  
  127. <RESOURCE_ID_STATE> {
  128.     "}{" {
  129.         yybegin(RESOURCE_COUNT_STATE);
  130.     }
  131.     [\}] {
  132.         yybegin(resourceRetrunState);
  133.     }
  134.     <<EOF>> {
  135.         yybegin(resourceRetrunState);
  136.     }
  137.     {DecimalIntegerConstant} {
  138.         currentResource.id = Integer.parseInt(yytext());
  139.     }
  140.     [^] {
  141.     }
  142. }
  143.  
  144. <RESOURCE_COUNT_STATE> {
  145.     [\}] {
  146.         yybegin(resourceRetrunState);
  147.     }
  148.     <<EOF>> {
  149.         yybegin(resourceRetrunState);
  150.     }
  151.     {PositiveDecimalIntegerConstant} {
  152.         currentResource.count = Integer.parseInt(yytext());
  153.     }
  154.     [\*] {
  155.         currentResource.count = -1;
  156.     }
  157.     [\+] {
  158.         currentResource.count = -2;
  159.     }
  160.     [^] {
  161.     }
  162. }
  163.  
  164.  
  165.  
  166. <PROGRAM_CLASS_STATE> {
  167.     {WhiteSpace} {
  168.     }
  169.     {Identifier} {
  170.         currentProgram.className = yytext();
  171.     }
  172.     [\.] {
  173.         yybegin(PROGRAM_FUNCTION_STATE);
  174.     }
  175.     [^] {
  176.         yybegin(YYINITIAL);
  177.     }
  178.     <<EOF>> {
  179.         yybegin(YYINITIAL);
  180.     }
  181. }
  182. <PROGRAM_FUNCTION_STATE> {
  183.     {WhiteSpace} {
  184.     }
  185.     {Identifier} {
  186.         currentProgram.functionName = yytext();
  187.     }
  188.     [\(] {
  189.         yybegin(PROGRAM_ARGUMENT_STATE);
  190.     }
  191.     [^] {
  192.         yybegin(YYINITIAL);
  193.     }
  194.     <<EOF>> {
  195.         yybegin(YYINITIAL);
  196.     }
  197. }
  198. <PROGRAM_ARGUMENT_STATE> {
  199.     {WhiteSpace} {
  200.     }
  201.     [\<] {
  202.         currentResource = new ResourceMessagePart();
  203.         currentProgram.arguments.add(currentResource);
  204.         resourceRetrunState = PROGRAM_ARGUMENT_STATE;
  205.         yybegin(RESOURCE_STATE);
  206.     }
  207.     [\)] {
  208.         yybegin(YYINITIAL);
  209.     }
  210.     [\,] {
  211.     }
  212.     [^] {
  213.         yybegin(YYINITIAL);
  214.     }
  215.     <<EOF>> {
  216.         yybegin(YYINITIAL);
  217.     }
  218. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement