FB Easy Parser(Update 2021.07.19)

User projects written in or related to FreeBASIC.
Post Reply
Kuan Hsu
Posts: 586
Joined: Sep 16, 2007 15:12
Location: Taiwan

FB Easy Parser(Update 2021.07.19)

Post by Kuan Hsu »

Hi every body:

I've written a small program can parse freeBASIC code and output json file, the code is a part of my poseidonFB IDE but translated from D to freeBASIC code, I think maybe someone interest in it......^^

It's name is "easy parser" because of it is design for create the AST tree for aucomplete/calltip in my IDE, it lacks error report and syntax analysis isn't very exact, so "EASY" is mean "NOT EXACT"......^^

V0.15:
It can be download at:http://www.mediafire.com/download/8rw4j ... arserFB.7z
The sources archive include poseidonFB's project file, it can be easy to read/edit/compile using poseidonFB.
Build with freeBASIC V1.05 32bit(64bit) and IUP 3.21

I think it's easy to read and parse....

enjoy!

( V0.18: Modified parser using in poseidonFB V0.406 )
( V0.17: Modified parser using in poseidonFB V0.328 )
( V0.16: Updated parser using in poseidonFB V0.3+; added document page )
( V0.15: Add "Export Json" )
( V0.14: Big Change! Create AST tree and using GUI to show outline tree (like poseidonFB), updated to poseidonFB rev0.266 version parser )
( V0.13: Add Modified parser about variables and function pointer, updated to poseidonFB rev0.251 using version )
( V0.12: Add "tail" element )
( V0.11: Add "Operator" parser )
( V0.10: First Version )
Image
Image
Last edited by Kuan Hsu on Jul 19, 2021 15:16, edited 17 times in total.
Imortis
Moderator
Posts: 1924
Joined: Jun 02, 2005 15:10
Location: USA
Contact:

Re: FB easy parser

Post by Imortis »

I just now had a chance to look at your code, and I noticed you made an object called "DynamicArray". I assume this is to get around the limitation in FBC of dynamic arrays not working in UDTs. As of 1.00.0, Dynamic arrays can be used in UDTs.

Am I mistaken? is there another reason for this object?
Kuan Hsu
Posts: 586
Joined: Sep 16, 2007 15:12
Location: Taiwan

Re: FB easy parser

Post by Kuan Hsu »

Imortis wrote:I just now had a chance to look at your code, and I noticed you made an object called "DynamicArray". I assume this is to get around the limitation in FBC of dynamic arrays not working in UDTs. As of 1.00.0, Dynamic arrays can be used in UDTs.

Am I mistaken? is there another reason for this object?
HAHA, you are right! I've tried but compiler told me "error 9: Expected expression, found ')' in 'test() as string"...@@, so I change to use the typical way-- malloc the memory and the pointer.

The problem is I use test() as string, not test(any) as string......

Thank you!
aloberoger
Posts: 507
Joined: Jan 13, 2009 19:23

Re: FB easy parser

Post by aloberoger »

Can I use this parser to build another type of code with freebasic?
for example udt to class converter ?
Kuan Hsu
Posts: 586
Joined: Sep 16, 2007 15:12
Location: Taiwan

Re: FB easy parser

Post by Kuan Hsu »

aloberoger wrote:Can I use this parser to build another type of code with freebasic?
for example udt to class converter ?
Feel free for use anywhere/use, just I said: enjoy~
aloberoger
Posts: 507
Joined: Jan 13, 2009 19:23

Re: FB easy parser

Post by aloberoger »

your example show how to store result in a json file, so it is necessary to know about json file?
Kuan Hsu
Posts: 586
Joined: Sep 16, 2007 15:12
Location: Taiwan

Re: FB easy parser

Post by Kuan Hsu »

aloberoger wrote:your example show how to store result in a json file, so it is necessary to know about json file?
The JSON format is easy to read that I think the answer is "YES", the official web is one page:http://www.json.org/
Kuan Hsu
Posts: 586
Joined: Sep 16, 2007 15:12
Location: Taiwan

Re: FB easy parser

Post by Kuan Hsu »

Dear all:
I update this project today, for
(1)Test poseidonFB is stable or not
(2)Test freeBASIC with IUP 3.21(32bit/64bit)

I rewrite the code to create AST tree instead of out json file, but next version I will enable to create json file.
Feel free to test and use......^^
Image
Tourist Trap
Posts: 2958
Joined: Jun 02, 2015 16:24

Re: FB easy parser

Post by Tourist Trap »

Kuan Hsu wrote: Feel free for use anywhere/use, just I said: enjoy~
Thanks a lot, I'll try it.
Kuan Hsu
Posts: 586
Joined: Sep 16, 2007 15:12
Location: Taiwan

Re: FB Easy Parser(Update 2021.07.13)

