Page 1 of 1

New Syntax Extension: (Structure pointer as return value)

Posted: Fri Feb 22, 2013 9:49 pm
by IceSoft
Hello all,

Here are my first PureBasic Syntax Extension. Hope you find it valuable.
Compile this soure as 'StructurePointer.exe' and add it to the Tools Settings.

'StrucurePointer.pb": The Syntax Extension source file for 'StructurePointer as return values:

Code: Select all

;-------------------------------------------------
:
; PureBasic Syntax Extension 1.0 (20130220)
; Supports a new StructurePointer syntax like:
;
;    Declare.*<Struct> foo()
;    DeclareC.*<Struct> foo()
;    DeclareCDLL.*<Struct> foo()
;    DeclareDLL.*<Struct> foo()
;    
;    Procedure.*<Struct> foo()
;    ProcedureC.*<Struct> foo()
;    ProcedureCDLL.*<Struct> foo()
;    ProcedureDLL.*<Struct> foo()
;
;    ImportC
;       foo.*<Struct>() As "foo@16"
;    EndImport
;   
;    Import
;       foo.*<Struct>() As "foo@16"
;    EndImport   
;
;    Prototype.*<Struct> foo()
;    PrototypeC.*<Struct> foo()
;
;-------------------------------------------------

NewList aSourceLine.s()
 
file$ = ProgramParameter(0)
 
CreateRegularExpression(0, "\.\*.*?\(")
CreateRegularExpression(1, "\.\*.*? ")
 
If ReadFile(0, file$)   
  While Eof(0) = 0           
    AddElement(aSourceLine())
    aSourceLine() = ReadString(0) 
  Wend
  CloseFile(0)             
EndIf
 
ResetList(aSourceLine()) 
 
If CreateFile(0, file$)   
  s.s = ""
  ForEach aSourceLine() 
    s = aSourceLine()
    If MatchRegularExpression(1, aSourceLine())
       s = ReplaceRegularExpression(1, aSourceLine(), ".i ")
    ElseIf MatchRegularExpression(0, aSourceLine())
       s = ReplaceRegularExpression(0, aSourceLine(), ".i(")
    EndIf
    WriteStringN(0, s)
  Next
  CloseFile(0)       
EndIf
Tool Settings:
Image


Please test the new Syntax Extension with this small example too:

Code: Select all

; Small example 4 StructurePointer syntax extension


Structure myBigStructure
  a.b
  b.s
  c.s
  d.i
  e.l
  f.q
EndStructure

;- Declare with StructurePointer syntax:
Declare.*myBigStructure foo()


;- Normal way with pointers  
*a.myBigStructure = foo()

Debug  *a\a  ; 120
Debug  *a\b  ; myBigStructure
Debug  *a\d  ; 4321
Debug  *a\e  ; 999
Debug  *a\f  ; 1234567890


End
;- Procedure with StructurePointer syntax:
Procedure.*myBigStructure foo()
  *a.myBigStructure = AllocateMemory(SizeOf(myBigStructure))
  *a\a = 120
  *a\b = "myBigStructure"
  *a\d = 4321
  *a\e = 999
  *a\f = 1234567890
  ProcedureReturn *a
EndProcedure  

Re: New Syntax Extension: (Structure pointer as return value

Posted: Sun Feb 24, 2013 11:40 am
by Deeem2031
I like the idea, but there are 2 flaws I noticed:

1. Your regular expressions search until they find "(" or " ". This fails for example with this: "*x = *y.*foo|*z.*bar" (there are probably better examples out there)
You should include characters a identifier for a structure can have instead of exclude characters it can't.
I would use a regular expression like this:

Code: Select all

CreateRegularExpression(0, "\.\*[_a-zA-Z][_0-9a-zA-Z]*")
2. You can use

Code: Select all

Debug *a.myBigStructure\a
but you can't use

Code: Select all

Debug *a.*myBigStructure\a
This is a minor problem but still confusing.

Re: New Syntax Extension: (Structure pointer as return value

Posted: Sun Feb 24, 2013 11:56 am
by IceSoft
Deeem2031 wrote: but you can't use

Code: Select all

Debug *a.*myBigStructure\a
This is a minor problem but still confusing.
Right! But your examples using not 'official' syntax.
The extension should not change the normal Pure Basic syntax of pointers (see on the small example, there is one line using normal pointer Syntax, which normaly dont have a .*<struct>).

Re: New Syntax Extension: (Structure pointer as return value

Posted: Thu Jun 20, 2013 2:52 pm
by IceSoft
@Fred,
maybe it makes sence to add this ideas to the 5.20 version too.
What are you think?