SHARE
TWEET

Untitled

a guest Oct 15th, 2019 113 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import strutils, parseutils, pegs, lists
  2. import npeg
  3.  
  4. type
  5.   Section = ref object
  6.     Visible: bool
  7.     Corrupted: bool
  8.     Identified: bool
  9.     ShaperItem: bool
  10.     ElderItem: bool
  11.     FracturedItem: bool
  12.     SynthesisedItem: bool
  13.     AnyEnchantment: bool
  14.     DisableDropSound: bool
  15.     BlightedMap: bool
  16.     ElderMap: bool
  17.     ShapedMap: bool
  18.     BaseType: seq[string]
  19.     Class: seq[string]
  20.     HasExplicitMod: seq[string]
  21.     Prophecy: seq[string]
  22.     SocketGroup: string
  23.     SetFontSize: int
  24.     Rarity: OpAndRarity
  25.     Width: OpAndInt
  26.     Height: OpAndInt
  27.     Quality: OpAndInt
  28.     Sockets: OpAndInt
  29.     MapTier: OpAndInt
  30.     GemLevel: OpAndInt
  31.     StackSize: OpAndInt
  32.     ItemLevel: OpAndInt
  33.     DropLevel: OpAndInt
  34.     LinkedSockets: OpAndInt
  35.     SetTextColor: Color
  36.     SetBorderColor: Color
  37.     SetBackgroundColor: Color
  38.     PlayAlertSound: Sound
  39.     PlayAlertSoundPositional: Sound
  40.     PlayEffect: Effect
  41.     MinimapIcon: Icon
  42.  
  43.   # Color = ref object
  44.   #   r, g, b, a: int
  45.  
  46.   RarityEnum = enum
  47.     normal = (0, "Normal")
  48.     magic = (1, "Magic")
  49.     rare = (2, "Rare")
  50.     unique = (3,"Unique")
  51.  
  52. #  ">=" | ">" | "<=" | "<" | "="
  53.   OperatorEnum = enum
  54.     equals = (0, "=")
  55.     greaterOrEqual = (1, ">=")
  56.     greaterThan = (2, ">")
  57.     lessOrEqual = (3, "<=")
  58.     lessThan = (4, "<")
  59.  
  60.   ColorEnum = enum
  61.     red = (0, "Red")
  62.     green = (1, "Green")
  63.     blue = (2, "Blue")
  64.     brown = (3, "Brown")
  65.     white = (4, "White")
  66.     yellow = (5, "Yellow")
  67.  
  68.   ShapeEnum = enum
  69.     circle = (0, "Circle")
  70.     diamond = (1, "Diamond")
  71.     hexagon = (2, "Hexagon")
  72.     square = (3, "Square")
  73.     start = (4, "Star")
  74.     triangle = (5, "Triangle")
  75.  
  76.  
  77.   Color = ref object
  78.     r, g, b, a: int
  79.   Sound = ref object
  80.     id: int
  81.     volume: int
  82.   Effect = ref object
  83.     id: ColorEnum
  84.     temporary: bool
  85.   Icon = ref object
  86.     size: int
  87.     color: ColorEnum
  88.     shape: ShapeEnum
  89.   OpAndInt = ref object
  90.     operator: OperatorEnum
  91.     value: int
  92.   OpAndRarity = ref object
  93.     operator: OperatorEnum
  94.     rarity: RarityEnum
  95.  
  96.   Sections = seq[Section]
  97.  
  98. var sectionsSeq: Sections
  99. var curSection: Section
  100.  
  101. let p = peg("doc"):
  102.   doc <- S * *section * !1
  103.   section <- ShowHide * S * +(field * S)
  104.   ShowHide <- >("Show" | "Hide"):
  105.     curSection = Section()
  106.     sectionsSeq.add curSection
  107.     if $1 == "Show": curSection.Visible = true else: curSection.Visible = false
  108.  
  109.   field <-  AnyEnchantment |
  110.             BaseType |
  111.             BlightedMap |
  112.             Class |
  113.             Corrupted |
  114.             DisableDropSound |
  115.             DropLevel |
  116.             ElderItem |
  117.             ElderMap |
  118.             FracturedItem |
  119.             GemLevel |
  120.             HasExplicitMod |
  121.             Height |
  122.             Identified |
  123.             ItemLevel |
  124.             LinkedSockets |
  125.             MapTier |
  126.             MinimapIcon |
  127.             PlayAlertSound |
  128.             PlayAlertSoundPositional |
  129.             PlayEffect |
  130.             Prophecy |
  131.             Quality |
  132.             Rarity |
  133.             SetBackgroundColor |
  134.             SetBorderColor |
  135.             SetFontSize |
  136.             SetTextColor |
  137.             ShapedMap |
  138.             ShaperItem |
  139.             SocketGroup |
  140.             Sockets |
  141.             StackSize |
  142.             SynthesisedItem |
  143.             Width
  144.  
  145.   BaseType <- "BaseType" * S * +(BaseTypeString * S)
  146.   BaseTypeString <- '"' * >*(1-'"') * '"':
  147.   #TODO: split
  148.     curSection.BaseType.add $1
  149.  
  150.   Class <- "Class" * S * +(ClassString * S)
  151.   ClassString <- '"' * >*(1-'"') * '"':
  152.   #TODO: split
  153.     curSection.Class.add $1
  154.  
  155.   HasExplicitMod <- "HasExplicitMod" * S * +(HasExplicitModString * S)
  156.   HasExplicitModString <- '"' * >*(1-'"') * '"':
  157.   #TODO: split
  158.     curSection.HasExplicitMod.add $1
  159.  
  160.   Prophecy <- "Prophecy" * S * +(ProphecyString * S)
  161.   ProphecyString <- '"' * >*(1-'"') * '"':
  162.   #TODO: split
  163.     curSection.Prophecy.add $1
  164.  
  165.   SocketGroup <- "SocketGroup" * S * >+Graph:
  166.     curSection.SocketGroup = $1
  167.  
  168.   SetFontSize <- "SetFontSize" * (S * >+Digit):
  169.     curSection.SetFontSize = parseInt($1)
  170.  
  171.   MapTier <- "MapTier" * S * ?Operator * S * >+Digit:
  172.     curSection.MapTier = OpAndInt(operator: parseEnum[OperatorEnum]($1), value: parseInt($2))
  173.  
  174.   ItemLevel <- "ItemLevel" * S * ?Operator * S * >+Digit:
  175.     curSection.ItemLevel = OpAndInt(operator: parseEnum[OperatorEnum]($1), value: parseInt($2))
  176.  
  177.   DropLevel <- "DropLevel" * S * ?Operator * S * >+Digit:
  178.     curSection.DropLevel = OpAndInt(operator: parseEnum[OperatorEnum]($1), value: parseInt($2))
  179.  
  180.   Width <- "Width" * S * ?Operator * S * >+Digit:
  181.     curSection.Width = OpAndInt(operator: parseEnum[OperatorEnum]($1), value: parseInt($2))
  182.  
  183.   Height <- "Height" * S * ?Operator * S * >+Digit:
  184.     curSection.Height = OpAndInt(operator: parseEnum[OperatorEnum]($1), value: parseInt($2))
  185.  
  186.   Quality <- "Quality" * S * ?Operator * S * >+Digit:
  187.     curSection.Quality = OpAndInt(operator: parseEnum[OperatorEnum]($1), value: parseInt($2))
  188.  
  189.   GemLevel <- "GemLevel" * S * ?Operator * S * >+Digit:
  190.     curSection.GemLevel = OpAndInt(operator: parseEnum[OperatorEnum]($1), value: parseInt($2))
  191.  
  192.   StackSize <- "StackSize" * S * ?Operator * S * >+Digit:
  193.     curSection.StackSize = OpAndInt(operator: parseEnum[OperatorEnum]($1), value: parseInt($2))
  194.    
  195.   Sockets <- "Sockets" * S * ?Operator * S * >+Digit:
  196.     curSection.Sockets = OpAndInt(operator: parseEnum[OperatorEnum]($1), value: parseInt($2))
  197.    
  198.   LinkedSockets <- "LinkedSockets" * S * ?Operator * S * >+Digit:
  199.     curSection.LinkedSockets = OpAndInt(operator: parseEnum[OperatorEnum]($1), value: parseInt($2))
  200.  
  201.   PlayAlertSound <- "PlayAlertSound" * (S * >+Digit)[2]:
  202.     curSection.PlayAlertSound = Sound(id: parseInt($1), volume: parseInt($2))
  203.  
  204.   PlayAlertSoundPositional <- "PlayAlertSoundPositional" * (S * >+Digit)[2]:
  205.     curSection.PlayAlertSoundPositional = Sound(id: parseInt($1), volume: parseInt($2))
  206.  
  207.   SetTextColor <- "SetTextColor" * (S * >+Digit)[3..4]:
  208.   #TODO: capture.len
  209.     curSection.SetTextColor = Color(r: parseInt($1), g: parseInt($2), b: parseInt($3), a: parseInt($4))
  210.  
  211.   SetBorderColor <- "SetBorderColor" * (S * >+Digit)[3..4]:
  212.     curSection.SetBorderColor = Color(r: parseInt($1), g: parseInt($2), b: parseInt($3), a: parseInt($4))
  213.  
  214.   SetBackgroundColor <- "SetBackgroundColor" * (S * >+Digit)[3..4]:
  215.     curSection.SetBackgroundColor = Color(r: parseInt($1), g: parseInt($2), b: parseInt($3), a: parseInt($4))
  216.  
  217.   Corrupted <- "Corrupted" * S * >("True" | "False"):
  218.     if $1 == "True": curSection.Corrupted = true else: curSection.Corrupted = false
  219.  
  220.   Identified <- "Identified" * S * >("True" | "False"):
  221.     if $1 == "True": curSection.Identified = true else: curSection.Identified = false
  222.  
  223.   ShaperItem <- "ShaperItem" * S * >("True" | "False"):
  224.     if $1 == "True": curSection.ShaperItem = true else: curSection.ShaperItem = false
  225.  
  226.   ElderItem <- "ElderItem" * S * >("True" | "False"):
  227.     if $1 == "True": curSection.ElderItem = true else: curSection.ElderItem = false
  228.  
  229.   ElderMap <- "ElderMap" * S * >("True" | "False"):
  230.     if $1 == "True": curSection.ElderMap = true else: curSection.ElderMap = false
  231.  
  232.   ShapedMap <- "ShapedMap" * S * >("True" | "False"):
  233.     if $1 == "True": curSection.ShapedMap = true else: curSection.ShapedMap = false
  234.  
  235.   FracturedItem <- "FracturedItem" * S * >("True" | "False"):
  236.     if $1 == "True": curSection.FracturedItem = true else: curSection.FracturedItem = false
  237.  
  238.   SynthesisedItem <- "SynthesisedItem" * S * >("True" | "False"):
  239.     if $1 == "True": curSection.SynthesisedItem = true else: curSection.SynthesisedItem = false
  240.  
  241.   AnyEnchantment <- "AnyEnchantment" * S * >("True" | "False"):
  242.     if $1 == "True": curSection.AnyEnchantment = true else: curSection.AnyEnchantment = false
  243.  
  244.   DisableDropSound <- "DisableDropSound" * S * >("True" | "False"):
  245.     if $1 == "True": curSection.DisableDropSound = true else: curSection.DisableDropSound = false
  246.  
  247.   BlightedMap <- "BlightedMap" * S * >("True" | "False"):
  248.     if $1 == "True": curSection.BlightedMap = true else: curSection.BlightedMap = false
  249.  
  250.   Rarity <- "Rarity" * S * ?Operator * S * >("Rare" | "Magic" | "Normal" | "Unique"):
  251.     curSection.Rarity = OpAndRarity(operator: parseEnum[OperatorEnum]($1), rarity: parseEnum[RarityEnum]($2))
  252.  
  253.   PlayEffect <- "PlayEffect" * S * >("Red" | "Green" | "Blue" | "Brown" | "White" | "Yellow") * S * >("True" | "False"):
  254.     var t = false
  255.     if $2 == "True": t = true else: t = false
  256.     curSection.PlayEffect = Effect(id: parseEnum[ColorEnum]($1), temporary: t)
  257.  
  258.   MinimapIcon <- "MinimapIcon" * S * >("Red" | "Green" | "Blue" | "Brown" | "White" | "Yellow") * S * >("Circle" | "Diamond" | "Hexagon" | "Square" | "Star" | "Triangle"):
  259.     curSection.MinimapIcon = Icon(size: parseInt($1), color: parseEnum[ColorEnum]($2), shape: parseEnum[ShapeEnum]($3))
  260.  
  261.  
  262.  
  263.   Operator <- ">=" | ">" | "<=" | "<" | "=="
  264.   NL <- {'\n','\r'}
  265.   S <- *(Space | "#" * *(1 - NL) * ?NL)
  266.  
  267. #end of grammar
  268.  
  269. echo p.matchFile("C:/Users/krab/Documents/My Games/Path of Exile/2.txt").ok
  270.  
  271. proc `$`(c: Color): string =
  272.   "(" & $c.r & ", " & $c.g & ", " & $c.b & ", " & $c.a & ")"
  273.  
  274. for section in sectionsSeq:
  275.   echo section.Visible
  276.   echo section.BaseType
  277.   if section.SetTextColor  != nil: echo section.SetTextColor
  278.   if section.SetBorderColor  != nil: echo section.SetBorderColor
  279.   echo ""
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Top