Post by Kuan Hsu »

coderJeff
Site Admin
Posts: 4323
Joined: Nov 04, 2005 14:23
Location: Ontario, Canada
Contact:

Re: FB Easy Parser(Update 2021.07.13)

Post by coderJeff »

It might not be obvious how users should try this out. Here's a demo (with a few edits on your source).

Code: Select all

'' Get the source and build the DLL
'' $ git clone https://bitbucket.org/KuanHsu/fbeasyparser
'' $ cd fbeasyparser

'' for this demo, needed a couple functions exported from the DLL
'' edit parser/ast.bas to add EXPORT to: 
'' - CASTnode.getChild()
'' - CASTnode.getChildrenCount()

'' make a DLL (or make a static library)
'' $ fbc -dll fbeasyparser.bas parser/*.bas

'' Build this demo and try it...
'' - save this file in a new file "atest.bas"
'' $ fbc atest.bas

'' Try the demo on itself
'' $ atest atest.bas 

'' ----------------
'' atest.bas (DEMO)

'' include the internals so we can read the parsernodes directly
'' which will also include kinds from ast.bi so we can decode the kind 
#include "parser/parser.bi"

#include once "FBeasyParser.bi"
#inclib "FBeasyParser"

'' decode KIND to a printable string
function kind2Str( byval kind as integer ) as string
	dim ret as string

	#macro CHK( name_ )
		if( (kind and B_##name_) <> 0 ) then
			ret &= #name_ & " "	
		end if
		ret &= "" 
	#endmacro

	CHK( VARIABLE )
	CHK( FUNCTION )
	CHK( SUB )
	CHK( PROPERTY )
	CHK( CTOR )
	CHK( DTOR )
	CHK( PARAM )
	CHK( TYPE )
	CHK( ENUM )
	CHK( UNION )
	CHK( CLASS )
	CHK( INCLUDE )
	CHK( ENUMMEMBER )
	CHK( ALIAS )
	CHK( BAS )
	CHK( BI )
	CHK( NAMESPACE )
	CHK( MACRO )
	CHK( SCOPE )
	CHK( DEFINE )
	CHK( OPERATOR )
	CHK( WITH )

	return ret
end function

'' print out a node and recurse all children
sub dumpNode2Str( byval node as CASTnode ptr, byval indent as integer = 0 )

	print using "#####"; node->lineNumber_; 
	print space( indent + 1 ); 

	dim s as string
	s &= node->name_
	if( node->type_ > "" ) then 
		s &= ", Type='" & node->type_ & "'"
	end if
	s &= ", " & kind2Str( node->kind_ )
	print s 

	if( node->getChildrenCount > 0 ) then 
		for i as integer = 0 to node->getChildrenCount - 1
			dumpNode2Str( node->getChild( i ), indent + 2 ) 			
		next
	end if
end sub

'' ----------------
'' MAIN

dim as string document

if( command(1) > "" ) then
	var filename = command(1)
	var h = freefile
	if( open( filename for binary access read as #h ) = 0 ) then
		document = space( lof( h ) )
		if( document > "" ) then 
			get #h,, document
		end if
		close #h

		dim as CASTnode ptr root = getParserNode( filename, document )
		dumpNode2Str( root )
	else
		print "Unable to open '" & filename & "'"
	end if
else
	print "error: no filename specified"	
end if

/'
OUTPUT:

    0 atest.bas, BI
   31   kind2Str, Type='string(kind as integer)', FUNCTION
   31     kind, Type='integer', PARAM
   32     ret, Type='string', VARIABLE
   34     CHK, Type='(name_)', MACRO
   48     ), TYPE
   50       ), UNION
   68       dumpNode2Str, Type='(node as CASTnode*,indent as integer)', SUB
   68         node, Type='CASTnode*', PARAM
   68         indent, Type='integer', PARAM
   73         s, Type='string', VARIABLE
   91       document, Type='string', VARIABLE
   94       filename, VARIABLE
   95       h, VARIABLE
  103       root, Type='CASTnode*', VARIABLE
'/
aurelVZAB
Posts: 666
Joined: Jul 02, 2008 14:55
Contact:

Re: FB Easy Parser(Update 2018.12.21)

Post by aurelVZAB »

That is really cool i must try!!
Kuan Hsu
Posts: 586
Joined: Sep 16, 2007 15:12
Location: Taiwan

Re: FB Easy Parser(Update 2018.12.21)

Post by Kuan Hsu »

I commit new reversion to bitbucket, the changes are:
(1) As coderjeff, I Indicate that member functions of ast.bas are "Export"
(2) I modified CParser.parse() function -- No release memory before generate the node, it means that return value of getParserNode() will be reserved when next getParserNode() function calling, but we need release the memory( delete node ) manually before program end.
Post Reply